blob: 83bac7243907c8d25a497b876d1fb46a82396146 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
475static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200486static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200487static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
488static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_asin(typval_T *argvars, typval_T *rettv);
492static void f_atan(typval_T *argvars, typval_T *rettv);
493static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100495static void f_browse(typval_T *argvars, typval_T *rettv);
496static void f_browsedir(typval_T *argvars, typval_T *rettv);
497static void f_bufexists(typval_T *argvars, typval_T *rettv);
498static void f_buflisted(typval_T *argvars, typval_T *rettv);
499static void f_bufloaded(typval_T *argvars, typval_T *rettv);
500static void f_bufname(typval_T *argvars, typval_T *rettv);
501static void f_bufnr(typval_T *argvars, typval_T *rettv);
502static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
503static void f_byte2line(typval_T *argvars, typval_T *rettv);
504static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
505static void f_byteidx(typval_T *argvars, typval_T *rettv);
506static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
507static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100515static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100516static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100517static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
520static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100521static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100523static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
524static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100525static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100526static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_changenr(typval_T *argvars, typval_T *rettv);
529static void f_char2nr(typval_T *argvars, typval_T *rettv);
530static void f_cindent(typval_T *argvars, typval_T *rettv);
531static void f_clearmatches(typval_T *argvars, typval_T *rettv);
532static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000533#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_complete(typval_T *argvars, typval_T *rettv);
535static void f_complete_add(typval_T *argvars, typval_T *rettv);
536static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_confirm(typval_T *argvars, typval_T *rettv);
539static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_cos(typval_T *argvars, typval_T *rettv);
542static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_count(typval_T *argvars, typval_T *rettv);
545static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
546static void f_cursor(typval_T *argsvars, typval_T *rettv);
547static void f_deepcopy(typval_T *argvars, typval_T *rettv);
548static void f_delete(typval_T *argvars, typval_T *rettv);
549static void f_did_filetype(typval_T *argvars, typval_T *rettv);
550static void f_diff_filler(typval_T *argvars, typval_T *rettv);
551static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100552static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_empty(typval_T *argvars, typval_T *rettv);
554static void f_escape(typval_T *argvars, typval_T *rettv);
555static void f_eval(typval_T *argvars, typval_T *rettv);
556static void f_eventhandler(typval_T *argvars, typval_T *rettv);
557static void f_executable(typval_T *argvars, typval_T *rettv);
558static void f_exepath(typval_T *argvars, typval_T *rettv);
559static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_expand(typval_T *argvars, typval_T *rettv);
564static void f_extend(typval_T *argvars, typval_T *rettv);
565static void f_feedkeys(typval_T *argvars, typval_T *rettv);
566static void f_filereadable(typval_T *argvars, typval_T *rettv);
567static void f_filewritable(typval_T *argvars, typval_T *rettv);
568static void f_filter(typval_T *argvars, typval_T *rettv);
569static void f_finddir(typval_T *argvars, typval_T *rettv);
570static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000571#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100572static void f_float2nr(typval_T *argvars, typval_T *rettv);
573static void f_floor(typval_T *argvars, typval_T *rettv);
574static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000575#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100576static void f_fnameescape(typval_T *argvars, typval_T *rettv);
577static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
578static void f_foldclosed(typval_T *argvars, typval_T *rettv);
579static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
580static void f_foldlevel(typval_T *argvars, typval_T *rettv);
581static void f_foldtext(typval_T *argvars, typval_T *rettv);
582static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
583static void f_foreground(typval_T *argvars, typval_T *rettv);
584static void f_function(typval_T *argvars, typval_T *rettv);
585static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200586static void f_garbagecollect_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100587static void f_get(typval_T *argvars, typval_T *rettv);
588static void f_getbufline(typval_T *argvars, typval_T *rettv);
589static void f_getbufvar(typval_T *argvars, typval_T *rettv);
590static void f_getchar(typval_T *argvars, typval_T *rettv);
591static void f_getcharmod(typval_T *argvars, typval_T *rettv);
592static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
593static void f_getcmdline(typval_T *argvars, typval_T *rettv);
594static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
595static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
596static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
597static void f_getcwd(typval_T *argvars, typval_T *rettv);
598static void f_getfontname(typval_T *argvars, typval_T *rettv);
599static void f_getfperm(typval_T *argvars, typval_T *rettv);
600static void f_getfsize(typval_T *argvars, typval_T *rettv);
601static void f_getftime(typval_T *argvars, typval_T *rettv);
602static void f_getftype(typval_T *argvars, typval_T *rettv);
603static void f_getline(typval_T *argvars, typval_T *rettv);
604static void f_getmatches(typval_T *argvars, typval_T *rettv);
605static void f_getpid(typval_T *argvars, typval_T *rettv);
606static void f_getcurpos(typval_T *argvars, typval_T *rettv);
607static void f_getpos(typval_T *argvars, typval_T *rettv);
608static void f_getqflist(typval_T *argvars, typval_T *rettv);
609static void f_getreg(typval_T *argvars, typval_T *rettv);
610static void f_getregtype(typval_T *argvars, typval_T *rettv);
611static void f_gettabvar(typval_T *argvars, typval_T *rettv);
612static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
613static void f_getwinposx(typval_T *argvars, typval_T *rettv);
614static void f_getwinposy(typval_T *argvars, typval_T *rettv);
615static void f_getwinvar(typval_T *argvars, typval_T *rettv);
616static void f_glob(typval_T *argvars, typval_T *rettv);
617static void f_globpath(typval_T *argvars, typval_T *rettv);
618static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
619static void f_has(typval_T *argvars, typval_T *rettv);
620static void f_has_key(typval_T *argvars, typval_T *rettv);
621static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
622static void f_hasmapto(typval_T *argvars, typval_T *rettv);
623static void f_histadd(typval_T *argvars, typval_T *rettv);
624static void f_histdel(typval_T *argvars, typval_T *rettv);
625static void f_histget(typval_T *argvars, typval_T *rettv);
626static void f_histnr(typval_T *argvars, typval_T *rettv);
627static void f_hlID(typval_T *argvars, typval_T *rettv);
628static void f_hlexists(typval_T *argvars, typval_T *rettv);
629static void f_hostname(typval_T *argvars, typval_T *rettv);
630static void f_iconv(typval_T *argvars, typval_T *rettv);
631static void f_indent(typval_T *argvars, typval_T *rettv);
632static void f_index(typval_T *argvars, typval_T *rettv);
633static void f_input(typval_T *argvars, typval_T *rettv);
634static void f_inputdialog(typval_T *argvars, typval_T *rettv);
635static void f_inputlist(typval_T *argvars, typval_T *rettv);
636static void f_inputrestore(typval_T *argvars, typval_T *rettv);
637static void f_inputsave(typval_T *argvars, typval_T *rettv);
638static void f_inputsecret(typval_T *argvars, typval_T *rettv);
639static void f_insert(typval_T *argvars, typval_T *rettv);
640static void f_invert(typval_T *argvars, typval_T *rettv);
641static void f_isdirectory(typval_T *argvars, typval_T *rettv);
642static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100643#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
644static void f_isnan(typval_T *argvars, typval_T *rettv);
645#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100648static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100649static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100650static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100651static void f_job_start(typval_T *argvars, typval_T *rettv);
652static void f_job_stop(typval_T *argvars, typval_T *rettv);
653static void f_job_status(typval_T *argvars, typval_T *rettv);
654#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100656static void f_js_decode(typval_T *argvars, typval_T *rettv);
657static void f_js_encode(typval_T *argvars, typval_T *rettv);
658static void f_json_decode(typval_T *argvars, typval_T *rettv);
659static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_keys(typval_T *argvars, typval_T *rettv);
661static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
662static void f_len(typval_T *argvars, typval_T *rettv);
663static void f_libcall(typval_T *argvars, typval_T *rettv);
664static void f_libcallnr(typval_T *argvars, typval_T *rettv);
665static void f_line(typval_T *argvars, typval_T *rettv);
666static void f_line2byte(typval_T *argvars, typval_T *rettv);
667static void f_lispindent(typval_T *argvars, typval_T *rettv);
668static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_log(typval_T *argvars, typval_T *rettv);
671static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000672#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_map(typval_T *argvars, typval_T *rettv);
677static void f_maparg(typval_T *argvars, typval_T *rettv);
678static void f_mapcheck(typval_T *argvars, typval_T *rettv);
679static void f_match(typval_T *argvars, typval_T *rettv);
680static void f_matchadd(typval_T *argvars, typval_T *rettv);
681static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
682static void f_matcharg(typval_T *argvars, typval_T *rettv);
683static void f_matchdelete(typval_T *argvars, typval_T *rettv);
684static void f_matchend(typval_T *argvars, typval_T *rettv);
685static void f_matchlist(typval_T *argvars, typval_T *rettv);
686static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200687static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_max(typval_T *argvars, typval_T *rettv);
689static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
698static void f_nr2char(typval_T *argvars, typval_T *rettv);
699static void f_or(typval_T *argvars, typval_T *rettv);
700static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
708static void f_printf(typval_T *argvars, typval_T *rettv);
709static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
713#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_range(typval_T *argvars, typval_T *rettv);
717static void f_readfile(typval_T *argvars, typval_T *rettv);
718static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100719#ifdef FEAT_FLOAT
720static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
721#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_reltimestr(typval_T *argvars, typval_T *rettv);
723static void f_remote_expr(typval_T *argvars, typval_T *rettv);
724static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
725static void f_remote_peek(typval_T *argvars, typval_T *rettv);
726static void f_remote_read(typval_T *argvars, typval_T *rettv);
727static void f_remote_send(typval_T *argvars, typval_T *rettv);
728static void f_remove(typval_T *argvars, typval_T *rettv);
729static void f_rename(typval_T *argvars, typval_T *rettv);
730static void f_repeat(typval_T *argvars, typval_T *rettv);
731static void f_resolve(typval_T *argvars, typval_T *rettv);
732static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_screenattr(typval_T *argvars, typval_T *rettv);
737static void f_screenchar(typval_T *argvars, typval_T *rettv);
738static void f_screencol(typval_T *argvars, typval_T *rettv);
739static void f_screenrow(typval_T *argvars, typval_T *rettv);
740static void f_search(typval_T *argvars, typval_T *rettv);
741static void f_searchdecl(typval_T *argvars, typval_T *rettv);
742static void f_searchpair(typval_T *argvars, typval_T *rettv);
743static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
744static void f_searchpos(typval_T *argvars, typval_T *rettv);
745static void f_server2client(typval_T *argvars, typval_T *rettv);
746static void f_serverlist(typval_T *argvars, typval_T *rettv);
747static void f_setbufvar(typval_T *argvars, typval_T *rettv);
748static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
749static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100750static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_setline(typval_T *argvars, typval_T *rettv);
752static void f_setloclist(typval_T *argvars, typval_T *rettv);
753static void f_setmatches(typval_T *argvars, typval_T *rettv);
754static void f_setpos(typval_T *argvars, typval_T *rettv);
755static void f_setqflist(typval_T *argvars, typval_T *rettv);
756static void f_setreg(typval_T *argvars, typval_T *rettv);
757static void f_settabvar(typval_T *argvars, typval_T *rettv);
758static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
759static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_shellescape(typval_T *argvars, typval_T *rettv);
764static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
765static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sin(typval_T *argvars, typval_T *rettv);
768static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_sort(typval_T *argvars, typval_T *rettv);
771static void f_soundfold(typval_T *argvars, typval_T *rettv);
772static void f_spellbadword(typval_T *argvars, typval_T *rettv);
773static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
774static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_sqrt(typval_T *argvars, typval_T *rettv);
777static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_str2nr(typval_T *argvars, typval_T *rettv);
780static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200784static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100785static void f_stridx(typval_T *argvars, typval_T *rettv);
786static void f_string(typval_T *argvars, typval_T *rettv);
787static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200788static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_strpart(typval_T *argvars, typval_T *rettv);
790static void f_strridx(typval_T *argvars, typval_T *rettv);
791static void f_strtrans(typval_T *argvars, typval_T *rettv);
792static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
793static void f_strwidth(typval_T *argvars, typval_T *rettv);
794static void f_submatch(typval_T *argvars, typval_T *rettv);
795static void f_substitute(typval_T *argvars, typval_T *rettv);
796static void f_synID(typval_T *argvars, typval_T *rettv);
797static void f_synIDattr(typval_T *argvars, typval_T *rettv);
798static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
799static void f_synstack(typval_T *argvars, typval_T *rettv);
800static void f_synconcealed(typval_T *argvars, typval_T *rettv);
801static void f_system(typval_T *argvars, typval_T *rettv);
802static void f_systemlist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
804static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
805static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
806static void f_taglist(typval_T *argvars, typval_T *rettv);
807static void f_tagfiles(typval_T *argvars, typval_T *rettv);
808static void f_tempname(typval_T *argvars, typval_T *rettv);
809static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200810#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_tan(typval_T *argvars, typval_T *rettv);
812static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200813#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100814#ifdef FEAT_TIMERS
815static void f_timer_start(typval_T *argvars, typval_T *rettv);
816static void f_timer_stop(typval_T *argvars, typval_T *rettv);
817#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static void f_tolower(typval_T *argvars, typval_T *rettv);
819static void f_toupper(typval_T *argvars, typval_T *rettv);
820static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000821#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static void f_type(typval_T *argvars, typval_T *rettv);
825static void f_undofile(typval_T *argvars, typval_T *rettv);
826static void f_undotree(typval_T *argvars, typval_T *rettv);
827static void f_uniq(typval_T *argvars, typval_T *rettv);
828static void f_values(typval_T *argvars, typval_T *rettv);
829static void f_virtcol(typval_T *argvars, typval_T *rettv);
830static void f_visualmode(typval_T *argvars, typval_T *rettv);
831static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100832static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100833static void f_win_getid(typval_T *argvars, typval_T *rettv);
834static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
835static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
836static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_winbufnr(typval_T *argvars, typval_T *rettv);
838static void f_wincol(typval_T *argvars, typval_T *rettv);
839static void f_winheight(typval_T *argvars, typval_T *rettv);
840static void f_winline(typval_T *argvars, typval_T *rettv);
841static void f_winnr(typval_T *argvars, typval_T *rettv);
842static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
843static void f_winrestview(typval_T *argvars, typval_T *rettv);
844static void f_winsaveview(typval_T *argvars, typval_T *rettv);
845static void f_winwidth(typval_T *argvars, typval_T *rettv);
846static void f_writefile(typval_T *argvars, typval_T *rettv);
847static void f_wordcount(typval_T *argvars, typval_T *rettv);
848static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
851static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
852static int get_env_len(char_u **arg);
853static int get_id_len(char_u **arg);
854static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
855static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000856#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
857#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
858 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
860static int eval_isnamec(int c);
861static int eval_isnamec1(int c);
862static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
863static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864static typval_T *alloc_string_tv(char_u *string);
865static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100866#ifdef FEAT_FLOAT
867static float_T get_tv_float(typval_T *varp);
868#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static linenr_T get_tv_lnum(typval_T *argvars);
870static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
872static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
873static hashtab_T *find_var_ht(char_u *name, char_u **varname);
874static funccall_T *get_funccal(void);
875static void vars_clear_ext(hashtab_T *ht, int free_val);
876static void delete_var(hashtab_T *ht, hashitem_T *hi);
877static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
878static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
879static void set_var(char_u *name, typval_T *varp, int copy);
880static int var_check_ro(int flags, char_u *name, int use_gettext);
881static int var_check_fixed(int flags, char_u *name, int use_gettext);
882static int var_check_func_name(char_u *name, int new_var);
883static int valid_varname(char_u *varname);
884static int tv_check_lock(int lock, char_u *name, int use_gettext);
885static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
886static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100887static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int eval_fname_script(char_u *p);
889static int eval_fname_sid(char_u *p);
890static void list_func_head(ufunc_T *fp, int indent);
891static ufunc_T *find_func(char_u *name);
892static int function_exists(char_u *name);
893static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100895static void func_do_profile(ufunc_T *fp);
896static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
897static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000898static int
899# ifdef __BORLANDC__
900 _RTLENTRYF
901# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000903static int
904# ifdef __BORLANDC__
905 _RTLENTRYF
906# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100909static int script_autoload(char_u *name, int reload);
910static char_u *autoload_name(char_u *name);
911static void cat_func_name(char_u *buf, ufunc_T *fp);
912static void func_free(ufunc_T *fp);
913static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
914static int can_free_funccal(funccall_T *fc, int copyID) ;
915static void free_funccal(funccall_T *fc, int free_val);
916static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
917static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
918static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
919static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
920static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
921static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
922static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
923static int write_list(FILE *fd, list_T *list, int binary);
924static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200926
927#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100928static int compare_func_name(const void *s1, const void *s2);
929static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930#endif
931
Bram Moolenaar33570922005-01-25 22:26:29 +0000932/*
933 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000934 */
935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000937{
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 int i;
939 struct vimvar *p;
940
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200941 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
942 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200943 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000944 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946
947 for (i = 0; i < VV_LEN; ++i)
948 {
949 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200950 if (STRLEN(p->vv_name) > 16)
951 {
952 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
953 getout(1);
954 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 STRCPY(p->vv_di.di_key, p->vv_name);
956 if (p->vv_flags & VV_RO)
957 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
958 else if (p->vv_flags & VV_RO_SBX)
959 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
960 else
961 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000962
963 /* add to v: scope dict, unless the value is not always available */
964 if (p->vv_type != VAR_UNKNOWN)
965 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000967 /* add to compat scope dict */
968 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100970 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
971
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000972 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100973 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200974 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100975 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100976
977 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
978 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
979 set_vim_var_nr(VV_NONE, VVAL_NONE);
980 set_vim_var_nr(VV_NULL, VVAL_NULL);
981
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200982 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200983
984#ifdef EBCDIC
985 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100986 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200987 */
988 sortFunctions();
989#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000990}
991
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992#if defined(EXITFREE) || defined(PROTO)
993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100994eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995{
996 int i;
997 struct vimvar *p;
998
999 for (i = 0; i < VV_LEN; ++i)
1000 {
1001 p = &vimvars[i];
1002 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001003 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001004 vim_free(p->vv_str);
1005 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001006 }
1007 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1008 {
1009 list_unref(p->vv_list);
1010 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001011 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012 }
1013 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001014 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001015 hash_clear(&compat_hashtab);
1016
Bram Moolenaard9fba312005-06-26 22:34:35 +00001017 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001018# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001019 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001020# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021
1022 /* global variables */
1023 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001024
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001025 /* autoloaded script names */
1026 ga_clear_strings(&ga_loaded);
1027
Bram Moolenaarcca74132013-09-25 21:00:28 +02001028 /* Script-local variables. First clear all the variables and in a second
1029 * loop free the scriptvar_T, because a variable in one script might hold
1030 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001031 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001032 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001033 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001034 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001035 ga_clear(&ga_scripts);
1036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001037 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001038 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001039
1040 /* functions */
1041 free_all_functions();
1042 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001043}
1044#endif
1045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Return the name of the executed function.
1048 */
1049 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001050func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
1054
1055/*
1056 * Return the address holding the next breakpoint line for a funccall cookie.
1057 */
1058 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001059func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar33570922005-01-25 22:26:29 +00001061 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the debug tick for a funccall cookie.
1066 */
1067 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001068func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the nesting level for a funccall cookie.
1075 */
1076 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001083funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001085/* pointer to list of previously used funccal, still around because some
1086 * item in it is still being used. */
1087funccall_T *previous_funccal = NULL;
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089/*
1090 * Return TRUE when a function was ended by a ":return" command.
1091 */
1092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001093current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 return current_funccal->returned;
1096}
1097
1098
1099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * Set an internal variable to a string value. Creates the variable if it does
1101 * not already exist.
1102 */
1103 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001104set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001106 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001107 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 val = vim_strsave(value);
1110 if (val != NULL)
1111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001112 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001113 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 }
1119}
1120
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123static char_u *redir_endp = NULL;
1124static char_u *redir_varname = NULL;
1125
1126/*
1127 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001128 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 * Returns OK if successfully completed the setup. FAIL otherwise.
1130 */
1131 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001132var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133{
1134 int save_emsg;
1135 int err;
1136 typval_T tv;
1137
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001139 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 {
1141 EMSG(_(e_invarg));
1142 return FAIL;
1143 }
1144
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 redir_varname = vim_strsave(name);
1147 if (redir_varname == NULL)
1148 return FAIL;
1149
1150 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1151 if (redir_lval == NULL)
1152 {
1153 var_redir_stop();
1154 return FAIL;
1155 }
1156
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 /* The output is stored in growarray "redir_ga" until redirection ends. */
1158 ga_init2(&redir_ga, (int)sizeof(char), 500);
1159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001162 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1164 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (redir_endp != NULL && *redir_endp != NUL)
1167 /* Trailing characters are present after the variable name */
1168 EMSG(_(e_trailing));
1169 else
1170 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 var_redir_stop();
1173 return FAIL;
1174 }
1175
1176 /* check if we can write to the variable: set it to or append an empty
1177 * string */
1178 save_emsg = did_emsg;
1179 did_emsg = FALSE;
1180 tv.v_type = VAR_STRING;
1181 tv.vval.v_string = (char_u *)"";
1182 if (append)
1183 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1184 else
1185 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001186 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001188 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 if (err)
1190 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
1193 return FAIL;
1194 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
1196 return OK;
1197}
1198
1199/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 * Append "value[value_len]" to the variable set by var_redir_start().
1201 * The actual appending is postponed until redirection ends, because the value
1202 * appended may in fact be the string we write to, changing it may cause freed
1203 * memory to be used:
1204 * :redir => foo
1205 * :let foo
1206 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001209var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001211 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212
1213 if (redir_lval == NULL)
1214 return;
1215
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001217 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001219 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222 {
1223 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001224 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 }
1226 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228}
1229
1230/*
1231 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001232 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233 */
1234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001235var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237 typval_T tv;
1238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239 if (redir_lval != NULL)
1240 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 /* If there was no error: assign the text to the variable. */
1242 if (redir_endp != NULL)
1243 {
1244 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1245 tv.v_type = VAR_STRING;
1246 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001247 /* Call get_lval() again, if it's inside a Dict or List it may
1248 * have changed. */
1249 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001250 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001251 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1252 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1253 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001254 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001255
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001256 /* free the collected output */
1257 vim_free(redir_ga.ga_data);
1258 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001259
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001260 vim_free(redir_lval);
1261 redir_lval = NULL;
1262 }
1263 vim_free(redir_varname);
1264 redir_varname = NULL;
1265}
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267# if defined(FEAT_MBYTE) || defined(PROTO)
1268 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001269eval_charconvert(
1270 char_u *enc_from,
1271 char_u *enc_to,
1272 char_u *fname_from,
1273 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1278 set_vim_var_string(VV_CC_TO, enc_to, -1);
1279 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1280 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1281 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1282 err = TRUE;
1283 set_vim_var_string(VV_CC_FROM, NULL, -1);
1284 set_vim_var_string(VV_CC_TO, NULL, -1);
1285 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1286 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1287
1288 if (err)
1289 return FAIL;
1290 return OK;
1291}
1292# endif
1293
1294# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int err = FALSE;
1299
1300 set_vim_var_string(VV_FNAME_IN, fname, -1);
1301 set_vim_var_string(VV_CMDARG, args, -1);
1302 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1303 err = TRUE;
1304 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1305 set_vim_var_string(VV_CMDARG, NULL, -1);
1306
1307 if (err)
1308 {
1309 mch_remove(fname);
1310 return FAIL;
1311 }
1312 return OK;
1313}
1314# endif
1315
1316# if defined(FEAT_DIFF) || defined(PROTO)
1317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_diff(
1319 char_u *origfile,
1320 char_u *newfile,
1321 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 int err = FALSE;
1324
1325 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1326 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1327 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1328 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1329 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1330 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1331 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1332}
1333
1334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335eval_patch(
1336 char_u *origfile,
1337 char_u *difffile,
1338 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 int err;
1341
1342 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1343 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1344 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1345 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1346 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1347 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1348 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1349}
1350# endif
1351
1352/*
1353 * Top level evaluation function, returning a boolean.
1354 * Sets "error" to TRUE if there was an error.
1355 * Return TRUE or FALSE.
1356 */
1357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001358eval_to_bool(
1359 char_u *arg,
1360 int *error,
1361 char_u **nextcmd,
1362 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 int retval = FALSE;
1366
1367 if (skip)
1368 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 else
1372 {
1373 *error = FALSE;
1374 if (!skip)
1375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001376 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379 }
1380 if (skip)
1381 --emsg_skip;
1382
1383 return retval;
1384}
1385
1386/*
1387 * Top level evaluation function, returning a string. If "skip" is TRUE,
1388 * only parsing to "nextcmd" is done, without reporting errors. Return
1389 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1390 */
1391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001392eval_to_string_skip(
1393 char_u *arg,
1394 char_u **nextcmd,
1395 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *retval;
1399
1400 if (skip)
1401 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 retval = NULL;
1404 else
1405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 retval = vim_strsave(get_tv_string(&tv));
1407 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 if (skip)
1410 --emsg_skip;
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001416 * Skip over an expression at "*pp".
1417 * Return FAIL for an error, OK otherwise.
1418 */
1419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001423
1424 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001426}
1427
1428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430 * When "convert" is TRUE convert a List into a sequence of lines and convert
1431 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 * Return pointer to allocated memory, or NULL for failure.
1433 */
1434 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001435eval_to_string(
1436 char_u *arg,
1437 char_u **nextcmd,
1438 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
Bram Moolenaar33570922005-01-25 22:26:29 +00001440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001443#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001444 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = NULL;
1449 else
1450 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001451 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 {
1453 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001454 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001455 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001456 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001457 if (tv.vval.v_list->lv_len > 0)
1458 ga_append(&ga, NL);
1459 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001460 ga_append(&ga, NUL);
1461 retval = (char_u *)ga.ga_data;
1462 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001463#ifdef FEAT_FLOAT
1464 else if (convert && tv.v_type == VAR_FLOAT)
1465 {
1466 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1467 retval = vim_strsave(numbuf);
1468 }
1469#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 else
1471 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474
1475 return retval;
1476}
1477
1478/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001479 * Call eval_to_string() without using current local variables and using
1480 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 */
1482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_string_safe(
1484 char_u *arg,
1485 char_u **nextcmd,
1486 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 char_u *retval;
1489 void *save_funccalp;
1490
1491 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001492 if (use_sandbox)
1493 ++sandbox;
1494 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001495 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001496 if (use_sandbox)
1497 --sandbox;
1498 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 restore_funccal(save_funccalp);
1500 return retval;
1501}
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503/*
1504 * Top level evaluation function, returning a number.
1505 * Evaluates "expr" silently.
1506 * Returns -1 for an error.
1507 */
1508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar33570922005-01-25 22:26:29 +00001511 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001513 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515 ++emsg_off;
1516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 retval = -1;
1519 else
1520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001521 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 --emsg_off;
1525
1526 return retval;
1527}
1528
Bram Moolenaara40058a2005-07-11 22:42:07 +00001529/*
1530 * Prepare v: variable "idx" to be used.
1531 * Save the current typeval in "save_tv".
1532 * When not used yet add the variable to the v: hashtable.
1533 */
1534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001536{
1537 *save_tv = vimvars[idx].vv_tv;
1538 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1539 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1540}
1541
1542/*
1543 * Restore v: variable "idx" to typeval "save_tv".
1544 * When no longer defined, remove the variable from the v: hashtable.
1545 */
1546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001548{
1549 hashitem_T *hi;
1550
Bram Moolenaara40058a2005-07-11 22:42:07 +00001551 vimvars[idx].vv_tv = *save_tv;
1552 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1553 {
1554 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1555 if (HASHITEM_EMPTY(hi))
1556 EMSG2(_(e_intern2), "restore_vimvar()");
1557 else
1558 hash_remove(&vimvarht, hi);
1559 }
1560}
1561
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001562#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001563/*
1564 * Evaluate an expression to a list with suggestions.
1565 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001566 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 */
1568 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570{
1571 typval_T save_val;
1572 typval_T rettv;
1573 list_T *list = NULL;
1574 char_u *p = skipwhite(expr);
1575
1576 /* Set "v:val" to the bad word. */
1577 prepare_vimvar(VV_VAL, &save_val);
1578 vimvars[VV_VAL].vv_type = VAR_STRING;
1579 vimvars[VV_VAL].vv_str = badword;
1580 if (p_verbose == 0)
1581 ++emsg_off;
1582
1583 if (eval1(&p, &rettv, TRUE) == OK)
1584 {
1585 if (rettv.v_type != VAR_LIST)
1586 clear_tv(&rettv);
1587 else
1588 list = rettv.vval.v_list;
1589 }
1590
1591 if (p_verbose == 0)
1592 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593 restore_vimvar(VV_VAL, &save_val);
1594
1595 return list;
1596}
1597
1598/*
1599 * "list" is supposed to contain two items: a word and a number. Return the
1600 * word in "pp" and the number as the return value.
1601 * Return -1 if anything isn't right.
1602 * Used to get the good word and score from the eval_spell_expr() result.
1603 */
1604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001606{
1607 listitem_T *li;
1608
1609 li = list->lv_first;
1610 if (li == NULL)
1611 return -1;
1612 *pp = get_tv_string(&li->li_tv);
1613
1614 li = li->li_next;
1615 if (li == NULL)
1616 return -1;
1617 return get_tv_number(&li->li_tv);
1618}
1619#endif
1620
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001621/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001622 * Top level evaluation function.
1623 * Returns an allocated typval_T with the result.
1624 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001625 */
1626 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001628{
1629 typval_T *tv;
1630
1631 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633 {
1634 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001635 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001636 }
1637
1638 return tv;
1639}
1640
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 * Uses argv[argc] for the function arguments. Only Number and String
1645 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649call_vim_function(
1650 char_u *func,
1651 int argc,
1652 char_u **argv,
1653 int safe, /* use the sandbox */
1654 int str_arg_only, /* all arguments are strings */
1655 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 long n;
1659 int len;
1660 int i;
1661 int doesrange;
1662 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001665 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 for (i = 0; i < argc; i++)
1670 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001671 /* Pass a NULL or empty argument as an empty string */
1672 if (argv[i] == NULL || *argv[i] == NUL)
1673 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001674 argvars[i].v_type = VAR_STRING;
1675 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001676 continue;
1677 }
1678
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001679 if (str_arg_only)
1680 len = 0;
1681 else
1682 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001683 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (len != 0 && len == (int)STRLEN(argv[i]))
1685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001686 argvars[i].v_type = VAR_NUMBER;
1687 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
1689 else
1690 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001691 argvars[i].v_type = VAR_STRING;
1692 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694 }
1695
1696 if (safe)
1697 {
1698 save_funccalp = save_funccal();
1699 ++sandbox;
1700 }
1701
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1703 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001705 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (safe)
1707 {
1708 --sandbox;
1709 restore_funccal(save_funccalp);
1710 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 vim_free(argvars);
1712
1713 if (ret == FAIL)
1714 clear_tv(rettv);
1715
1716 return ret;
1717}
1718
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001719/*
1720 * Call vimL function "func" and return the result as a number.
1721 * Returns -1 when calling the function fails.
1722 * Uses argv[argc] for the function arguments.
1723 */
1724 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retnr(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001730{
1731 typval_T rettv;
1732 long retval;
1733
1734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1736 return -1;
1737
1738 retval = get_tv_number_chk(&rettv, NULL);
1739 clear_tv(&rettv);
1740 return retval;
1741}
1742
1743#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1744 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1745
Bram Moolenaar4f688582007-07-24 12:34:30 +00001746# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001748 * Call vimL function "func" and return the result as a string.
1749 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 * Uses argv[argc] for the function arguments.
1751 */
1752 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753call_func_retstr(
1754 char_u *func,
1755 int argc,
1756 char_u **argv,
1757 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758{
1759 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001760 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001762 /* All arguments are passed as strings, no conversion to number. */
1763 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 return NULL;
1765
1766 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 return retval;
1769}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001770# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001772/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001773 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001775 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778call_func_retlist(
1779 char_u *func,
1780 int argc,
1781 char_u **argv,
1782 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783{
1784 typval_T rettv;
1785
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001786 /* All arguments are passed as strings, no conversion to number. */
1787 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 return NULL;
1789
1790 if (rettv.v_type != VAR_LIST)
1791 {
1792 clear_tv(&rettv);
1793 return NULL;
1794 }
1795
1796 return rettv.vval.v_list;
1797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
1799
1800/*
1801 * Save the current function call pointer, and set it to NULL.
1802 * Used when executing autocommands and for ":source".
1803 */
1804 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 current_funccal = NULL;
1810 return (void *)fc;
1811}
1812
1813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = (funccall_T *)vfc;
1817
1818 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
Bram Moolenaar05159a02005-02-26 23:04:13 +00001821#if defined(FEAT_PROFILE) || defined(PROTO)
1822/*
1823 * Prepare profiling for entering a child or something else that is not
1824 * counted for the script/function itself.
1825 * Should always be called in pair with prof_child_exit().
1826 */
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828prof_child_enter(
1829 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830{
1831 funccall_T *fc = current_funccal;
1832
1833 if (fc != NULL && fc->func->uf_profiling)
1834 profile_start(&fc->prof_child);
1835 script_prof_save(tm);
1836}
1837
1838/*
1839 * Take care of time spent in a child.
1840 * Should always be called after prof_child_enter().
1841 */
1842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843prof_child_exit(
1844 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001845{
1846 funccall_T *fc = current_funccal;
1847
1848 if (fc != NULL && fc->func->uf_profiling)
1849 {
1850 profile_end(&fc->prof_child);
1851 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1852 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1853 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1854 }
1855 script_prof_restore(tm);
1856}
1857#endif
1858
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#ifdef FEAT_FOLDING
1861/*
1862 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1863 * it in "*cp". Doesn't give error messages.
1864 */
1865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001866eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 int retval;
1870 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001871 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1872 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001875 if (use_sandbox)
1876 ++sandbox;
1877 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 retval = 0;
1881 else
1882 {
1883 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 if (tv.v_type == VAR_NUMBER)
1885 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001886 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 retval = 0;
1888 else
1889 {
1890 /* If the result is a string, check if there is a non-digit before
1891 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (!VIM_ISDIGIT(*s) && *s != '-')
1894 *cp = *s++;
1895 retval = atol((char *)s);
1896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001900 if (use_sandbox)
1901 --sandbox;
1902 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 return retval;
1905}
1906#endif
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 * ":let" list all variable values
1910 * ":let var1 var2" list variable values
1911 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 * ":let var += expr" assignment command.
1913 * ":let var -= expr" assignment command.
1914 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 */
1917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 int var_count = 0;
1925 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001927 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaardb552d602006-03-23 22:59:57 +00001930 argend = skip_var_list(arg, &var_count, &semicolon);
1931 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001933 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1934 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 expr = skipwhite(argend);
1936 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1937 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001939 /*
1940 * ":let" without "=": list variables
1941 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 if (*arg == '[')
1943 EMSG(_(e_invarg));
1944 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001946 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 list_glob_vars(&first);
1951 list_buf_vars(&first);
1952 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001955#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 list_script_vars(&first);
1957 list_func_vars(&first);
1958 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 eap->nextcmd = check_nextcmd(arg);
1961 }
1962 else
1963 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 op[0] = '=';
1965 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001966 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001968 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1969 op[0] = *expr; /* +=, -= or .= */
1970 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001972 else
1973 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (eap->skip)
1976 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (eap->skip)
1979 {
1980 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 --emsg_skip;
1983 }
1984 else if (i != FAIL)
1985 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 }
1991}
1992
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993/*
1994 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1995 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 * When "nextchars" is not NULL it points to a string with characters that
1997 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1998 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 * Returns OK or FAIL;
2000 */
2001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002002ex_let_vars(
2003 char_u *arg_start,
2004 typval_T *tv,
2005 int copy, /* copy values from "tv", don't move */
2006 int semicolon, /* from skip_var_list() */
2007 int var_count, /* from skip_var_list() */
2008 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009{
2010 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002011 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002013 listitem_T *item;
2014 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015
2016 if (*arg != '[')
2017 {
2018 /*
2019 * ":let var = expr" or ":for var in list"
2020 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 return FAIL;
2023 return OK;
2024 }
2025
2026 /*
2027 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2028 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002029 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 {
2031 EMSG(_(e_listreq));
2032 return FAIL;
2033 }
2034
2035 i = list_len(l);
2036 if (semicolon == 0 && var_count < i)
2037 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002038 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 return FAIL;
2040 }
2041 if (var_count - semicolon > i)
2042 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002043 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 return FAIL;
2045 }
2046
2047 item = l->lv_first;
2048 while (*arg != ']')
2049 {
2050 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 item = item->li_next;
2053 if (arg == NULL)
2054 return FAIL;
2055
2056 arg = skipwhite(arg);
2057 if (*arg == ';')
2058 {
2059 /* Put the rest of the list (may be empty) in the var after ';'.
2060 * Create a new list for this. */
2061 l = list_alloc();
2062 if (l == NULL)
2063 return FAIL;
2064 while (item != NULL)
2065 {
2066 list_append_tv(l, &item->li_tv);
2067 item = item->li_next;
2068 }
2069
2070 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002071 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 ltv.vval.v_list = l;
2073 l->lv_refcount = 1;
2074
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2076 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 clear_tv(&ltv);
2078 if (arg == NULL)
2079 return FAIL;
2080 break;
2081 }
2082 else if (*arg != ',' && *arg != ']')
2083 {
2084 EMSG2(_(e_intern2), "ex_let_vars()");
2085 return FAIL;
2086 }
2087 }
2088
2089 return OK;
2090}
2091
2092/*
2093 * Skip over assignable variable "var" or list of variables "[var, var]".
2094 * Used for ":let varvar = expr" and ":for varvar in expr".
2095 * For "[var, var]" increment "*var_count" for each variable.
2096 * for "[var, var; var]" set "semicolon".
2097 * Return NULL for an error.
2098 */
2099 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002100skip_var_list(
2101 char_u *arg,
2102 int *var_count,
2103 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104{
2105 char_u *p, *s;
2106
2107 if (*arg == '[')
2108 {
2109 /* "[var, var]": find the matching ']'. */
2110 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002111 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112 {
2113 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2114 s = skip_var_one(p);
2115 if (s == p)
2116 {
2117 EMSG2(_(e_invarg2), p);
2118 return NULL;
2119 }
2120 ++*var_count;
2121
2122 p = skipwhite(s);
2123 if (*p == ']')
2124 break;
2125 else if (*p == ';')
2126 {
2127 if (*semicolon == 1)
2128 {
2129 EMSG(_("Double ; in list of variables"));
2130 return NULL;
2131 }
2132 *semicolon = 1;
2133 }
2134 else if (*p != ',')
2135 {
2136 EMSG2(_(e_invarg2), p);
2137 return NULL;
2138 }
2139 }
2140 return p + 1;
2141 }
2142 else
2143 return skip_var_one(arg);
2144}
2145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002147 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002148 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002149 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002153 if (*arg == '@' && arg[1] != NUL)
2154 return arg + 2;
2155 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2156 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002157}
2158
Bram Moolenaara7043832005-01-21 11:56:39 +00002159/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 * List variables for hashtab "ht" with prefix "prefix".
2161 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_hashtable_vars(
2165 hashtab_T *ht,
2166 char_u *prefix,
2167 int empty,
2168 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 hashitem_T *hi;
2171 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 int todo;
2173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002174 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002175 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2176 {
2177 if (!HASHITEM_EMPTY(hi))
2178 {
2179 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 di = HI2DI(hi);
2181 if (empty || di->di_tv.v_type != VAR_STRING
2182 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 }
2185 }
2186}
2187
2188/*
2189 * List global variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002193{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
2197/*
2198 * List buffer variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 char_u numbuf[NUMBUFLEN];
2204
Bram Moolenaar429fa852013-04-15 12:27:36 +02002205 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207
2208 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2210 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002211}
2212
2213/*
2214 * List window variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002218{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223#ifdef FEAT_WINDOWS
2224/*
2225 * List tab page variables.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002230 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232}
2233#endif
2234
Bram Moolenaara7043832005-01-21 11:56:39 +00002235/*
2236 * List Vim variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242}
2243
2244/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002245 * List script-local variables, if there is a script.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002249{
2250 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2252 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002253}
2254
2255/*
2256 * List function variables, if there is a function.
2257 */
2258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002260{
2261 if (current_funccal != NULL)
2262 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002264}
2265
2266/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 * List variables in "arg".
2268 */
2269 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271{
2272 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 char_u *name_start;
2276 char_u *arg_subsc;
2277 char_u *tofree;
2278 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279
2280 while (!ends_excmd(*arg) && !got_int)
2281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002284 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2286 {
2287 emsg_severe = TRUE;
2288 EMSG(_(e_trailing));
2289 break;
2290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 /* get_name_len() takes care of expanding curly braces */
2295 name_start = name = arg;
2296 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2297 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 /* This is mainly to keep test 49 working: when expanding
2300 * curly braces fails overrule the exception error message. */
2301 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 emsg_severe = TRUE;
2304 EMSG2(_(e_invarg2), arg);
2305 break;
2306 }
2307 error = TRUE;
2308 }
2309 else
2310 {
2311 if (tofree != NULL)
2312 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002313 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 else
2316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 /* handle d.key, l[idx], f(expr) */
2318 arg_subsc = arg;
2319 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 case 'g': list_glob_vars(first); break;
2328 case 'b': list_buf_vars(first); break;
2329 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002330#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002331 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002332#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002333 case 'v': list_vim_vars(first); break;
2334 case 's': list_script_vars(first); break;
2335 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336 default:
2337 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
2340 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002341 {
2342 char_u numbuf[NUMBUFLEN];
2343 char_u *tf;
2344 int c;
2345 char_u *s;
2346
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002347 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002348 c = *arg;
2349 *arg = NUL;
2350 list_one_var_a((char_u *)"",
2351 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002352 tv.v_type,
2353 s == NULL ? (char_u *)"" : s,
2354 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002355 *arg = c;
2356 vim_free(tf);
2357 }
2358 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362
2363 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002365
2366 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
2368
2369 return arg;
2370}
2371
2372/*
2373 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2374 * Returns a pointer to the char just after the var name.
2375 * Returns NULL if there is an error.
2376 */
2377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378ex_let_one(
2379 char_u *arg, /* points to variable name */
2380 typval_T *tv, /* value to assign to variable */
2381 int copy, /* copy value from "tv" */
2382 char_u *endchars, /* valid chars after variable name or NULL */
2383 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384{
2385 int c1;
2386 char_u *name;
2387 char_u *p;
2388 char_u *arg_end = NULL;
2389 int len;
2390 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392
2393 /*
2394 * ":let $VAR = expr": Set environment variable.
2395 */
2396 if (*arg == '$')
2397 {
2398 /* Find the end of the name. */
2399 ++arg;
2400 name = arg;
2401 len = get_env_len(&arg);
2402 if (len == 0)
2403 EMSG2(_(e_invarg2), name - 1);
2404 else
2405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 if (op != NULL && (*op == '+' || *op == '-'))
2407 EMSG2(_(e_letwrong), op);
2408 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002409 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002411 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 {
2413 c1 = name[len];
2414 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 p = get_tv_string_chk(tv);
2416 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 {
2418 int mustfree = FALSE;
2419 char_u *s = vim_getenv(name, &mustfree);
2420
2421 if (s != NULL)
2422 {
2423 p = tofree = concat_str(s, p);
2424 if (mustfree)
2425 vim_free(s);
2426 }
2427 }
2428 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 if (STRICMP(name, "HOME") == 0)
2432 init_homedir();
2433 else if (didset_vim && STRICMP(name, "VIM") == 0)
2434 didset_vim = FALSE;
2435 else if (didset_vimruntime
2436 && STRICMP(name, "VIMRUNTIME") == 0)
2437 didset_vimruntime = FALSE;
2438 arg_end = arg;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444 }
2445
2446 /*
2447 * ":let &option = expr": Set option value.
2448 * ":let &l:option = expr": Set local option value.
2449 * ":let &g:option = expr": Set global option value.
2450 */
2451 else if (*arg == '&')
2452 {
2453 /* Find the end of the name. */
2454 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 if (p == NULL || (endchars != NULL
2456 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 EMSG(_(e_letunexp));
2458 else
2459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 long n;
2461 int opt_type;
2462 long numval;
2463 char_u *stringval = NULL;
2464 char_u *s;
2465
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 c1 = *p;
2467 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468
2469 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 s = get_tv_string_chk(tv); /* != NULL if number or string */
2471 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 {
2473 opt_type = get_option_value(arg, &numval,
2474 &stringval, opt_flags);
2475 if ((opt_type == 1 && *op == '.')
2476 || (opt_type == 0 && *op != '.'))
2477 EMSG2(_(e_letwrong), op);
2478 else
2479 {
2480 if (opt_type == 1) /* number */
2481 {
2482 if (*op == '+')
2483 n = numval + n;
2484 else
2485 n = numval - n;
2486 }
2487 else if (opt_type == 0 && stringval != NULL) /* string */
2488 {
2489 s = concat_str(stringval, s);
2490 vim_free(stringval);
2491 stringval = s;
2492 }
2493 }
2494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 if (s != NULL)
2496 {
2497 set_option_value(arg, n, s, opt_flags);
2498 arg_end = p;
2499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 }
2503 }
2504
2505 /*
2506 * ":let @r = expr": Set register contents.
2507 */
2508 else if (*arg == '@')
2509 {
2510 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 if (op != NULL && (*op == '+' || *op == '-'))
2512 EMSG2(_(e_letwrong), op);
2513 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 char_u *s;
2520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 p = get_tv_string_chk(tv);
2522 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002524 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 if (s != NULL)
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 vim_free(s);
2529 }
2530 }
2531 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 arg_end = arg + 1;
2535 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
2538 }
2539
2540 /*
2541 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2552 EMSG(_(e_letunexp));
2553 else
2554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 arg_end = p;
2557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 }
2561
2562 else
2563 EMSG2(_(e_invarg2), arg);
2564
2565 return arg_end;
2566}
2567
2568/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2570 */
2571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573{
2574 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2575 {
2576 EMSG2(_(e_readonlyvar), arg);
2577 return TRUE;
2578 }
2579 return FALSE;
2580}
2581
2582/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 * Get an lval: variable, Dict item or List item that can be assigned a value
2584 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2585 * "name.key", "name.key[expr]" etc.
2586 * Indexing only works if "name" is an existing List or Dictionary.
2587 * "name" points to the start of the name.
2588 * If "rettv" is not NULL it points to the value to be assigned.
2589 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2590 * wrong; must end in space or cmd separator.
2591 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002592 * flags:
2593 * GLV_QUIET: do not give error messages
2594 * GLV_NO_AUTOLOAD: do not use script autoloading
2595 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002597 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 */
2600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002601get_lval(
2602 char_u *name,
2603 typval_T *rettv,
2604 lval_T *lp,
2605 int unlet,
2606 int skip,
2607 int flags, /* GLV_ values */
2608 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 char_u *expr_start, *expr_end;
2612 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 dictitem_T *v;
2614 typval_T var1;
2615 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002621 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625
2626 if (skip)
2627 {
2628 /* When skipping just find the end of the name. */
2629 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002630 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 }
2632
2633 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002634 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (expr_start != NULL)
2636 {
2637 /* Don't expand the name when we already know there is an error. */
2638 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2639 && *p != '[' && *p != '.')
2640 {
2641 EMSG(_(e_trailing));
2642 return NULL;
2643 }
2644
2645 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2646 if (lp->ll_exp_name == NULL)
2647 {
2648 /* Report an invalid expression in braces, unless the
2649 * expression evaluation has been cancelled due to an
2650 * aborting error, an interrupt, or an exception. */
2651 if (!aborting() && !quiet)
2652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002653 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 EMSG2(_(e_invarg2), name);
2655 return NULL;
2656 }
2657 }
2658 lp->ll_name = lp->ll_exp_name;
2659 }
2660 else
2661 lp->ll_name = name;
2662
2663 /* Without [idx] or .key we are done. */
2664 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2665 return p;
2666
2667 cc = *p;
2668 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002669 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (v == NULL && !quiet)
2671 EMSG2(_(e_undefvar), lp->ll_name);
2672 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 if (v == NULL)
2674 return NULL;
2675
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 /*
2677 * Loop until no more [idx] or .key is following.
2678 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2683 && !(lp->ll_tv->v_type == VAR_DICT
2684 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_("E689: Can only index a List or Dictionary"));
2688 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E708: [:] must come last"));
2694 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 len = -1;
2698 if (*p == '.')
2699 {
2700 key = p + 1;
2701 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2702 ;
2703 if (len == 0)
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_(e_emptykey));
2707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709 p = key + len;
2710 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 else
2712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (*p == ':')
2716 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 else
2718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 empty1 = FALSE;
2720 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var1) == NULL)
2723 {
2724 /* not a number or string */
2725 clear_tv(&var1);
2726 return NULL;
2727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
2729
2730 /* Optionally get the second index [ :expr]. */
2731 if (*p == ':')
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 if (!empty1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (rettv != NULL && (rettv->v_type != VAR_LIST
2742 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
2745 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750 p = skipwhite(p + 1);
2751 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (!empty1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 if (get_tv_string_chk(&var2) == NULL)
2763 {
2764 /* not a number or string */
2765 if (!empty1)
2766 clear_tv(&var1);
2767 clear_tv(&var2);
2768 return NULL;
2769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002775
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
2779 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (!empty1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 }
2786
2787 /* Skip to past ']'. */
2788 ++p;
2789 }
2790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
2793 if (len == -1)
2794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002796 key = get_tv_string_chk(&var1); /* is number or string */
2797 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 lp->ll_list = NULL;
2804 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002805 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002807 /* When assigning to a scope dictionary check that a function and
2808 * variable name is valid (only variable name unless it is l: or
2809 * g: dictionary). Disallow overwriting a builtin function. */
2810 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002812 int prevval;
2813 int wrong;
2814
2815 if (len != -1)
2816 {
2817 prevval = key[len];
2818 key[len] = NUL;
2819 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002820 else
2821 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2823 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002825 || !valid_varname(key);
2826 if (len != -1)
2827 key[len] = prevval;
2828 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 return NULL;
2830 }
2831
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* Can't add "v:" variable. */
2835 if (lp->ll_dict == &vimvardict)
2836 {
2837 EMSG2(_(e_illvar), name);
2838 return NULL;
2839 }
2840
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002841 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (len == -1)
2847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 if (len == -1)
2855 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 p = NULL;
2858 break;
2859 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002861 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002862 return NULL;
2863
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 if (len == -1)
2865 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868 else
2869 {
2870 /*
2871 * Get the number and item for the only or first index of the List.
2872 */
2873 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 else
2876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var1);
2879 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_list = lp->ll_tv->vval.v_list;
2882 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2883 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002885 if (lp->ll_n1 < 0)
2886 {
2887 lp->ll_n1 = 0;
2888 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2889 }
2890 }
2891 if (lp->ll_li == NULL)
2892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 if (!quiet)
2896 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 }
2899
2900 /*
2901 * May need to find the item or absolute index for the second
2902 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * When no index given: "lp->ll_empty2" is TRUE.
2904 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002908 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002914 {
2915 if (!quiet)
2916 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 }
2921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2923 if (lp->ll_n1 < 0)
2924 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2925 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002926 {
2927 if (!quiet)
2928 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002930 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 }
2932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 }
2936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 return p;
2938}
2939
2940/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 */
2943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945{
2946 vim_free(lp->ll_exp_name);
2947 vim_free(lp->ll_newkey);
2948}
2949
2950/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 */
2955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956set_var_lval(
2957 lval_T *lp,
2958 char_u *endp,
2959 typval_T *rettv,
2960 int copy,
2961 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962{
2963 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 listitem_T *ri;
2965 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966
2967 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 cc = *endp;
2972 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002975 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002977 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002978 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002979 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002980 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002982 if ((di == NULL
2983 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2984 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2985 FALSE)))
2986 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 set_var(lp->ll_name, &tv, FALSE);
2988 clear_tv(&tv);
2989 }
2990 }
2991 else
2992 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002996 else if (tv_check_lock(lp->ll_newkey == NULL
2997 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002998 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002999 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 else if (lp->ll_range)
3001 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003002 listitem_T *ll_li = lp->ll_li;
3003 int ll_n1 = lp->ll_n1;
3004
3005 /*
3006 * Check whether any of the list items is locked
3007 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003008 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003009 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003010 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 return;
3012 ri = ri->li_next;
3013 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3014 break;
3015 ll_li = ll_li->li_next;
3016 ++ll_n1;
3017 }
3018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 /*
3020 * Assign the List values to the list items.
3021 */
3022 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (op != NULL && *op != '=')
3025 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3026 else
3027 {
3028 clear_tv(&lp->ll_li->li_tv);
3029 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3030 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 ri = ri->li_next;
3032 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3033 break;
3034 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003037 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 ri = NULL;
3040 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003041 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 lp->ll_li = lp->ll_li->li_next;
3044 ++lp->ll_n1;
3045 }
3046 if (ri != NULL)
3047 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else if (lp->ll_empty2
3049 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 : lp->ll_n1 != lp->ll_n2)
3051 EMSG(_("E711: List value has not enough items"));
3052 }
3053 else
3054 {
3055 /*
3056 * Assign to a List or Dictionary item.
3057 */
3058 if (lp->ll_newkey != NULL)
3059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 if (op != NULL && *op != '=')
3061 {
3062 EMSG2(_(e_letwrong), op);
3063 return;
3064 }
3065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003067 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003068 if (di == NULL)
3069 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003070 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3071 {
3072 vim_free(di);
3073 return;
3074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003076 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 else if (op != NULL && *op != '=')
3078 {
3079 tv_op(lp->ll_tv, rettv, op);
3080 return;
3081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003084
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 /*
3086 * Assign the value to the variable or list item.
3087 */
3088 if (copy)
3089 copy_tv(rettv, lp->ll_tv);
3090 else
3091 {
3092 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003093 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 }
3096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097}
3098
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3101 * Returns OK or FAIL.
3102 */
3103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105{
3106 long n;
3107 char_u numbuf[NUMBUFLEN];
3108 char_u *s;
3109
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003110 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3111 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3112 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 {
3114 switch (tv1->v_type)
3115 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003116 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 case VAR_DICT:
3118 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003119 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003120 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003121 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003122 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 break;
3124
3125 case VAR_LIST:
3126 if (*op != '+' || tv2->v_type != VAR_LIST)
3127 break;
3128 /* List += List */
3129 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3130 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3131 return OK;
3132
3133 case VAR_NUMBER:
3134 case VAR_STRING:
3135 if (tv2->v_type == VAR_LIST)
3136 break;
3137 if (*op == '+' || *op == '-')
3138 {
3139 /* nr += nr or nr -= nr*/
3140 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141#ifdef FEAT_FLOAT
3142 if (tv2->v_type == VAR_FLOAT)
3143 {
3144 float_T f = n;
3145
3146 if (*op == '+')
3147 f += tv2->vval.v_float;
3148 else
3149 f -= tv2->vval.v_float;
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_FLOAT;
3152 tv1->vval.v_float = f;
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155#endif
3156 {
3157 if (*op == '+')
3158 n += get_tv_number(tv2);
3159 else
3160 n -= get_tv_number(tv2);
3161 clear_tv(tv1);
3162 tv1->v_type = VAR_NUMBER;
3163 tv1->vval.v_number = n;
3164 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003165 }
3166 else
3167 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168 if (tv2->v_type == VAR_FLOAT)
3169 break;
3170
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003171 /* str .= str */
3172 s = get_tv_string(tv1);
3173 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_STRING;
3176 tv1->vval.v_string = s;
3177 }
3178 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003181#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 {
3183 float_T f;
3184
3185 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3186 && tv2->v_type != VAR_NUMBER
3187 && tv2->v_type != VAR_STRING))
3188 break;
3189 if (tv2->v_type == VAR_FLOAT)
3190 f = tv2->vval.v_float;
3191 else
3192 f = get_tv_number(tv2);
3193 if (*op == '+')
3194 tv1->vval.v_float += f;
3195 else
3196 tv1->vval.v_float -= f;
3197 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003199 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003200 }
3201 }
3202
3203 EMSG2(_(e_letwrong), op);
3204 return FAIL;
3205}
3206
3207/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 * Add a watcher to a list.
3209 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
3213 lw->lw_next = l->lv_watch;
3214 l->lv_watch = lw;
3215}
3216
3217/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003218 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 * No warning when it isn't found...
3220 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
3226 lwp = &l->lv_watch;
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 {
3229 if (lw == lwrem)
3230 {
3231 *lwp = lw->lw_next;
3232 break;
3233 }
3234 lwp = &lw->lw_next;
3235 }
3236}
3237
3238/*
3239 * Just before removing an item from a list: advance watchers to the next
3240 * item.
3241 */
3242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3248 if (lw->lw_item == item)
3249 lw->lw_item = item->li_next;
3250}
3251
3252/*
3253 * Evaluate the expression used in a ":for var in expr" command.
3254 * "arg" points to "var".
3255 * Set "*errp" to TRUE for an error, FALSE otherwise;
3256 * Return a pointer that holds the info. Null when there is an error.
3257 */
3258 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259eval_for_line(
3260 char_u *arg,
3261 int *errp,
3262 char_u **nextcmdp,
3263 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003267 typval_T tv;
3268 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269
3270 *errp = TRUE; /* default: there is an error */
3271
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 if (fi == NULL)
3274 return NULL;
3275
3276 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3277 if (expr == NULL)
3278 return fi;
3279
3280 expr = skipwhite(expr);
3281 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3282 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003283 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 return fi;
3285 }
3286
3287 if (skip)
3288 ++emsg_skip;
3289 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3290 {
3291 *errp = FALSE;
3292 if (!skip)
3293 {
3294 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003295 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298 clear_tv(&tv);
3299 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003300 else if (l == NULL)
3301 {
3302 /* a null list is like an empty list: do nothing */
3303 clear_tv(&tv);
3304 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 else
3306 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003307 /* No need to increment the refcount, it's already set for the
3308 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 fi->fi_list = l;
3310 list_add_watch(l, &fi->fi_lw);
3311 fi->fi_lw.lw_item = l->lv_first;
3312 }
3313 }
3314 }
3315 if (skip)
3316 --emsg_skip;
3317
3318 return fi;
3319}
3320
3321/*
3322 * Use the first item in a ":for" list. Advance to the next.
3323 * Assign the values to the variable (list). "arg" points to the first one.
3324 * Return TRUE when a valid item was found, FALSE when at end of list or
3325 * something wrong.
3326 */
3327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003330 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003332 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333
3334 item = fi->fi_lw.lw_item;
3335 if (item == NULL)
3336 result = FALSE;
3337 else
3338 {
3339 fi->fi_lw.lw_item = item->li_next;
3340 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3341 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3342 }
3343 return result;
3344}
3345
3346/*
3347 * Free the structure used to store info used by ":for".
3348 */
3349 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003350free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351{
Bram Moolenaar33570922005-01-25 22:26:29 +00003352 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003353
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003354 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003355 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003357 list_unref(fi->fi_list);
3358 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 vim_free(fi);
3360}
3361
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3363
3364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003365set_context_for_expression(
3366 expand_T *xp,
3367 char_u *arg,
3368 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369{
3370 int got_eq = FALSE;
3371 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003372 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003374 if (cmdidx == CMD_let)
3375 {
3376 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003377 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003378 {
3379 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003380 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 {
3382 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003383 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 if (vim_iswhite(*p))
3385 break;
3386 }
3387 return;
3388 }
3389 }
3390 else
3391 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3392 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 while ((xp->xp_pattern = vim_strpbrk(arg,
3394 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3395 {
3396 c = *xp->xp_pattern;
3397 if (c == '&')
3398 {
3399 c = xp->xp_pattern[1];
3400 if (c == '&')
3401 {
3402 ++xp->xp_pattern;
3403 xp->xp_context = cmdidx != CMD_let || got_eq
3404 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3405 }
3406 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003409 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3410 xp->xp_pattern += 2;
3411
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 }
3414 else if (c == '$')
3415 {
3416 /* environment variable */
3417 xp->xp_context = EXPAND_ENV_VARS;
3418 }
3419 else if (c == '=')
3420 {
3421 got_eq = TRUE;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003424 else if (c == '#'
3425 && xp->xp_context == EXPAND_EXPRESSION)
3426 {
3427 /* Autoload function/variable contains '#'. */
3428 break;
3429 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003430 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 && xp->xp_context == EXPAND_FUNCTIONS
3432 && vim_strchr(xp->xp_pattern, '(') == NULL)
3433 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003434 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 break;
3436 }
3437 else if (cmdidx != CMD_let || got_eq)
3438 {
3439 if (c == '"') /* string */
3440 {
3441 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3442 if (c == '\\' && xp->xp_pattern[1] != NUL)
3443 ++xp->xp_pattern;
3444 xp->xp_context = EXPAND_NOTHING;
3445 }
3446 else if (c == '\'') /* literal string */
3447 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003448 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3450 /* skip */ ;
3451 xp->xp_context = EXPAND_NOTHING;
3452 }
3453 else if (c == '|')
3454 {
3455 if (xp->xp_pattern[1] == '|')
3456 {
3457 ++xp->xp_pattern;
3458 xp->xp_context = EXPAND_EXPRESSION;
3459 }
3460 else
3461 xp->xp_context = EXPAND_COMMANDS;
3462 }
3463 else
3464 xp->xp_context = EXPAND_EXPRESSION;
3465 }
3466 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003467 /* Doesn't look like something valid, expand as an expression
3468 * anyway. */
3469 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 arg = xp->xp_pattern;
3471 if (*arg != NUL)
3472 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3473 /* skip */ ;
3474 }
3475 xp->xp_pattern = arg;
3476}
3477
3478#endif /* FEAT_CMDL_COMPL */
3479
3480/*
3481 * ":1,25call func(arg1, arg2)" function call.
3482 */
3483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485{
3486 char_u *arg = eap->arg;
3487 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003489 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003491 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 linenr_T lnum;
3493 int doesrange;
3494 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003495 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003496 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003498 if (eap->skip)
3499 {
3500 /* trans_function_name() doesn't work well when skipping, use eval0()
3501 * instead to skip to any following command, e.g. for:
3502 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003503 ++emsg_skip;
3504 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3505 clear_tv(&rettv);
3506 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 return;
3508 }
3509
Bram Moolenaar65639032016-03-16 21:40:30 +01003510 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003511 if (fudi.fd_newkey != NULL)
3512 {
3513 /* Still need to give an error message for missing key. */
3514 EMSG2(_(e_dictkey), fudi.fd_newkey);
3515 vim_free(fudi.fd_newkey);
3516 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003517 if (tofree == NULL)
3518 return;
3519
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003520 /* Increase refcount on dictionary, it could get deleted when evaluating
3521 * the arguments. */
3522 if (fudi.fd_dict != NULL)
3523 ++fudi.fd_dict->dv_refcount;
3524
Bram Moolenaar65639032016-03-16 21:40:30 +01003525 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3526 * contents. For VAR_PARTIAL get its partial, unless we already have one
3527 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003528 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003529 name = deref_func_name(tofree, &len,
3530 partial != NULL ? NULL : &partial, FALSE);
3531
Bram Moolenaar532c7802005-01-27 14:44:31 +00003532 /* Skip white space to allow ":call func ()". Not good, but required for
3533 * backward compatibility. */
3534 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003535 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 if (*startarg != '(')
3538 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003539 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 goto end;
3541 }
3542
3543 /*
3544 * When skipping, evaluate the function once, to find the end of the
3545 * arguments.
3546 * When the function takes a range, this is discovered after the first
3547 * call, and the loop is broken.
3548 */
3549 if (eap->skip)
3550 {
3551 ++emsg_skip;
3552 lnum = eap->line2; /* do it once, also with an invalid range */
3553 }
3554 else
3555 lnum = eap->line1;
3556 for ( ; lnum <= eap->line2; ++lnum)
3557 {
3558 if (!eap->skip && eap->addr_count > 0)
3559 {
3560 curwin->w_cursor.lnum = lnum;
3561 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003562#ifdef FEAT_VIRTUALEDIT
3563 curwin->w_cursor.coladd = 0;
3564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003567 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003568 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003569 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 {
3571 failed = TRUE;
3572 break;
3573 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003574
3575 /* Handle a function returning a Funcref, Dictionary or List. */
3576 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3577 {
3578 failed = TRUE;
3579 break;
3580 }
3581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003582 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 if (doesrange || eap->skip)
3584 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003585
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003587 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003588 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003589 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 if (aborting())
3591 break;
3592 }
3593 if (eap->skip)
3594 --emsg_skip;
3595
3596 if (!failed)
3597 {
3598 /* Check for trailing illegal characters and a following command. */
3599 if (!ends_excmd(*arg))
3600 {
3601 emsg_severe = TRUE;
3602 EMSG(_(e_trailing));
3603 }
3604 else
3605 eap->nextcmd = check_nextcmd(arg);
3606 }
3607
3608end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003609 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003610 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611}
3612
3613/*
3614 * ":unlet[!] var1 ... " command.
3615 */
3616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 ex_unletlock(eap, eap->arg, 0);
3620}
3621
3622/*
3623 * ":lockvar" and ":unlockvar" commands
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003627{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 int deep = 2;
3630
3631 if (eap->forceit)
3632 deep = -1;
3633 else if (vim_isdigit(*arg))
3634 {
3635 deep = getdigits(&arg);
3636 arg = skipwhite(arg);
3637 }
3638
3639 ex_unletlock(eap, arg, deep);
3640}
3641
3642/*
3643 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3644 */
3645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003646ex_unletlock(
3647 exarg_T *eap,
3648 char_u *argstart,
3649 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650{
3651 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655
3656 do
3657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003658 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003659 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003660 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (lv.ll_name == NULL)
3662 error = TRUE; /* error but continue parsing */
3663 if (name_end == NULL || (!vim_iswhite(*name_end)
3664 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 if (name_end != NULL)
3667 {
3668 emsg_severe = TRUE;
3669 EMSG(_(e_trailing));
3670 }
3671 if (!(eap->skip || error))
3672 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 break;
3674 }
3675
3676 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 {
3678 if (eap->cmdidx == CMD_unlet)
3679 {
3680 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3681 error = TRUE;
3682 }
3683 else
3684 {
3685 if (do_lock_var(&lv, name_end, deep,
3686 eap->cmdidx == CMD_lockvar) == FAIL)
3687 error = TRUE;
3688 }
3689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003691 if (!eap->skip)
3692 clear_lval(&lv);
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 arg = skipwhite(name_end);
3695 } while (!ends_excmd(*arg));
3696
3697 eap->nextcmd = check_nextcmd(arg);
3698}
3699
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003701do_unlet_var(
3702 lval_T *lp,
3703 char_u *name_end,
3704 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 int ret = OK;
3707 int cc;
3708
3709 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711 cc = *name_end;
3712 *name_end = NUL;
3713
3714 /* Normal name or expanded name. */
3715 if (check_changedtick(lp->ll_name))
3716 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003717 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003721 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003722 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003723 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003724 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 else if (lp->ll_range)
3727 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003728 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003729 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003730 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003731
3732 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3733 {
3734 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003735 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003736 return FAIL;
3737 ll_li = li;
3738 ++ll_n1;
3739 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003740
3741 /* Delete a range of List items. */
3742 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3743 {
3744 li = lp->ll_li->li_next;
3745 listitem_remove(lp->ll_list, lp->ll_li);
3746 lp->ll_li = li;
3747 ++lp->ll_n1;
3748 }
3749 }
3750 else
3751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003752 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003753 /* unlet a List item. */
3754 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003755 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003756 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003757 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003758 }
3759
3760 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003761}
3762
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763/*
3764 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
3767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003768do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769{
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hashtab_T *ht;
3771 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003772 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003773 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003774 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaar33570922005-01-25 22:26:29 +00003776 ht = find_var_ht(name, &varname);
3777 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003779 if (ht == &globvarht)
3780 d = &globvardict;
3781 else if (current_funccal != NULL
3782 && ht == &current_funccal->l_vars.dv_hashtab)
3783 d = &current_funccal->l_vars;
3784 else if (ht == &compat_hashtab)
3785 d = &vimvardict;
3786 else
3787 {
3788 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3789 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3790 }
3791 if (d == NULL)
3792 {
3793 EMSG2(_(e_intern2), "do_unlet()");
3794 return FAIL;
3795 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003796 hi = hash_find(ht, varname);
3797 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003798 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003799 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003800 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003801 || var_check_ro(di->di_flags, name, FALSE)
3802 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003803 return FAIL;
3804
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 delete_var(ht, hi);
3806 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003809 if (forceit)
3810 return OK;
3811 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 return FAIL;
3813}
3814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815/*
3816 * Lock or unlock variable indicated by "lp".
3817 * "deep" is the levels to go (-1 for unlimited);
3818 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3819 */
3820 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003821do_lock_var(
3822 lval_T *lp,
3823 char_u *name_end,
3824 int deep,
3825 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003826{
3827 int ret = OK;
3828 int cc;
3829 dictitem_T *di;
3830
3831 if (deep == 0) /* nothing to do */
3832 return OK;
3833
3834 if (lp->ll_tv == NULL)
3835 {
3836 cc = *name_end;
3837 *name_end = NUL;
3838
3839 /* Normal name or expanded name. */
3840 if (check_changedtick(lp->ll_name))
3841 ret = FAIL;
3842 else
3843 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003844 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845 if (di == NULL)
3846 ret = FAIL;
3847 else
3848 {
3849 if (lock)
3850 di->di_flags |= DI_FLAGS_LOCK;
3851 else
3852 di->di_flags &= ~DI_FLAGS_LOCK;
3853 item_lock(&di->di_tv, deep, lock);
3854 }
3855 }
3856 *name_end = cc;
3857 }
3858 else if (lp->ll_range)
3859 {
3860 listitem_T *li = lp->ll_li;
3861
3862 /* (un)lock a range of List items. */
3863 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3864 {
3865 item_lock(&li->li_tv, deep, lock);
3866 li = li->li_next;
3867 ++lp->ll_n1;
3868 }
3869 }
3870 else if (lp->ll_list != NULL)
3871 /* (un)lock a List item. */
3872 item_lock(&lp->ll_li->li_tv, deep, lock);
3873 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003874 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003875 item_lock(&lp->ll_di->di_tv, deep, lock);
3876
3877 return ret;
3878}
3879
3880/*
3881 * Lock or unlock an item. "deep" is nr of levels to go.
3882 */
3883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003884item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003885{
3886 static int recurse = 0;
3887 list_T *l;
3888 listitem_T *li;
3889 dict_T *d;
3890 hashitem_T *hi;
3891 int todo;
3892
3893 if (recurse >= DICT_MAXNEST)
3894 {
3895 EMSG(_("E743: variable nested too deep for (un)lock"));
3896 return;
3897 }
3898 if (deep == 0)
3899 return;
3900 ++recurse;
3901
3902 /* lock/unlock the item itself */
3903 if (lock)
3904 tv->v_lock |= VAR_LOCKED;
3905 else
3906 tv->v_lock &= ~VAR_LOCKED;
3907
3908 switch (tv->v_type)
3909 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003910 case VAR_UNKNOWN:
3911 case VAR_NUMBER:
3912 case VAR_STRING:
3913 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003914 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003915 case VAR_FLOAT:
3916 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003917 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003918 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 break;
3920
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003921 case VAR_LIST:
3922 if ((l = tv->vval.v_list) != NULL)
3923 {
3924 if (lock)
3925 l->lv_lock |= VAR_LOCKED;
3926 else
3927 l->lv_lock &= ~VAR_LOCKED;
3928 if (deep < 0 || deep > 1)
3929 /* recursive: lock/unlock the items the List contains */
3930 for (li = l->lv_first; li != NULL; li = li->li_next)
3931 item_lock(&li->li_tv, deep - 1, lock);
3932 }
3933 break;
3934 case VAR_DICT:
3935 if ((d = tv->vval.v_dict) != NULL)
3936 {
3937 if (lock)
3938 d->dv_lock |= VAR_LOCKED;
3939 else
3940 d->dv_lock &= ~VAR_LOCKED;
3941 if (deep < 0 || deep > 1)
3942 {
3943 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003944 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003945 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3946 {
3947 if (!HASHITEM_EMPTY(hi))
3948 {
3949 --todo;
3950 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3951 }
3952 }
3953 }
3954 }
3955 }
3956 --recurse;
3957}
3958
Bram Moolenaara40058a2005-07-11 22:42:07 +00003959/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003960 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3961 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003962 */
3963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003965{
3966 return (tv->v_lock & VAR_LOCKED)
3967 || (tv->v_type == VAR_LIST
3968 && tv->vval.v_list != NULL
3969 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3970 || (tv->v_type == VAR_DICT
3971 && tv->vval.v_dict != NULL
3972 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3973}
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3976/*
3977 * Delete all "menutrans_" variables.
3978 */
3979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003980del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981{
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003986 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003988 {
3989 if (!HASHITEM_EMPTY(hi))
3990 {
3991 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3993 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003994 }
3995 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998#endif
3999
4000#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4001
4002/*
4003 * Local string buffer for the next two functions to store a variable name
4004 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4005 * get_user_var_name().
4006 */
4007
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004008static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010static char_u *varnamebuf = NULL;
4011static int varnamebuflen = 0;
4012
4013/*
4014 * Function to concatenate a prefix and a variable name.
4015 */
4016 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004017cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
4019 int len;
4020
4021 len = (int)STRLEN(name) + 3;
4022 if (len > varnamebuflen)
4023 {
4024 vim_free(varnamebuf);
4025 len += 10; /* some additional space */
4026 varnamebuf = alloc(len);
4027 if (varnamebuf == NULL)
4028 {
4029 varnamebuflen = 0;
4030 return NULL;
4031 }
4032 varnamebuflen = len;
4033 }
4034 *varnamebuf = prefix;
4035 varnamebuf[1] = ':';
4036 STRCPY(varnamebuf + 2, name);
4037 return varnamebuf;
4038}
4039
4040/*
4041 * Function given to ExpandGeneric() to obtain the list of user defined
4042 * (global/buffer/window/built-in) variable names.
4043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004045get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004047 static long_u gdone;
4048 static long_u bdone;
4049 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004050#ifdef FEAT_WINDOWS
4051 static long_u tdone;
4052#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004053 static int vidx;
4054 static hashitem_T *hi;
4055 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004058 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004059 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 tdone = 0;
4062#endif
4063 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004064
4065 /* Global variables */
4066 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004070 else
4071 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004072 while (HASHITEM_EMPTY(hi))
4073 ++hi;
4074 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4075 return cat_prefix_varname('g', hi->hi_key);
4076 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004078
4079 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004080 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004081 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004085 else
4086 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004087 while (HASHITEM_EMPTY(hi))
4088 ++hi;
4089 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004093 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 return (char_u *)"b:changedtick";
4095 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004096
4097 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004098 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004101 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004102 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004103 else
4104 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004105 while (HASHITEM_EMPTY(hi))
4106 ++hi;
4107 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004109
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004110#ifdef FEAT_WINDOWS
4111 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004112 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004113 if (tdone < ht->ht_used)
4114 {
4115 if (tdone++ == 0)
4116 hi = ht->ht_array;
4117 else
4118 ++hi;
4119 while (HASHITEM_EMPTY(hi))
4120 ++hi;
4121 return cat_prefix_varname('t', hi->hi_key);
4122 }
4123#endif
4124
Bram Moolenaar33570922005-01-25 22:26:29 +00004125 /* v: variables */
4126 if (vidx < VV_LEN)
4127 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 vim_free(varnamebuf);
4130 varnamebuf = NULL;
4131 varnamebuflen = 0;
4132 return NULL;
4133}
4134
4135#endif /* FEAT_CMDL_COMPL */
4136
4137/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004138 * Return TRUE if "pat" matches "text".
4139 * Does not use 'cpo' and always uses 'magic'.
4140 */
4141 static int
4142pattern_match(char_u *pat, char_u *text, int ic)
4143{
4144 int matches = FALSE;
4145 char_u *save_cpo;
4146 regmatch_T regmatch;
4147
4148 /* avoid 'l' flag in 'cpoptions' */
4149 save_cpo = p_cpo;
4150 p_cpo = (char_u *)"";
4151 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4152 if (regmatch.regprog != NULL)
4153 {
4154 regmatch.rm_ic = ic;
4155 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4156 vim_regfree(regmatch.regprog);
4157 }
4158 p_cpo = save_cpo;
4159 return matches;
4160}
4161
4162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * types for expressions.
4164 */
4165typedef enum
4166{
4167 TYPE_UNKNOWN = 0
4168 , TYPE_EQUAL /* == */
4169 , TYPE_NEQUAL /* != */
4170 , TYPE_GREATER /* > */
4171 , TYPE_GEQUAL /* >= */
4172 , TYPE_SMALLER /* < */
4173 , TYPE_SEQUAL /* <= */
4174 , TYPE_MATCH /* =~ */
4175 , TYPE_NOMATCH /* !~ */
4176} exptype_T;
4177
4178/*
4179 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4182 */
4183
4184/*
4185 * Handle zero level expression.
4186 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004187 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004188 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 * Return OK or FAIL.
4190 */
4191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004192eval0(
4193 char_u *arg,
4194 typval_T *rettv,
4195 char_u **nextcmd,
4196 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198 int ret;
4199 char_u *p;
4200
4201 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004202 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (ret == FAIL || !ends_excmd(*p))
4204 {
4205 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 /*
4208 * Report the invalid expression unless the expression evaluation has
4209 * been cancelled due to an aborting error, an interrupt, or an
4210 * exception.
4211 */
4212 if (!aborting())
4213 EMSG2(_(e_invexpr2), arg);
4214 ret = FAIL;
4215 }
4216 if (nextcmd != NULL)
4217 *nextcmd = check_nextcmd(p);
4218
4219 return ret;
4220}
4221
4222/*
4223 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004224 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 *
4226 * "arg" must point to the first non-white of the expression.
4227 * "arg" is advanced to the next non-white after the recognized expression.
4228 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004229 * Note: "rettv.v_lock" is not set.
4230 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 * Return OK or FAIL.
4232 */
4233 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004234eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
4236 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004237 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238
4239 /*
4240 * Get the first variable.
4241 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004242 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 return FAIL;
4244
4245 if ((*arg)[0] == '?')
4246 {
4247 result = FALSE;
4248 if (evaluate)
4249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 int error = FALSE;
4251
4252 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 if (error)
4256 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258
4259 /*
4260 * Get the second variable.
4261 */
4262 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265
4266 /*
4267 * Check for the ":".
4268 */
4269 if ((*arg)[0] != ':')
4270 {
4271 EMSG(_("E109: Missing ':' after '?'"));
4272 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275 }
4276
4277 /*
4278 * Get the third variable.
4279 */
4280 *arg = skipwhite(*arg + 1);
4281 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4282 {
4283 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004284 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 return FAIL;
4286 }
4287 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 return OK;
4292}
4293
4294/*
4295 * Handle first level expression:
4296 * expr2 || expr2 || expr2 logical OR
4297 *
4298 * "arg" must point to the first non-white of the expression.
4299 * "arg" is advanced to the next non-white after the recognized expression.
4300 *
4301 * Return OK or FAIL.
4302 */
4303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004304eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaar33570922005-01-25 22:26:29 +00004306 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 long result;
4308 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004309 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 /*
4318 * Repeat until there is no following "||".
4319 */
4320 first = TRUE;
4321 result = FALSE;
4322 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4323 {
4324 if (evaluate && first)
4325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004329 if (error)
4330 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 first = FALSE;
4332 }
4333
4334 /*
4335 * Get the second variable.
4336 */
4337 *arg = skipwhite(*arg + 2);
4338 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4339 return FAIL;
4340
4341 /*
4342 * Compute the result.
4343 */
4344 if (evaluate && !result)
4345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004348 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 if (error)
4350 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 if (evaluate)
4353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004354 rettv->v_type = VAR_NUMBER;
4355 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 }
4358
4359 return OK;
4360}
4361
4362/*
4363 * Handle second level expression:
4364 * expr3 && expr3 && expr3 logical AND
4365 *
4366 * "arg" must point to the first non-white of the expression.
4367 * "arg" is advanced to the next non-white after the recognized expression.
4368 *
4369 * Return OK or FAIL.
4370 */
4371 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004372eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
Bram Moolenaar33570922005-01-25 22:26:29 +00004374 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 long result;
4376 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004377 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 /*
4380 * Get the first variable.
4381 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
4384
4385 /*
4386 * Repeat until there is no following "&&".
4387 */
4388 first = TRUE;
4389 result = TRUE;
4390 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4391 {
4392 if (evaluate && first)
4393 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004394 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004396 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004397 if (error)
4398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 first = FALSE;
4400 }
4401
4402 /*
4403 * Get the second variable.
4404 */
4405 *arg = skipwhite(*arg + 2);
4406 if (eval4(arg, &var2, evaluate && result) == FAIL)
4407 return FAIL;
4408
4409 /*
4410 * Compute the result.
4411 */
4412 if (evaluate && result)
4413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004414 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004417 if (error)
4418 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 if (evaluate)
4421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 }
4426
4427 return OK;
4428}
4429
4430/*
4431 * Handle third level expression:
4432 * var1 == var2
4433 * var1 =~ var2
4434 * var1 != var2
4435 * var1 !~ var2
4436 * var1 > var2
4437 * var1 >= var2
4438 * var1 < var2
4439 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004440 * var1 is var2
4441 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 *
4443 * "arg" must point to the first non-white of the expression.
4444 * "arg" is advanced to the next non-white after the recognized expression.
4445 *
4446 * Return OK or FAIL.
4447 */
4448 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004449eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450{
Bram Moolenaar33570922005-01-25 22:26:29 +00004451 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 char_u *p;
4453 int i;
4454 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004455 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 int len = 2;
4457 long n1, n2;
4458 char_u *s1, *s2;
4459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 /*
4463 * Get the first variable.
4464 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004465 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 return FAIL;
4467
4468 p = *arg;
4469 switch (p[0])
4470 {
4471 case '=': if (p[1] == '=')
4472 type = TYPE_EQUAL;
4473 else if (p[1] == '~')
4474 type = TYPE_MATCH;
4475 break;
4476 case '!': if (p[1] == '=')
4477 type = TYPE_NEQUAL;
4478 else if (p[1] == '~')
4479 type = TYPE_NOMATCH;
4480 break;
4481 case '>': if (p[1] != '=')
4482 {
4483 type = TYPE_GREATER;
4484 len = 1;
4485 }
4486 else
4487 type = TYPE_GEQUAL;
4488 break;
4489 case '<': if (p[1] != '=')
4490 {
4491 type = TYPE_SMALLER;
4492 len = 1;
4493 }
4494 else
4495 type = TYPE_SEQUAL;
4496 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004497 case 'i': if (p[1] == 's')
4498 {
4499 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4500 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004501 i = p[len];
4502 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004503 {
4504 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4505 type_is = TRUE;
4506 }
4507 }
4508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510
4511 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 */
4514 if (type != TYPE_UNKNOWN)
4515 {
4516 /* extra question mark appended: ignore case */
4517 if (p[len] == '?')
4518 {
4519 ic = TRUE;
4520 ++len;
4521 }
4522 /* extra '#' appended: match case */
4523 else if (p[len] == '#')
4524 {
4525 ic = FALSE;
4526 ++len;
4527 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004528 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 else
4530 ic = p_ic;
4531
4532 /*
4533 * Get the second variable.
4534 */
4535 *arg = skipwhite(p + len);
4536 if (eval5(arg, &var2, evaluate) == FAIL)
4537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004538 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 return FAIL;
4540 }
4541
4542 if (evaluate)
4543 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 if (type_is && rettv->v_type != var2.v_type)
4545 {
4546 /* For "is" a different type always means FALSE, for "notis"
4547 * it means TRUE. */
4548 n1 = (type == TYPE_NEQUAL);
4549 }
4550 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4551 {
4552 if (type_is)
4553 {
4554 n1 = (rettv->v_type == var2.v_type
4555 && rettv->vval.v_list == var2.vval.v_list);
4556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 else if (rettv->v_type != var2.v_type
4560 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4561 {
4562 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004563 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004565 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004566 clear_tv(rettv);
4567 clear_tv(&var2);
4568 return FAIL;
4569 }
4570 else
4571 {
4572 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004573 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4574 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 if (type == TYPE_NEQUAL)
4576 n1 = !n1;
4577 }
4578 }
4579
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004580 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4581 {
4582 if (type_is)
4583 {
4584 n1 = (rettv->v_type == var2.v_type
4585 && rettv->vval.v_dict == var2.vval.v_dict);
4586 if (type == TYPE_NEQUAL)
4587 n1 = !n1;
4588 }
4589 else if (rettv->v_type != var2.v_type
4590 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4591 {
4592 if (rettv->v_type != var2.v_type)
4593 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4594 else
4595 EMSG(_("E736: Invalid operation for Dictionary"));
4596 clear_tv(rettv);
4597 clear_tv(&var2);
4598 return FAIL;
4599 }
4600 else
4601 {
4602 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004603 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4604 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004605 if (type == TYPE_NEQUAL)
4606 n1 = !n1;
4607 }
4608 }
4609
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004610 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4611 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004612 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004613 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004614 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004615 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004616 clear_tv(rettv);
4617 clear_tv(&var2);
4618 return FAIL;
4619 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004620 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004621 if (type == TYPE_NEQUAL)
4622 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 }
4624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004625#ifdef FEAT_FLOAT
4626 /*
4627 * If one of the two variables is a float, compare as a float.
4628 * When using "=~" or "!~", always compare as string.
4629 */
4630 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4631 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4632 {
4633 float_T f1, f2;
4634
4635 if (rettv->v_type == VAR_FLOAT)
4636 f1 = rettv->vval.v_float;
4637 else
4638 f1 = get_tv_number(rettv);
4639 if (var2.v_type == VAR_FLOAT)
4640 f2 = var2.vval.v_float;
4641 else
4642 f2 = get_tv_number(&var2);
4643 n1 = FALSE;
4644 switch (type)
4645 {
4646 case TYPE_EQUAL: n1 = (f1 == f2); break;
4647 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4648 case TYPE_GREATER: n1 = (f1 > f2); break;
4649 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4650 case TYPE_SMALLER: n1 = (f1 < f2); break;
4651 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4652 case TYPE_UNKNOWN:
4653 case TYPE_MATCH:
4654 case TYPE_NOMATCH: break; /* avoid gcc warning */
4655 }
4656 }
4657#endif
4658
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 /*
4660 * If one of the two variables is a number, compare as a number.
4661 * When using "=~" or "!~", always compare as string.
4662 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004663 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004666 n1 = get_tv_number(rettv);
4667 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 switch (type)
4669 {
4670 case TYPE_EQUAL: n1 = (n1 == n2); break;
4671 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4672 case TYPE_GREATER: n1 = (n1 > n2); break;
4673 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4674 case TYPE_SMALLER: n1 = (n1 < n2); break;
4675 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4676 case TYPE_UNKNOWN:
4677 case TYPE_MATCH:
4678 case TYPE_NOMATCH: break; /* avoid gcc warning */
4679 }
4680 }
4681 else
4682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004683 s1 = get_tv_string_buf(rettv, buf1);
4684 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4686 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4687 else
4688 i = 0;
4689 n1 = FALSE;
4690 switch (type)
4691 {
4692 case TYPE_EQUAL: n1 = (i == 0); break;
4693 case TYPE_NEQUAL: n1 = (i != 0); break;
4694 case TYPE_GREATER: n1 = (i > 0); break;
4695 case TYPE_GEQUAL: n1 = (i >= 0); break;
4696 case TYPE_SMALLER: n1 = (i < 0); break;
4697 case TYPE_SEQUAL: n1 = (i <= 0); break;
4698
4699 case TYPE_MATCH:
4700 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004701 n1 = pattern_match(s2, s1, ic);
4702 if (type == TYPE_NOMATCH)
4703 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 break;
4705
4706 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4707 }
4708 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004709 clear_tv(rettv);
4710 clear_tv(&var2);
4711 rettv->v_type = VAR_NUMBER;
4712 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
4714 }
4715
4716 return OK;
4717}
4718
4719/*
4720 * Handle fourth level expression:
4721 * + number addition
4722 * - number subtraction
4723 * . string concatenation
4724 *
4725 * "arg" must point to the first non-white of the expression.
4726 * "arg" is advanced to the next non-white after the recognized expression.
4727 *
4728 * Return OK or FAIL.
4729 */
4730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004731eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
Bram Moolenaar33570922005-01-25 22:26:29 +00004733 typval_T var2;
4734 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 int op;
4736 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737#ifdef FEAT_FLOAT
4738 float_T f1 = 0, f2 = 0;
4739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 char_u *s1, *s2;
4741 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4742 char_u *p;
4743
4744 /*
4745 * Get the first variable.
4746 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004747 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 return FAIL;
4749
4750 /*
4751 * Repeat computing, until no '+', '-' or '.' is following.
4752 */
4753 for (;;)
4754 {
4755 op = **arg;
4756 if (op != '+' && op != '-' && op != '.')
4757 break;
4758
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759 if ((op != '+' || rettv->v_type != VAR_LIST)
4760#ifdef FEAT_FLOAT
4761 && (op == '.' || rettv->v_type != VAR_FLOAT)
4762#endif
4763 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004764 {
4765 /* For "list + ...", an illegal use of the first operand as
4766 * a number cannot be determined before evaluating the 2nd
4767 * operand: if this is also a list, all is ok.
4768 * For "something . ...", "something - ..." or "non-list + ...",
4769 * we know that the first operand needs to be a string or number
4770 * without evaluating the 2nd operand. So check before to avoid
4771 * side effects after an error. */
4772 if (evaluate && get_tv_string_chk(rettv) == NULL)
4773 {
4774 clear_tv(rettv);
4775 return FAIL;
4776 }
4777 }
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 /*
4780 * Get the second variable.
4781 */
4782 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004783 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 return FAIL;
4787 }
4788
4789 if (evaluate)
4790 {
4791 /*
4792 * Compute the result.
4793 */
4794 if (op == '.')
4795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4797 s2 = get_tv_string_buf_chk(&var2, buf2);
4798 if (s2 == NULL) /* type error ? */
4799 {
4800 clear_tv(rettv);
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004804 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004805 clear_tv(rettv);
4806 rettv->v_type = VAR_STRING;
4807 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004809 else if (op == '+' && rettv->v_type == VAR_LIST
4810 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004811 {
4812 /* concatenate Lists */
4813 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4814 &var3) == FAIL)
4815 {
4816 clear_tv(rettv);
4817 clear_tv(&var2);
4818 return FAIL;
4819 }
4820 clear_tv(rettv);
4821 *rettv = var3;
4822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 else
4824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 int error = FALSE;
4826
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004827#ifdef FEAT_FLOAT
4828 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004830 f1 = rettv->vval.v_float;
4831 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004832 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004833 else
4834#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836 n1 = get_tv_number_chk(rettv, &error);
4837 if (error)
4838 {
4839 /* This can only happen for "list + non-list". For
4840 * "non-list + ..." or "something - ...", we returned
4841 * before evaluating the 2nd operand. */
4842 clear_tv(rettv);
4843 return FAIL;
4844 }
4845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 f1 = n1;
4848#endif
4849 }
4850#ifdef FEAT_FLOAT
4851 if (var2.v_type == VAR_FLOAT)
4852 {
4853 f2 = var2.vval.v_float;
4854 n2 = 0;
4855 }
4856 else
4857#endif
4858 {
4859 n2 = get_tv_number_chk(&var2, &error);
4860 if (error)
4861 {
4862 clear_tv(rettv);
4863 clear_tv(&var2);
4864 return FAIL;
4865 }
4866#ifdef FEAT_FLOAT
4867 if (rettv->v_type == VAR_FLOAT)
4868 f2 = n2;
4869#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004871 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872
4873#ifdef FEAT_FLOAT
4874 /* If there is a float on either side the result is a float. */
4875 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4876 {
4877 if (op == '+')
4878 f1 = f1 + f2;
4879 else
4880 f1 = f1 - f2;
4881 rettv->v_type = VAR_FLOAT;
4882 rettv->vval.v_float = f1;
4883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004885#endif
4886 {
4887 if (op == '+')
4888 n1 = n1 + n2;
4889 else
4890 n1 = n1 - n2;
4891 rettv->v_type = VAR_NUMBER;
4892 rettv->vval.v_number = n1;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 }
4897 }
4898 return OK;
4899}
4900
4901/*
4902 * Handle fifth level expression:
4903 * * number multiplication
4904 * / number division
4905 * % number modulo
4906 *
4907 * "arg" must point to the first non-white of the expression.
4908 * "arg" is advanced to the next non-white after the recognized expression.
4909 *
4910 * Return OK or FAIL.
4911 */
4912 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004913eval6(
4914 char_u **arg,
4915 typval_T *rettv,
4916 int evaluate,
4917 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
Bram Moolenaar33570922005-01-25 22:26:29 +00004919 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 int op;
4921 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004922#ifdef FEAT_FLOAT
4923 int use_float = FALSE;
4924 float_T f1 = 0, f2;
4925#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004926 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 /*
4929 * Get the first variable.
4930 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004931 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return FAIL;
4933
4934 /*
4935 * Repeat computing, until no '*', '/' or '%' is following.
4936 */
4937 for (;;)
4938 {
4939 op = **arg;
4940 if (op != '*' && op != '/' && op != '%')
4941 break;
4942
4943 if (evaluate)
4944 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004945#ifdef FEAT_FLOAT
4946 if (rettv->v_type == VAR_FLOAT)
4947 {
4948 f1 = rettv->vval.v_float;
4949 use_float = TRUE;
4950 n1 = 0;
4951 }
4952 else
4953#endif
4954 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004956 if (error)
4957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 else
4960 n1 = 0;
4961
4962 /*
4963 * Get the second variable.
4964 */
4965 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004966 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 return FAIL;
4968
4969 if (evaluate)
4970 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004971#ifdef FEAT_FLOAT
4972 if (var2.v_type == VAR_FLOAT)
4973 {
4974 if (!use_float)
4975 {
4976 f1 = n1;
4977 use_float = TRUE;
4978 }
4979 f2 = var2.vval.v_float;
4980 n2 = 0;
4981 }
4982 else
4983#endif
4984 {
4985 n2 = get_tv_number_chk(&var2, &error);
4986 clear_tv(&var2);
4987 if (error)
4988 return FAIL;
4989#ifdef FEAT_FLOAT
4990 if (use_float)
4991 f2 = n2;
4992#endif
4993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
4995 /*
4996 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999#ifdef FEAT_FLOAT
5000 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 if (op == '*')
5003 f1 = f1 * f2;
5004 else if (op == '/')
5005 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005006# ifdef VMS
5007 /* VMS crashes on divide by zero, work around it */
5008 if (f2 == 0.0)
5009 {
5010 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005011 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005012 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005013 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005014 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005015 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005016 }
5017 else
5018 f1 = f1 / f2;
5019# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 /* We rely on the floating point library to handle divide
5021 * by zero to result in "inf" and not a crash. */
5022 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005026 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005027 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028 return FAIL;
5029 }
5030 rettv->v_type = VAR_FLOAT;
5031 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005036 if (op == '*')
5037 n1 = n1 * n2;
5038 else if (op == '/')
5039 {
5040 if (n2 == 0) /* give an error message? */
5041 {
5042 if (n1 == 0)
5043 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5044 else if (n1 < 0)
5045 n1 = -0x7fffffffL;
5046 else
5047 n1 = 0x7fffffffL;
5048 }
5049 else
5050 n1 = n1 / n2;
5051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005053 {
5054 if (n2 == 0) /* give an error message? */
5055 n1 = 0;
5056 else
5057 n1 = n1 % n2;
5058 }
5059 rettv->v_type = VAR_NUMBER;
5060 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063 }
5064
5065 return OK;
5066}
5067
5068/*
5069 * Handle sixth level expression:
5070 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005071 * "string" string constant
5072 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 * &option-name option value
5074 * @r register contents
5075 * identifier variable value
5076 * function() function call
5077 * $VAR environment variable
5078 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005079 * [expr, expr] List
5080 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 *
5082 * Also handle:
5083 * ! in front logical NOT
5084 * - in front unary minus
5085 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005086 * trailing [] subscript in String or List
5087 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 *
5089 * "arg" must point to the first non-white of the expression.
5090 * "arg" is advanced to the next non-white after the recognized expression.
5091 *
5092 * Return OK or FAIL.
5093 */
5094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005095eval7(
5096 char_u **arg,
5097 typval_T *rettv,
5098 int evaluate,
5099 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 long n;
5102 int len;
5103 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 char_u *start_leader, *end_leader;
5105 int ret = OK;
5106 char_u *alias;
5107
5108 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005110 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 /*
5115 * Skip '!' and '-' characters. They are handled later.
5116 */
5117 start_leader = *arg;
5118 while (**arg == '!' || **arg == '-' || **arg == '+')
5119 *arg = skipwhite(*arg + 1);
5120 end_leader = *arg;
5121
5122 switch (**arg)
5123 {
5124 /*
5125 * Number constant.
5126 */
5127 case '0':
5128 case '1':
5129 case '2':
5130 case '3':
5131 case '4':
5132 case '5':
5133 case '6':
5134 case '7':
5135 case '8':
5136 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005137 {
5138#ifdef FEAT_FLOAT
5139 char_u *p = skipdigits(*arg + 1);
5140 int get_float = FALSE;
5141
5142 /* We accept a float when the format matches
5143 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005144 * strict to avoid backwards compatibility problems.
5145 * Don't look for a float after the "." operator, so that
5146 * ":let vers = 1.2.3" doesn't fail. */
5147 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005149 get_float = TRUE;
5150 p = skipdigits(p + 2);
5151 if (*p == 'e' || *p == 'E')
5152 {
5153 ++p;
5154 if (*p == '-' || *p == '+')
5155 ++p;
5156 if (!vim_isdigit(*p))
5157 get_float = FALSE;
5158 else
5159 p = skipdigits(p + 1);
5160 }
5161 if (ASCII_ISALPHA(*p) || *p == '.')
5162 get_float = FALSE;
5163 }
5164 if (get_float)
5165 {
5166 float_T f;
5167
5168 *arg += string2float(*arg, &f);
5169 if (evaluate)
5170 {
5171 rettv->v_type = VAR_FLOAT;
5172 rettv->vval.v_float = f;
5173 }
5174 }
5175 else
5176#endif
5177 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005178 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005179 *arg += len;
5180 if (evaluate)
5181 {
5182 rettv->v_type = VAR_NUMBER;
5183 rettv->vval.v_number = n;
5184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 /*
5190 * String constant: "string".
5191 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005192 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 break;
5194
5195 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005196 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005199 break;
5200
5201 /*
5202 * List: [expr, expr]
5203 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005204 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206
5207 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005208 * Dictionary: {key: val, key: val}
5209 */
5210 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5211 break;
5212
5213 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005214 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005216 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218
5219 /*
5220 * Environment variable: $VAR.
5221 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005222 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224
5225 /*
5226 * Register contents: @r.
5227 */
5228 case '@': ++*arg;
5229 if (evaluate)
5230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005232 rettv->vval.v_string = get_reg_contents(**arg,
5233 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 if (**arg != NUL)
5236 ++*arg;
5237 break;
5238
5239 /*
5240 * nested expression: (expression).
5241 */
5242 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005243 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (**arg == ')')
5245 ++*arg;
5246 else if (ret == OK)
5247 {
5248 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 ret = FAIL;
5251 }
5252 break;
5253
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 break;
5256 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005257
5258 if (ret == NOTDONE)
5259 {
5260 /*
5261 * Must be a variable or function name.
5262 * Can also be a curly-braces kind of name: {expr}.
5263 */
5264 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 if (alias != NULL)
5267 s = alias;
5268
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005269 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 ret = FAIL;
5271 else
5272 {
5273 if (**arg == '(') /* recursive! */
5274 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005275 partial_T *partial;
5276
Bram Moolenaar8c711452005-01-14 21:53:12 +00005277 /* If "s" is the name of a variable of type VAR_FUNC
5278 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005279 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005280
5281 /* Invoke the function. */
5282 ret = get_func_tv(s, len, rettv, arg,
5283 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005285
5286 /* If evaluate is FALSE rettv->v_type was not set in
5287 * get_func_tv, but it's needed in handle_subscript() to parse
5288 * what follows. So set it here. */
5289 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5290 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005291 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005292 rettv->v_type = VAR_FUNC;
5293 }
5294
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 /* Stop the expression evaluation when immediately
5296 * aborting on error, or when an interrupt occurred or
5297 * an exception was thrown but not caught. */
5298 if (aborting())
5299 {
5300 if (ret == OK)
5301 clear_tv(rettv);
5302 ret = FAIL;
5303 }
5304 }
5305 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005306 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005307 else
5308 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005310 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005311 }
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 *arg = skipwhite(*arg);
5314
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005315 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5316 * expr(expr). */
5317 if (ret == OK)
5318 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /*
5321 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5322 */
5323 if (ret == OK && evaluate && end_leader > start_leader)
5324 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005325 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005326 int val = 0;
5327#ifdef FEAT_FLOAT
5328 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005329
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330 if (rettv->v_type == VAR_FLOAT)
5331 f = rettv->vval.v_float;
5332 else
5333#endif
5334 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005337 clear_tv(rettv);
5338 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 else
5341 {
5342 while (end_leader > start_leader)
5343 {
5344 --end_leader;
5345 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005346 {
5347#ifdef FEAT_FLOAT
5348 if (rettv->v_type == VAR_FLOAT)
5349 f = !f;
5350 else
5351#endif
5352 val = !val;
5353 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 {
5356#ifdef FEAT_FLOAT
5357 if (rettv->v_type == VAR_FLOAT)
5358 f = -f;
5359 else
5360#endif
5361 val = -val;
5362 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005363 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364#ifdef FEAT_FLOAT
5365 if (rettv->v_type == VAR_FLOAT)
5366 {
5367 clear_tv(rettv);
5368 rettv->vval.v_float = f;
5369 }
5370 else
5371#endif
5372 {
5373 clear_tv(rettv);
5374 rettv->v_type = VAR_NUMBER;
5375 rettv->vval.v_number = val;
5376 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379
5380 return ret;
5381}
5382
5383/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005384 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5385 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5387 */
5388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005389eval_index(
5390 char_u **arg,
5391 typval_T *rettv,
5392 int evaluate,
5393 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394{
5395 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 long len = -1;
5399 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005402
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005405 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005406 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 if (verbose)
5408 EMSG(_("E695: Cannot index a Funcref"));
5409 return FAIL;
5410 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005411#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 if (verbose)
5413 EMSG(_(e_float_as_string));
5414 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005415#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005417 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005418 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005419 if (verbose)
5420 EMSG(_("E909: Cannot index a special variable"));
5421 return FAIL;
5422 case VAR_UNKNOWN:
5423 if (evaluate)
5424 return FAIL;
5425 /* FALLTHROUGH */
5426
5427 case VAR_STRING:
5428 case VAR_NUMBER:
5429 case VAR_LIST:
5430 case VAR_DICT:
5431 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005434 init_tv(&var1);
5435 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005436 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005438 /*
5439 * dict.name
5440 */
5441 key = *arg + 1;
5442 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5443 ;
5444 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005446 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 }
5448 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 /*
5451 * something[idx]
5452 *
5453 * Get the (first) variable from inside the [].
5454 */
5455 *arg = skipwhite(*arg + 1);
5456 if (**arg == ':')
5457 empty1 = TRUE;
5458 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5459 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005460 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5461 {
5462 /* not a number or string */
5463 clear_tv(&var1);
5464 return FAIL;
5465 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466
5467 /*
5468 * Get the second variable from inside the [:].
5469 */
5470 if (**arg == ':')
5471 {
5472 range = TRUE;
5473 *arg = skipwhite(*arg + 1);
5474 if (**arg == ']')
5475 empty2 = TRUE;
5476 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005478 if (!empty1)
5479 clear_tv(&var1);
5480 return FAIL;
5481 }
5482 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5483 {
5484 /* not a number or string */
5485 if (!empty1)
5486 clear_tv(&var1);
5487 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 return FAIL;
5489 }
5490 }
5491
5492 /* Check for the ']'. */
5493 if (**arg != ']')
5494 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005495 if (verbose)
5496 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 clear_tv(&var1);
5498 if (range)
5499 clear_tv(&var2);
5500 return FAIL;
5501 }
5502 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 }
5504
5505 if (evaluate)
5506 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005507 n1 = 0;
5508 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 n1 = get_tv_number(&var1);
5511 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513 if (range)
5514 {
5515 if (empty2)
5516 n2 = -1;
5517 else
5518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n2 = get_tv_number(&var2);
5520 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 }
5523
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005526 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005527 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005528 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005529 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005530 case VAR_SPECIAL:
5531 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005532 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005533 break; /* not evaluating, skipping over subscript */
5534
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005535 case VAR_NUMBER:
5536 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 len = (long)STRLEN(s);
5539 if (range)
5540 {
5541 /* The resulting variable is a substring. If the indexes
5542 * are out of range the result is empty. */
5543 if (n1 < 0)
5544 {
5545 n1 = len + n1;
5546 if (n1 < 0)
5547 n1 = 0;
5548 }
5549 if (n2 < 0)
5550 n2 = len + n2;
5551 else if (n2 >= len)
5552 n2 = len;
5553 if (n1 >= len || n2 < 0 || n1 > n2)
5554 s = NULL;
5555 else
5556 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5557 }
5558 else
5559 {
5560 /* The resulting variable is a string of a single
5561 * character. If the index is too big or negative the
5562 * result is empty. */
5563 if (n1 >= len || n1 < 0)
5564 s = NULL;
5565 else
5566 s = vim_strnsave(s + n1, 1);
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 rettv->v_type = VAR_STRING;
5570 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 break;
5572
5573 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005575 if (n1 < 0)
5576 n1 = len + n1;
5577 if (!empty1 && (n1 < 0 || n1 >= len))
5578 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005579 /* For a range we allow invalid values and return an empty
5580 * list. A list index out of range is an error. */
5581 if (!range)
5582 {
5583 if (verbose)
5584 EMSGN(_(e_listidx), n1);
5585 return FAIL;
5586 }
5587 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 }
5589 if (range)
5590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 list_T *l;
5592 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593
5594 if (n2 < 0)
5595 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005596 else if (n2 >= len)
5597 n2 = len - 1;
5598 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005599 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 l = list_alloc();
5601 if (l == NULL)
5602 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 n1 <= n2; ++n1)
5605 {
5606 if (list_append_tv(l, &item->li_tv) == FAIL)
5607 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005608 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 return FAIL;
5610 }
5611 item = item->li_next;
5612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005613 clear_tv(rettv);
5614 rettv->v_type = VAR_LIST;
5615 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005616 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 }
5618 else
5619 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005620 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621 clear_tv(rettv);
5622 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 }
5624 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005625
5626 case VAR_DICT:
5627 if (range)
5628 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005629 if (verbose)
5630 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005631 if (len == -1)
5632 clear_tv(&var1);
5633 return FAIL;
5634 }
5635 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005636 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637
5638 if (len == -1)
5639 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005640 key = get_tv_string_chk(&var1);
5641 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005642 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005643 clear_tv(&var1);
5644 return FAIL;
5645 }
5646 }
5647
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005648 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005649
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005650 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005651 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 if (len == -1)
5653 clear_tv(&var1);
5654 if (item == NULL)
5655 return FAIL;
5656
5657 copy_tv(&item->di_tv, &var1);
5658 clear_tv(rettv);
5659 *rettv = var1;
5660 }
5661 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 }
5663 }
5664
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005665 return OK;
5666}
5667
5668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 * Get an option value.
5670 * "arg" points to the '&' or '+' before the option name.
5671 * "arg" is advanced to character after the option name.
5672 * Return OK or FAIL.
5673 */
5674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005675get_option_tv(
5676 char_u **arg,
5677 typval_T *rettv, /* when NULL, only check if option exists */
5678 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 char_u *option_end;
5681 long numval;
5682 char_u *stringval;
5683 int opt_type;
5684 int c;
5685 int working = (**arg == '+'); /* has("+option") */
5686 int ret = OK;
5687 int opt_flags;
5688
5689 /*
5690 * Isolate the option name and find its value.
5691 */
5692 option_end = find_option_end(arg, &opt_flags);
5693 if (option_end == NULL)
5694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 EMSG2(_("E112: Option name missing: %s"), *arg);
5697 return FAIL;
5698 }
5699
5700 if (!evaluate)
5701 {
5702 *arg = option_end;
5703 return OK;
5704 }
5705
5706 c = *option_end;
5707 *option_end = NUL;
5708 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710
5711 if (opt_type == -3) /* invalid name */
5712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 EMSG2(_("E113: Unknown option: %s"), *arg);
5715 ret = FAIL;
5716 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005717 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 if (opt_type == -2) /* hidden string option */
5720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721 rettv->v_type = VAR_STRING;
5722 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 else if (opt_type == -1) /* hidden number option */
5725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 rettv->v_type = VAR_NUMBER;
5727 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 else if (opt_type == 1) /* number option */
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_NUMBER;
5732 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 else /* string option */
5735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005736 rettv->v_type = VAR_STRING;
5737 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739 }
5740 else if (working && (opt_type == -2 || opt_type == -1))
5741 ret = FAIL;
5742
5743 *option_end = c; /* put back for error messages */
5744 *arg = option_end;
5745
5746 return ret;
5747}
5748
5749/*
5750 * Allocate a variable for a string constant.
5751 * Return OK or FAIL.
5752 */
5753 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005754get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 char_u *p;
5757 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 int extra = 0;
5759
5760 /*
5761 * Find the end of the string, skipping backslashed characters.
5762 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 if (*p == '\\' && p[1] != NUL)
5766 {
5767 ++p;
5768 /* A "\<x>" form occupies at least 4 characters, and produces up
5769 * to 6 characters: reserve space for 2 extra */
5770 if (*p == '<')
5771 extra += 2;
5772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 }
5774
5775 if (*p != '"')
5776 {
5777 EMSG2(_("E114: Missing quote: %s"), *arg);
5778 return FAIL;
5779 }
5780
5781 /* If only parsing, set *arg and return here */
5782 if (!evaluate)
5783 {
5784 *arg = p + 1;
5785 return OK;
5786 }
5787
5788 /*
5789 * Copy the string into allocated memory, handling backslashed
5790 * characters.
5791 */
5792 name = alloc((unsigned)(p - *arg + extra));
5793 if (name == NULL)
5794 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 rettv->v_type = VAR_STRING;
5796 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 if (*p == '\\')
5801 {
5802 switch (*++p)
5803 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 case 'b': *name++ = BS; ++p; break;
5805 case 'e': *name++ = ESC; ++p; break;
5806 case 'f': *name++ = FF; ++p; break;
5807 case 'n': *name++ = NL; ++p; break;
5808 case 'r': *name++ = CAR; ++p; break;
5809 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 case 'X': /* hex: "\x1", "\x12" */
5812 case 'x':
5813 case 'u': /* Unicode: "\u0023" */
5814 case 'U':
5815 if (vim_isxdigit(p[1]))
5816 {
5817 int n, nr;
5818 int c = toupper(*p);
5819
5820 if (c == 'X')
5821 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005822 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005824 else
5825 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 nr = 0;
5827 while (--n >= 0 && vim_isxdigit(p[1]))
5828 {
5829 ++p;
5830 nr = (nr << 4) + hex2nr(*p);
5831 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#ifdef FEAT_MBYTE
5834 /* For "\u" store the number according to
5835 * 'encoding'. */
5836 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 else
5839#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 break;
5843
5844 /* octal: "\1", "\12", "\123" */
5845 case '0':
5846 case '1':
5847 case '2':
5848 case '3':
5849 case '4':
5850 case '5':
5851 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 case '7': *name = *p++ - '0';
5853 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005855 *name = (*name << 3) + *p++ - '0';
5856 if (*p >= '0' && *p <= '7')
5857 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 break;
5861
5862 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005863 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 if (extra != 0)
5865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 break;
5868 }
5869 /* FALLTHROUGH */
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 break;
5873 }
5874 }
5875 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005876 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 *arg = p + 1;
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 return OK;
5883}
5884
5885/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 * Return OK or FAIL.
5888 */
5889 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005890get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 int reduce = 0;
5895
5896 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 */
5899 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5900 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 break;
5905 ++reduce;
5906 ++p;
5907 }
5908 }
5909
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 return FAIL;
5914 }
5915
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 if (!evaluate)
5918 {
5919 *arg = p + 1;
5920 return OK;
5921 }
5922
5923 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 */
5926 str = alloc((unsigned)((p - *arg) - reduce));
5927 if (str == NULL)
5928 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 rettv->v_type = VAR_STRING;
5930 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005934 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005936 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005937 break;
5938 ++p;
5939 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005942 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005943 *arg = p + 1;
5944
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005945 return OK;
5946}
5947
Bram Moolenaarddecc252016-04-06 22:59:37 +02005948 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005949partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005950{
5951 int i;
5952
5953 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005954 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005955 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005956 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 func_unref(pt->pt_name);
5958 vim_free(pt->pt_name);
5959 vim_free(pt);
5960}
5961
5962/*
5963 * Unreference a closure: decrement the reference count and free it when it
5964 * becomes zero.
5965 */
5966 void
5967partial_unref(partial_T *pt)
5968{
5969 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005970 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005971}
5972
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005973/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 * Allocate a variable for a List and fill it from "*arg".
5975 * Return OK or FAIL.
5976 */
5977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005978get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005979{
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 list_T *l = NULL;
5981 typval_T tv;
5982 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983
5984 if (evaluate)
5985 {
5986 l = list_alloc();
5987 if (l == NULL)
5988 return FAIL;
5989 }
5990
5991 *arg = skipwhite(*arg + 1);
5992 while (**arg != ']' && **arg != NUL)
5993 {
5994 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5995 goto failret;
5996 if (evaluate)
5997 {
5998 item = listitem_alloc();
5999 if (item != NULL)
6000 {
6001 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006002 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 list_append(l, item);
6004 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006005 else
6006 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006007 }
6008
6009 if (**arg == ']')
6010 break;
6011 if (**arg != ',')
6012 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006013 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014 goto failret;
6015 }
6016 *arg = skipwhite(*arg + 1);
6017 }
6018
6019 if (**arg != ']')
6020 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006021 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006022failret:
6023 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006024 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025 return FAIL;
6026 }
6027
6028 *arg = skipwhite(*arg + 1);
6029 if (evaluate)
6030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006031 rettv->v_type = VAR_LIST;
6032 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006033 ++l->lv_refcount;
6034 }
6035
6036 return OK;
6037}
6038
6039/*
6040 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006041 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006043 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006046 list_T *l;
6047
6048 l = (list_T *)alloc_clear(sizeof(list_T));
6049 if (l != NULL)
6050 {
6051 /* Prepend the list to the list of lists for garbage collection. */
6052 if (first_list != NULL)
6053 first_list->lv_used_prev = l;
6054 l->lv_used_prev = NULL;
6055 l->lv_used_next = first_list;
6056 first_list = l;
6057 }
6058 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059}
6060
6061/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006062 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006063 * Returns OK or FAIL.
6064 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006065 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006066rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006067{
6068 list_T *l = list_alloc();
6069
6070 if (l == NULL)
6071 return FAIL;
6072
6073 rettv->vval.v_list = l;
6074 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006075 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076 ++l->lv_refcount;
6077 return OK;
6078}
6079
6080/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081 * Unreference a list: decrement the reference count and free it when it
6082 * becomes zero.
6083 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006087 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006088 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089}
6090
6091/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006092 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 * Ignores the reference count.
6094 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006095 static void
6096list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097{
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006099
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006100 for (item = l->lv_first; item != NULL; item = l->lv_first)
6101 {
6102 /* Remove the item before deleting it. */
6103 l->lv_first = item->li_next;
6104 clear_tv(&item->li_tv);
6105 vim_free(item);
6106 }
6107}
6108
6109 static void
6110list_free_list(list_T *l)
6111{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006112 /* Remove the list from the list of lists for garbage collection. */
6113 if (l->lv_used_prev == NULL)
6114 first_list = l->lv_used_next;
6115 else
6116 l->lv_used_prev->lv_used_next = l->lv_used_next;
6117 if (l->lv_used_next != NULL)
6118 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6119
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 vim_free(l);
6121}
6122
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006123 void
6124list_free(list_T *l)
6125{
6126 if (!in_free_unref_items)
6127 {
6128 list_free_contents(l);
6129 list_free_list(l);
6130 }
6131}
6132
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006133/*
6134 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006135 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006137 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006138listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006141}
6142
6143/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006144 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006147listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006149 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150 vim_free(item);
6151}
6152
6153/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006154 * Remove a list item from a List and free it. Also clears the value.
6155 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006156 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006158{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006159 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006160 listitem_free(item);
6161}
6162
6163/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 * Get the number of items in a list.
6165 */
6166 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006167list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006169 if (l == NULL)
6170 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006171 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172}
6173
6174/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006175 * Return TRUE when two lists have exactly the same values.
6176 */
6177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006178list_equal(
6179 list_T *l1,
6180 list_T *l2,
6181 int ic, /* ignore case for strings */
6182 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183{
Bram Moolenaar33570922005-01-25 22:26:29 +00006184 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006186 if (l1 == NULL || l2 == NULL)
6187 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006188 if (l1 == l2)
6189 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006190 if (list_len(l1) != list_len(l2))
6191 return FALSE;
6192
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193 for (item1 = l1->lv_first, item2 = l2->lv_first;
6194 item1 != NULL && item2 != NULL;
6195 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 return FALSE;
6198 return item1 == NULL && item2 == NULL;
6199}
6200
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201/*
6202 * Return the dictitem that an entry in a hashtable points to.
6203 */
6204 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006205dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006206{
6207 return HI2DI(hi);
6208}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006209
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006211 * Return TRUE when two dictionaries have exactly the same key/values.
6212 */
6213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_equal(
6215 dict_T *d1,
6216 dict_T *d2,
6217 int ic, /* ignore case for strings */
6218 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006219{
Bram Moolenaar33570922005-01-25 22:26:29 +00006220 hashitem_T *hi;
6221 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006222 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006223
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006224 if (d1 == NULL || d2 == NULL)
6225 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 if (d1 == d2)
6227 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228 if (dict_len(d1) != dict_len(d2))
6229 return FALSE;
6230
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006231 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006232 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006233 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006234 if (!HASHITEM_EMPTY(hi))
6235 {
6236 item2 = dict_find(d2, hi->hi_key, -1);
6237 if (item2 == NULL)
6238 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006239 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006240 return FALSE;
6241 --todo;
6242 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006243 }
6244 return TRUE;
6245}
6246
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006247static int tv_equal_recurse_limit;
6248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006249/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006250 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006251 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006252 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 */
6254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006255tv_equal(
6256 typval_T *tv1,
6257 typval_T *tv2,
6258 int ic, /* ignore case */
6259 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260{
6261 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006262 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006263 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006264 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006265
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006266 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6267 if ((tv1->v_type == VAR_FUNC
6268 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6269 && (tv2->v_type == VAR_FUNC
6270 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6271 {
6272 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6273 : tv1->vval.v_partial->pt_name;
6274 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6275 : tv2->vval.v_partial->pt_name;
6276 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6277 }
6278
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006279 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006280 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006281
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006282 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006283 * recursiveness to a limit. We guess they are equal then.
6284 * A fixed limit has the problem of still taking an awful long time.
6285 * Reduce the limit every time running into it. That should work fine for
6286 * deeply linked structures that are not recursively linked and catch
6287 * recursiveness quickly. */
6288 if (!recursive)
6289 tv_equal_recurse_limit = 1000;
6290 if (recursive_cnt >= tv_equal_recurse_limit)
6291 {
6292 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006293 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006295
6296 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006297 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006298 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006299 ++recursive_cnt;
6300 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6301 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006302 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006303
6304 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006305 ++recursive_cnt;
6306 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6307 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006308 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006309
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006310 case VAR_NUMBER:
6311 return tv1->vval.v_number == tv2->vval.v_number;
6312
6313 case VAR_STRING:
6314 s1 = get_tv_string_buf(tv1, buf1);
6315 s2 = get_tv_string_buf(tv2, buf2);
6316 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006317
6318 case VAR_SPECIAL:
6319 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006320
6321 case VAR_FLOAT:
6322#ifdef FEAT_FLOAT
6323 return tv1->vval.v_float == tv2->vval.v_float;
6324#endif
6325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006327 return tv1->vval.v_job == tv2->vval.v_job;
6328#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006329 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006330#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006331 return tv1->vval.v_channel == tv2->vval.v_channel;
6332#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006333 case VAR_FUNC:
6334 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006335 case VAR_UNKNOWN:
6336 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006338
Bram Moolenaara03f2332016-02-06 18:09:59 +01006339 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6340 * does not equal anything, not even itself. */
6341 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006342}
6343
6344/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 * Locate item with index "n" in list "l" and return it.
6346 * A negative index is counted from the end; -1 is the last item.
6347 * Returns NULL when "n" is out of range.
6348 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006349 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006350list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351{
Bram Moolenaar33570922005-01-25 22:26:29 +00006352 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 long idx;
6354
6355 if (l == NULL)
6356 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006357
6358 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006360 n = l->lv_len + n;
6361
6362 /* Check for index out of range. */
6363 if (n < 0 || n >= l->lv_len)
6364 return NULL;
6365
6366 /* When there is a cached index may start search from there. */
6367 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 if (n < l->lv_idx / 2)
6370 {
6371 /* closest to the start of the list */
6372 item = l->lv_first;
6373 idx = 0;
6374 }
6375 else if (n > (l->lv_idx + l->lv_len) / 2)
6376 {
6377 /* closest to the end of the list */
6378 item = l->lv_last;
6379 idx = l->lv_len - 1;
6380 }
6381 else
6382 {
6383 /* closest to the cached index */
6384 item = l->lv_idx_item;
6385 idx = l->lv_idx;
6386 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006387 }
6388 else
6389 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006390 if (n < l->lv_len / 2)
6391 {
6392 /* closest to the start of the list */
6393 item = l->lv_first;
6394 idx = 0;
6395 }
6396 else
6397 {
6398 /* closest to the end of the list */
6399 item = l->lv_last;
6400 idx = l->lv_len - 1;
6401 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006402 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006403
6404 while (n > idx)
6405 {
6406 /* search forward */
6407 item = item->li_next;
6408 ++idx;
6409 }
6410 while (n < idx)
6411 {
6412 /* search backward */
6413 item = item->li_prev;
6414 --idx;
6415 }
6416
6417 /* cache the used index */
6418 l->lv_idx = idx;
6419 l->lv_idx_item = item;
6420
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421 return item;
6422}
6423
6424/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006425 * Get list item "l[idx]" as a number.
6426 */
6427 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_find_nr(
6429 list_T *l,
6430 long idx,
6431 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006432{
6433 listitem_T *li;
6434
6435 li = list_find(l, idx);
6436 if (li == NULL)
6437 {
6438 if (errorp != NULL)
6439 *errorp = TRUE;
6440 return -1L;
6441 }
6442 return get_tv_number_chk(&li->li_tv, errorp);
6443}
6444
6445/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006446 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6447 */
6448 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006449list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006450{
6451 listitem_T *li;
6452
6453 li = list_find(l, idx - 1);
6454 if (li == NULL)
6455 {
6456 EMSGN(_(e_listidx), idx);
6457 return NULL;
6458 }
6459 return get_tv_string(&li->li_tv);
6460}
6461
6462/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006463 * Locate "item" list "l" and return its index.
6464 * Returns -1 when "item" is not in the list.
6465 */
6466 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006467list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006468{
6469 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006470 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006471
6472 if (l == NULL)
6473 return -1;
6474 idx = 0;
6475 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6476 ++idx;
6477 if (li == NULL)
6478 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006479 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480}
6481
6482/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006483 * Append item "item" to the end of list "l".
6484 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006486list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487{
6488 if (l->lv_last == NULL)
6489 {
6490 /* empty list */
6491 l->lv_first = item;
6492 l->lv_last = item;
6493 item->li_prev = NULL;
6494 }
6495 else
6496 {
6497 l->lv_last->li_next = item;
6498 item->li_prev = l->lv_last;
6499 l->lv_last = item;
6500 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006501 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502 item->li_next = NULL;
6503}
6504
6505/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513
Bram Moolenaar05159a02005-02-26 23:04:13 +00006514 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006515 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006516 copy_tv(tv, &li->li_tv);
6517 list_append(l, li);
6518 return OK;
6519}
6520
6521/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006522 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 * Return FAIL when out of memory.
6524 */
6525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006526list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006527{
6528 listitem_T *li = listitem_alloc();
6529
6530 if (li == NULL)
6531 return FAIL;
6532 li->li_tv.v_type = VAR_DICT;
6533 li->li_tv.v_lock = 0;
6534 li->li_tv.vval.v_dict = dict;
6535 list_append(list, li);
6536 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 return OK;
6538}
6539
6540/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006541 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006542 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006543 * Returns FAIL when out of memory.
6544 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006547{
6548 listitem_T *li = listitem_alloc();
6549
6550 if (li == NULL)
6551 return FAIL;
6552 list_append(l, li);
6553 li->li_tv.v_type = VAR_STRING;
6554 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006555 if (str == NULL)
6556 li->li_tv.vval.v_string = NULL;
6557 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006558 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006559 return FAIL;
6560 return OK;
6561}
6562
6563/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006564 * Append "n" to list "l".
6565 * Returns FAIL when out of memory.
6566 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006569{
6570 listitem_T *li;
6571
6572 li = listitem_alloc();
6573 if (li == NULL)
6574 return FAIL;
6575 li->li_tv.v_type = VAR_NUMBER;
6576 li->li_tv.v_lock = 0;
6577 li->li_tv.vval.v_number = n;
6578 list_append(l, li);
6579 return OK;
6580}
6581
6582/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 * If "item" is NULL append at the end.
6585 * Return FAIL when out of memory.
6586 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006588list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589{
Bram Moolenaar33570922005-01-25 22:26:29 +00006590 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006591
6592 if (ni == NULL)
6593 return FAIL;
6594 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006595 list_insert(l, ni, item);
6596 return OK;
6597}
6598
6599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006600list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006601{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602 if (item == NULL)
6603 /* Append new item at end of list. */
6604 list_append(l, ni);
6605 else
6606 {
6607 /* Insert new item before existing item. */
6608 ni->li_prev = item->li_prev;
6609 ni->li_next = item;
6610 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 ++l->lv_idx;
6614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006616 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006618 l->lv_idx_item = NULL;
6619 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006620 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006621 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006622 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006623}
6624
6625/*
6626 * Extend "l1" with "l2".
6627 * If "bef" is NULL append at the end, otherwise insert before this item.
6628 * Returns FAIL when out of memory.
6629 */
6630 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006631list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632{
Bram Moolenaar33570922005-01-25 22:26:29 +00006633 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006634 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006635
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006636 /* We also quit the loop when we have inserted the original item count of
6637 * the list, avoid a hang when we extend a list with itself. */
6638 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6640 return FAIL;
6641 return OK;
6642}
6643
6644/*
6645 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6646 * Return FAIL when out of memory.
6647 */
6648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006649list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006650{
Bram Moolenaar33570922005-01-25 22:26:29 +00006651 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006652
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006653 if (l1 == NULL || l2 == NULL)
6654 return FAIL;
6655
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006656 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006657 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006658 if (l == NULL)
6659 return FAIL;
6660 tv->v_type = VAR_LIST;
6661 tv->vval.v_list = l;
6662
6663 /* append all items from the second list */
6664 return list_extend(l, l2, NULL);
6665}
6666
6667/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006668 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006670 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 * Returns NULL when out of memory.
6672 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006673 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
Bram Moolenaar33570922005-01-25 22:26:29 +00006676 list_T *copy;
6677 listitem_T *item;
6678 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679
6680 if (orig == NULL)
6681 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682
6683 copy = list_alloc();
6684 if (copy != NULL)
6685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006686 if (copyID != 0)
6687 {
6688 /* Do this before adding the items, because one of the items may
6689 * refer back to this list. */
6690 orig->lv_copyID = copyID;
6691 orig->lv_copylist = copy;
6692 }
6693 for (item = orig->lv_first; item != NULL && !got_int;
6694 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 {
6696 ni = listitem_alloc();
6697 if (ni == NULL)
6698 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006699 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006700 {
6701 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6702 {
6703 vim_free(ni);
6704 break;
6705 }
6706 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006707 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006708 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709 list_append(copy, ni);
6710 }
6711 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006712 if (item != NULL)
6713 {
6714 list_unref(copy);
6715 copy = NULL;
6716 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006717 }
6718
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 return copy;
6720}
6721
6722/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006723 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006724 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006725 * This used to be called list_remove, but that conflicts with a Sun header
6726 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006729vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006730{
Bram Moolenaar33570922005-01-25 22:26:29 +00006731 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006733 /* notify watchers */
6734 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006736 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006737 list_fix_watch(l, ip);
6738 if (ip == item2)
6739 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006740 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006741
6742 if (item2->li_next == NULL)
6743 l->lv_last = item->li_prev;
6744 else
6745 item2->li_next->li_prev = item->li_prev;
6746 if (item->li_prev == NULL)
6747 l->lv_first = item2->li_next;
6748 else
6749 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006750 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751}
6752
6753/*
6754 * Return an allocated string with the string representation of a list.
6755 * May return NULL.
6756 */
6757 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006758list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006759{
6760 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761
6762 if (tv->vval.v_list == NULL)
6763 return NULL;
6764 ga_init2(&ga, (int)sizeof(char), 80);
6765 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006766 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 {
6768 vim_free(ga.ga_data);
6769 return NULL;
6770 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006771 ga_append(&ga, ']');
6772 ga_append(&ga, NUL);
6773 return (char_u *)ga.ga_data;
6774}
6775
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006776typedef struct join_S {
6777 char_u *s;
6778 char_u *tofree;
6779} join_T;
6780
6781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782list_join_inner(
6783 garray_T *gap, /* to store the result in */
6784 list_T *l,
6785 char_u *sep,
6786 int echo_style,
6787 int copyID,
6788 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006789{
6790 int i;
6791 join_T *p;
6792 int len;
6793 int sumlen = 0;
6794 int first = TRUE;
6795 char_u *tofree;
6796 char_u numbuf[NUMBUFLEN];
6797 listitem_T *item;
6798 char_u *s;
6799
6800 /* Stringify each item in the list. */
6801 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6802 {
6803 if (echo_style)
6804 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6805 else
6806 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6807 if (s == NULL)
6808 return FAIL;
6809
6810 len = (int)STRLEN(s);
6811 sumlen += len;
6812
Bram Moolenaarcde88542015-08-11 19:14:00 +02006813 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6815 if (tofree != NULL || s != numbuf)
6816 {
6817 p->s = s;
6818 p->tofree = tofree;
6819 }
6820 else
6821 {
6822 p->s = vim_strnsave(s, len);
6823 p->tofree = p->s;
6824 }
6825
6826 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006827 if (did_echo_string_emsg) /* recursion error, bail out */
6828 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006829 }
6830
6831 /* Allocate result buffer with its total size, avoid re-allocation and
6832 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6833 if (join_gap->ga_len >= 2)
6834 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6835 if (ga_grow(gap, sumlen + 2) == FAIL)
6836 return FAIL;
6837
6838 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6839 {
6840 if (first)
6841 first = FALSE;
6842 else
6843 ga_concat(gap, sep);
6844 p = ((join_T *)join_gap->ga_data) + i;
6845
6846 if (p->s != NULL)
6847 ga_concat(gap, p->s);
6848 line_breakcheck();
6849 }
6850
6851 return OK;
6852}
6853
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006855 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006856 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006857 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006858 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006859 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860list_join(
6861 garray_T *gap,
6862 list_T *l,
6863 char_u *sep,
6864 int echo_style,
6865 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006867 garray_T join_ga;
6868 int retval;
6869 join_T *p;
6870 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006871
Bram Moolenaard39a7512015-04-16 22:51:22 +02006872 if (l->lv_len < 1)
6873 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006874 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6875 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6876
6877 /* Dispose each item in join_ga. */
6878 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006879 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006880 p = (join_T *)join_ga.ga_data;
6881 for (i = 0; i < join_ga.ga_len; ++i)
6882 {
6883 vim_free(p->tofree);
6884 ++p;
6885 }
6886 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006887 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006888
6889 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006890}
6891
6892/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006893 * Return the next (unique) copy ID.
6894 * Used for serializing nested structures.
6895 */
6896 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006897get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006898{
6899 current_copyID += COPYID_INC;
6900 return current_copyID;
6901}
6902
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006903/* Used by get_func_tv() */
6904static garray_T funcargs = GA_EMPTY;
6905
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006906/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 * Garbage collection for lists and dictionaries.
6908 *
6909 * We use reference counts to be able to free most items right away when they
6910 * are no longer used. But for composite items it's possible that it becomes
6911 * unused while the reference count is > 0: When there is a recursive
6912 * reference. Example:
6913 * :let l = [1, 2, 3]
6914 * :let d = {9: l}
6915 * :let l[1] = d
6916 *
6917 * Since this is quite unusual we handle this with garbage collection: every
6918 * once in a while find out which lists and dicts are not referenced from any
6919 * variable.
6920 *
6921 * Here is a good reference text about garbage collection (refers to Python
6922 * but it applies to all reference-counting mechanisms):
6923 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006924 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925
6926/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006927 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006928 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006930 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006932garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006934 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 buf_T *buf;
6937 win_T *wp;
6938 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006939 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006940 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006942#ifdef FEAT_WINDOWS
6943 tabpage_T *tp;
6944#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006946 if (!testing)
6947 {
6948 /* Only do this once. */
6949 want_garbage_collect = FALSE;
6950 may_garbage_collect = FALSE;
6951 garbage_collect_at_exit = FALSE;
6952 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006953
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 /* We advance by two because we add one for items referenced through
6955 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006956 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 /*
6959 * 1. Go through all accessible variables and mark all lists and dicts
6960 * with copyID.
6961 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962
6963 /* Don't free variables in the previous_funccal list unless they are only
6964 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006965 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6969 NULL);
6970 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6971 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972 }
6973
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 /* script-local variables */
6975 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006976 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977
6978 /* buffer-local variables */
6979 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6981 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006982
6983 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006984 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6986 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006987#ifdef FEAT_AUTOCMD
6988 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006991#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993#ifdef FEAT_WINDOWS
6994 /* tabpage-local variables */
6995 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6997 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006998#endif
6999
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002
7003 /* function-local variables */
7004 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7005 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007006 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7007 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 }
7009
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007010 /* function call arguments, if v:testing is set. */
7011 for (i = 0; i < funcargs.ga_len; ++i)
7012 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7013 copyID, NULL, NULL);
7014
Bram Moolenaard812df62008-11-09 12:46:09 +00007015 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007016 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007017
Bram Moolenaar1dced572012-04-05 16:54:08 +02007018#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007020#endif
7021
Bram Moolenaardb913952012-06-29 12:54:53 +02007022#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007023 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007024#endif
7025
7026#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007027 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007028#endif
7029
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007031 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007032 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007033#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007034#ifdef FEAT_NETBEANS_INTG
7035 abort = abort || set_ref_in_nb_channel(copyID);
7036#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007037
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007039 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 /*
7041 * 2. Free lists and dictionaries that are not referenced.
7042 */
7043 did_free = free_unref_items(copyID);
7044
7045 /*
7046 * 3. Check if any funccal can be freed now.
7047 */
7048 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007049 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 if (can_free_funccal(*pfc, copyID))
7051 {
7052 fc = *pfc;
7053 *pfc = fc->caller;
7054 free_funccal(fc, TRUE);
7055 did_free = TRUE;
7056 did_free_funccal = TRUE;
7057 }
7058 else
7059 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007060 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 if (did_free_funccal)
7062 /* When a funccal was freed some more items might be garbage
7063 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007064 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007065 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007066 else if (p_verbose > 0)
7067 {
7068 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7069 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070
7071 return did_free;
7072}
7073
7074/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007075 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007076 */
7077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007079{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007080 dict_T *dd, *dd_next;
7081 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007082 int did_free = FALSE;
7083
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007084 /* Let all "free" functions know that we are here. This means no
7085 * dictionaries, lists, channels or jobs are to be freed, because we will
7086 * do that here. */
7087 in_free_unref_items = TRUE;
7088
7089 /*
7090 * PASS 1: free the contents of the items. We don't free the items
7091 * themselves yet, so that it is possible to decrement refcount counters
7092 */
7093
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007094 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007095 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007097 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007098 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007099 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007100 /* Free the Dictionary and ordinary items it contains, but don't
7101 * recurse into Lists and Dictionaries, they will be in the list
7102 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007103 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106
7107 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007108 * Go through the list of lists and free items without the copyID.
7109 * But don't free a list that has a watcher (used in a for loop), these
7110 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007111 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007112 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7113 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7114 && ll->lv_watch == NULL)
7115 {
7116 /* Free the List and ordinary items it contains, but don't recurse
7117 * into Lists and Dictionaries, they will be in the list of dicts
7118 * or list of lists. */
7119 list_free_contents(ll);
7120 did_free = TRUE;
7121 }
7122
7123#ifdef FEAT_JOB_CHANNEL
7124 /* Go through the list of jobs and free items without the copyID. This
7125 * must happen before doing channels, because jobs refer to channels, but
7126 * the reference from the channel to the job isn't tracked. */
7127 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7128
7129 /* Go through the list of channels and free items without the copyID. */
7130 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7131#endif
7132
7133 /*
7134 * PASS 2: free the items themselves.
7135 */
7136 for (dd = first_dict; dd != NULL; dd = dd_next)
7137 {
7138 dd_next = dd->dv_used_next;
7139 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7140 dict_free_dict(dd);
7141 }
7142
7143 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007144 {
7145 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007146 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7147 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007148 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007149 /* Free the List and ordinary items it contains, but don't recurse
7150 * into Lists and Dictionaries, they will be in the list of dicts
7151 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007152 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007153 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007154 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007155
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007156#ifdef FEAT_JOB_CHANNEL
7157 /* Go through the list of jobs and free items without the copyID. This
7158 * must happen before doing channels, because jobs refer to channels, but
7159 * the reference from the channel to the job isn't tracked. */
7160 free_unused_jobs(copyID, COPYID_MASK);
7161
7162 /* Go through the list of channels and free items without the copyID. */
7163 free_unused_channels(copyID, COPYID_MASK);
7164#endif
7165
7166 in_free_unref_items = FALSE;
7167
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007168 return did_free;
7169}
7170
7171/*
7172 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007173 * "list_stack" is used to add lists to be marked. Can be NULL.
7174 *
7175 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007176 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007177 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007178set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007179{
7180 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 hashtab_T *cur_ht;
7184 ht_stack_T *ht_stack = NULL;
7185 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007186
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007187 cur_ht = ht;
7188 for (;;)
7189 {
7190 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 /* Mark each item in the hashtab. If the item contains a hashtab
7193 * it is added to ht_stack, if it contains a list it is added to
7194 * list_stack. */
7195 todo = (int)cur_ht->ht_used;
7196 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7197 if (!HASHITEM_EMPTY(hi))
7198 {
7199 --todo;
7200 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7201 &ht_stack, list_stack);
7202 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007203 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007204
7205 if (ht_stack == NULL)
7206 break;
7207
7208 /* take an item from the stack */
7209 cur_ht = ht_stack->ht;
7210 tempitem = ht_stack;
7211 ht_stack = ht_stack->prev;
7212 free(tempitem);
7213 }
7214
7215 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007216}
7217
7218/*
7219 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007220 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7221 *
7222 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007225set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007226{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007227 listitem_T *li;
7228 int abort = FALSE;
7229 list_T *cur_l;
7230 list_stack_T *list_stack = NULL;
7231 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007233 cur_l = l;
7234 for (;;)
7235 {
7236 if (!abort)
7237 /* Mark each item in the list. If the item contains a hashtab
7238 * it is added to ht_stack, if it contains a list it is added to
7239 * list_stack. */
7240 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7241 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7242 ht_stack, &list_stack);
7243 if (list_stack == NULL)
7244 break;
7245
7246 /* take an item from the stack */
7247 cur_l = list_stack->list;
7248 tempitem = list_stack;
7249 list_stack = list_stack->prev;
7250 free(tempitem);
7251 }
7252
7253 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007254}
7255
7256/*
7257 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007258 * "list_stack" is used to add lists to be marked. Can be NULL.
7259 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7260 *
7261 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007262 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007264set_ref_in_item(
7265 typval_T *tv,
7266 int copyID,
7267 ht_stack_T **ht_stack,
7268 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007269{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007270 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007271
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007272 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007273 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007274 dict_T *dd = tv->vval.v_dict;
7275
Bram Moolenaara03f2332016-02-06 18:09:59 +01007276 if (dd != NULL && dd->dv_copyID != copyID)
7277 {
7278 /* Didn't see this dict yet. */
7279 dd->dv_copyID = copyID;
7280 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007281 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007282 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7283 }
7284 else
7285 {
7286 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7287 if (newitem == NULL)
7288 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007289 else
7290 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 newitem->ht = &dd->dv_hashtab;
7292 newitem->prev = *ht_stack;
7293 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007294 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007295 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007296 }
7297 }
7298 else if (tv->v_type == VAR_LIST)
7299 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007300 list_T *ll = tv->vval.v_list;
7301
Bram Moolenaara03f2332016-02-06 18:09:59 +01007302 if (ll != NULL && ll->lv_copyID != copyID)
7303 {
7304 /* Didn't see this list yet. */
7305 ll->lv_copyID = copyID;
7306 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007307 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007308 abort = set_ref_in_list(ll, copyID, ht_stack);
7309 }
7310 else
7311 {
7312 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007313 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 if (newitem == NULL)
7315 abort = TRUE;
7316 else
7317 {
7318 newitem->list = ll;
7319 newitem->prev = *list_stack;
7320 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007321 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007322 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007323 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007324 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007325 else if (tv->v_type == VAR_PARTIAL)
7326 {
7327 partial_T *pt = tv->vval.v_partial;
7328 int i;
7329
7330 /* A partial does not have a copyID, because it cannot contain itself.
7331 */
7332 if (pt != NULL)
7333 {
7334 if (pt->pt_dict != NULL)
7335 {
7336 typval_T dtv;
7337
7338 dtv.v_type = VAR_DICT;
7339 dtv.vval.v_dict = pt->pt_dict;
7340 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7341 }
7342
7343 for (i = 0; i < pt->pt_argc; ++i)
7344 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7345 ht_stack, list_stack);
7346 }
7347 }
7348#ifdef FEAT_JOB_CHANNEL
7349 else if (tv->v_type == VAR_JOB)
7350 {
7351 job_T *job = tv->vval.v_job;
7352 typval_T dtv;
7353
7354 if (job != NULL && job->jv_copyID != copyID)
7355 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007356 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007357 if (job->jv_channel != NULL)
7358 {
7359 dtv.v_type = VAR_CHANNEL;
7360 dtv.vval.v_channel = job->jv_channel;
7361 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7362 }
7363 if (job->jv_exit_partial != NULL)
7364 {
7365 dtv.v_type = VAR_PARTIAL;
7366 dtv.vval.v_partial = job->jv_exit_partial;
7367 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7368 }
7369 }
7370 }
7371 else if (tv->v_type == VAR_CHANNEL)
7372 {
7373 channel_T *ch =tv->vval.v_channel;
7374 int part;
7375 typval_T dtv;
7376 jsonq_T *jq;
7377 cbq_T *cq;
7378
7379 if (ch != NULL && ch->ch_copyID != copyID)
7380 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007381 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007382 for (part = PART_SOCK; part <= PART_IN; ++part)
7383 {
7384 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7385 jq = jq->jq_next)
7386 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7387 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7388 cq = cq->cq_next)
7389 if (cq->cq_partial != NULL)
7390 {
7391 dtv.v_type = VAR_PARTIAL;
7392 dtv.vval.v_partial = cq->cq_partial;
7393 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7394 }
7395 if (ch->ch_part[part].ch_partial != NULL)
7396 {
7397 dtv.v_type = VAR_PARTIAL;
7398 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7399 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7400 }
7401 }
7402 if (ch->ch_partial != NULL)
7403 {
7404 dtv.v_type = VAR_PARTIAL;
7405 dtv.vval.v_partial = ch->ch_partial;
7406 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7407 }
7408 if (ch->ch_close_partial != NULL)
7409 {
7410 dtv.v_type = VAR_PARTIAL;
7411 dtv.vval.v_partial = ch->ch_close_partial;
7412 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7413 }
7414 }
7415 }
7416#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007417 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007418}
7419
7420/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007421 * Allocate an empty header for a dictionary.
7422 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007423 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007425{
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007431 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007432 if (first_dict != NULL)
7433 first_dict->dv_used_prev = d;
7434 d->dv_used_next = first_dict;
7435 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007436 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007437
Bram Moolenaar33570922005-01-25 22:26:29 +00007438 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007439 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007440 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007441 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007442 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007443 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007445}
7446
7447/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007448 * Allocate an empty dict for a return value.
7449 * Returns OK or FAIL.
7450 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007453{
7454 dict_T *d = dict_alloc();
7455
7456 if (d == NULL)
7457 return FAIL;
7458
7459 rettv->vval.v_dict = d;
7460 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007461 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007462 ++d->dv_refcount;
7463 return OK;
7464}
7465
7466
7467/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 * Unreference a Dictionary: decrement the reference count and free it when it
7469 * becomes zero.
7470 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007472dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007473{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007474 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007475 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476}
7477
7478/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007479 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007480 * Ignores the reference count.
7481 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007482 static void
7483dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007486 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007487 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007489 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007490 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007491 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007492 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 if (!HASHITEM_EMPTY(hi))
7495 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007496 /* Remove the item before deleting it, just in case there is
7497 * something recursive causing trouble. */
7498 di = HI2DI(hi);
7499 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007500 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007501 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007502 --todo;
7503 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007504 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007506}
7507
7508 static void
7509dict_free_dict(dict_T *d)
7510{
7511 /* Remove the dict from the list of dicts for garbage collection. */
7512 if (d->dv_used_prev == NULL)
7513 first_dict = d->dv_used_next;
7514 else
7515 d->dv_used_prev->dv_used_next = d->dv_used_next;
7516 if (d->dv_used_next != NULL)
7517 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 vim_free(d);
7519}
7520
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007521 void
7522dict_free(dict_T *d)
7523{
7524 if (!in_free_unref_items)
7525 {
7526 dict_free_contents(d);
7527 dict_free_dict(d);
7528 }
7529}
7530
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531/*
7532 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 * The "key" is copied to the new item.
7534 * Note that the value of the item "di_tv" still needs to be initialized!
7535 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007536 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007537 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007538dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007539{
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007541
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007542 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007544 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007545 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007546 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007547 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007548 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007549}
7550
7551/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 * Make a copy of a Dictionary item.
7553 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007554 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556{
Bram Moolenaar33570922005-01-25 22:26:29 +00007557 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007558
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007559 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7560 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 if (di != NULL)
7562 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007563 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007564 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565 copy_tv(&org->di_tv, &di->di_tv);
7566 }
7567 return di;
7568}
7569
7570/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007571 * Remove item "item" from Dictionary "dict" and free it.
7572 */
7573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007574dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007575{
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007577
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007579 if (HASHITEM_EMPTY(hi))
7580 EMSG2(_(e_intern2), "dictitem_remove()");
7581 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007583 dictitem_free(item);
7584}
7585
7586/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 * Free a dict item. Also clears the value.
7588 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007590dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007593 if (item->di_flags & DI_FLAGS_ALLOC)
7594 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007595}
7596
7597/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7599 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007600 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601 * Returns NULL when out of memory.
7602 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007603 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007605{
Bram Moolenaar33570922005-01-25 22:26:29 +00007606 dict_T *copy;
7607 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007608 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007609 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610
7611 if (orig == NULL)
7612 return NULL;
7613
7614 copy = dict_alloc();
7615 if (copy != NULL)
7616 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007617 if (copyID != 0)
7618 {
7619 orig->dv_copyID = copyID;
7620 orig->dv_copydict = copy;
7621 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007622 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007623 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007624 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007625 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007627 --todo;
7628
7629 di = dictitem_alloc(hi->hi_key);
7630 if (di == NULL)
7631 break;
7632 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007633 {
7634 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7635 copyID) == FAIL)
7636 {
7637 vim_free(di);
7638 break;
7639 }
7640 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641 else
7642 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7643 if (dict_add(copy, di) == FAIL)
7644 {
7645 dictitem_free(di);
7646 break;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650
Bram Moolenaare9a41262005-01-15 22:18:47 +00007651 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007652 if (todo > 0)
7653 {
7654 dict_unref(copy);
7655 copy = NULL;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 }
7658
7659 return copy;
7660}
7661
7662/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007664 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007667dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668{
Bram Moolenaar33570922005-01-25 22:26:29 +00007669 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670}
7671
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007673 * Add a number or string entry to dictionary "d".
7674 * When "str" is NULL use number "nr", otherwise use "str".
7675 * Returns FAIL when out of memory and when key already exists.
7676 */
7677 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678dict_add_nr_str(
7679 dict_T *d,
7680 char *key,
7681 long nr,
7682 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007683{
7684 dictitem_T *item;
7685
7686 item = dictitem_alloc((char_u *)key);
7687 if (item == NULL)
7688 return FAIL;
7689 item->di_tv.v_lock = 0;
7690 if (str == NULL)
7691 {
7692 item->di_tv.v_type = VAR_NUMBER;
7693 item->di_tv.vval.v_number = nr;
7694 }
7695 else
7696 {
7697 item->di_tv.v_type = VAR_STRING;
7698 item->di_tv.vval.v_string = vim_strsave(str);
7699 }
7700 if (dict_add(d, item) == FAIL)
7701 {
7702 dictitem_free(item);
7703 return FAIL;
7704 }
7705 return OK;
7706}
7707
7708/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007709 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007710 * Returns FAIL when out of memory and when key already exists.
7711 */
7712 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007713dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007714{
7715 dictitem_T *item;
7716
7717 item = dictitem_alloc((char_u *)key);
7718 if (item == NULL)
7719 return FAIL;
7720 item->di_tv.v_lock = 0;
7721 item->di_tv.v_type = VAR_LIST;
7722 item->di_tv.vval.v_list = list;
7723 if (dict_add(d, item) == FAIL)
7724 {
7725 dictitem_free(item);
7726 return FAIL;
7727 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007728 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007729 return OK;
7730}
7731
7732/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007733 * Get the number of items in a Dictionary.
7734 */
7735 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007736dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007737{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007738 if (d == NULL)
7739 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007740 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741}
7742
7743/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 * Find item "key[len]" in Dictionary "d".
7745 * If "len" is negative use strlen(key).
7746 * Returns NULL when not found.
7747 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007748 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007749dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751#define AKEYLEN 200
7752 char_u buf[AKEYLEN];
7753 char_u *akey;
7754 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007755 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007757 if (len < 0)
7758 akey = key;
7759 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007761 tofree = akey = vim_strnsave(key, len);
7762 if (akey == NULL)
7763 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007764 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007765 else
7766 {
7767 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007768 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007769 akey = buf;
7770 }
7771
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007773 vim_free(tofree);
7774 if (HASHITEM_EMPTY(hi))
7775 return NULL;
7776 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777}
7778
7779/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007780 * Get a string item from a dictionary.
7781 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007782 * Returns NULL if the entry doesn't exist or out of memory.
7783 */
7784 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007785get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007786{
7787 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007788 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007789
7790 di = dict_find(d, key, -1);
7791 if (di == NULL)
7792 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007793 s = get_tv_string(&di->di_tv);
7794 if (save && s != NULL)
7795 s = vim_strsave(s);
7796 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007797}
7798
7799/*
7800 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007801 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007802 */
7803 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007804get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007805{
7806 dictitem_T *di;
7807
7808 di = dict_find(d, key, -1);
7809 if (di == NULL)
7810 return 0;
7811 return get_tv_number(&di->di_tv);
7812}
7813
7814/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007815 * Return an allocated string with the string representation of a Dictionary.
7816 * May return NULL.
7817 */
7818 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007819dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820{
7821 garray_T ga;
7822 int first = TRUE;
7823 char_u *tofree;
7824 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007826 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007827 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007828 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007830 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007831 return NULL;
7832 ga_init2(&ga, (int)sizeof(char), 80);
7833 ga_append(&ga, '{');
7834
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007835 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007836 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007837 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007838 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007839 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007840 --todo;
7841
7842 if (first)
7843 first = FALSE;
7844 else
7845 ga_concat(&ga, (char_u *)", ");
7846
7847 tofree = string_quote(hi->hi_key, FALSE);
7848 if (tofree != NULL)
7849 {
7850 ga_concat(&ga, tofree);
7851 vim_free(tofree);
7852 }
7853 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007854 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007855 if (s != NULL)
7856 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007857 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007858 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007859 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007860 line_breakcheck();
7861
Bram Moolenaar8c711452005-01-14 21:53:12 +00007862 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007863 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007864 if (todo > 0)
7865 {
7866 vim_free(ga.ga_data);
7867 return NULL;
7868 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007869
7870 ga_append(&ga, '}');
7871 ga_append(&ga, NUL);
7872 return (char_u *)ga.ga_data;
7873}
7874
7875/*
7876 * Allocate a variable for a Dictionary and fill it from "*arg".
7877 * Return OK or FAIL. Returns NOTDONE for {expr}.
7878 */
7879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007880get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007881{
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 dict_T *d = NULL;
7883 typval_T tvkey;
7884 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007885 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007888 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007889
7890 /*
7891 * First check if it's not a curly-braces thing: {expr}.
7892 * Must do this without evaluating, otherwise a function may be called
7893 * twice. Unfortunately this means we need to call eval1() twice for the
7894 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897 if (*start != '}')
7898 {
7899 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7900 return FAIL;
7901 if (*start == '}')
7902 return NOTDONE;
7903 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904
7905 if (evaluate)
7906 {
7907 d = dict_alloc();
7908 if (d == NULL)
7909 return FAIL;
7910 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007911 tvkey.v_type = VAR_UNKNOWN;
7912 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913
7914 *arg = skipwhite(*arg + 1);
7915 while (**arg != '}' && **arg != NUL)
7916 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007917 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007918 goto failret;
7919 if (**arg != ':')
7920 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007921 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007922 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007923 goto failret;
7924 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007925 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007927 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007928 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007929 {
7930 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007931 clear_tv(&tvkey);
7932 goto failret;
7933 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935
7936 *arg = skipwhite(*arg + 1);
7937 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7938 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007939 if (evaluate)
7940 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 goto failret;
7942 }
7943 if (evaluate)
7944 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007945 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 if (item != NULL)
7947 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007948 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007949 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 clear_tv(&tv);
7951 goto failret;
7952 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007953 item = dictitem_alloc(key);
7954 clear_tv(&tvkey);
7955 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007957 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007958 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007959 if (dict_add(d, item) == FAIL)
7960 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007961 }
7962 }
7963
7964 if (**arg == '}')
7965 break;
7966 if (**arg != ',')
7967 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007968 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007969 goto failret;
7970 }
7971 *arg = skipwhite(*arg + 1);
7972 }
7973
7974 if (**arg != '}')
7975 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007976 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007977failret:
7978 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007979 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 return FAIL;
7981 }
7982
7983 *arg = skipwhite(*arg + 1);
7984 if (evaluate)
7985 {
7986 rettv->v_type = VAR_DICT;
7987 rettv->vval.v_dict = d;
7988 ++d->dv_refcount;
7989 }
7990
7991 return OK;
7992}
7993
Bram Moolenaar17a13432016-01-24 14:22:10 +01007994 static char *
7995get_var_special_name(int nr)
7996{
7997 switch (nr)
7998 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007999 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008000 case VVAL_TRUE: return "v:true";
8001 case VVAL_NONE: return "v:none";
8002 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008003 }
8004 EMSG2(_(e_intern2), "get_var_special_name()");
8005 return "42";
8006}
8007
Bram Moolenaar8c711452005-01-14 21:53:12 +00008008/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009 * Return a string with the string representation of a variable.
8010 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008011 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008012 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008013 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008014 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015 */
8016 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008017echo_string(
8018 typval_T *tv,
8019 char_u **tofree,
8020 char_u *numbuf,
8021 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 static int recurse = 0;
8024 char_u *r = NULL;
8025
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008028 if (!did_echo_string_emsg)
8029 {
8030 /* Only give this message once for a recursive call to avoid
8031 * flooding the user with errors. And stop iterating over lists
8032 * and dicts. */
8033 did_echo_string_emsg = TRUE;
8034 EMSG(_("E724: variable nested too deep for displaying"));
8035 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008037 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008038 }
8039 ++recurse;
8040
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008041 switch (tv->v_type)
8042 {
8043 case VAR_FUNC:
8044 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 r = tv->vval.v_string;
8046 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008047
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008048 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008049 {
8050 partial_T *pt = tv->vval.v_partial;
8051 char_u *fname = string_quote(pt == NULL ? NULL
8052 : pt->pt_name, FALSE);
8053 garray_T ga;
8054 int i;
8055 char_u *tf;
8056
8057 ga_init2(&ga, 1, 100);
8058 ga_concat(&ga, (char_u *)"function(");
8059 if (fname != NULL)
8060 {
8061 ga_concat(&ga, fname);
8062 vim_free(fname);
8063 }
8064 if (pt != NULL && pt->pt_argc > 0)
8065 {
8066 ga_concat(&ga, (char_u *)", [");
8067 for (i = 0; i < pt->pt_argc; ++i)
8068 {
8069 if (i > 0)
8070 ga_concat(&ga, (char_u *)", ");
8071 ga_concat(&ga,
8072 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8073 vim_free(tf);
8074 }
8075 ga_concat(&ga, (char_u *)"]");
8076 }
8077 if (pt != NULL && pt->pt_dict != NULL)
8078 {
8079 typval_T dtv;
8080
8081 ga_concat(&ga, (char_u *)", ");
8082 dtv.v_type = VAR_DICT;
8083 dtv.vval.v_dict = pt->pt_dict;
8084 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8085 vim_free(tf);
8086 }
8087 ga_concat(&ga, (char_u *)")");
8088
8089 *tofree = ga.ga_data;
8090 r = *tofree;
8091 break;
8092 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008093
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008094 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008095 if (tv->vval.v_list == NULL)
8096 {
8097 *tofree = NULL;
8098 r = NULL;
8099 }
8100 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8101 {
8102 *tofree = NULL;
8103 r = (char_u *)"[...]";
8104 }
8105 else
8106 {
8107 tv->vval.v_list->lv_copyID = copyID;
8108 *tofree = list2string(tv, copyID);
8109 r = *tofree;
8110 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008111 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008112
Bram Moolenaar8c711452005-01-14 21:53:12 +00008113 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008114 if (tv->vval.v_dict == NULL)
8115 {
8116 *tofree = NULL;
8117 r = NULL;
8118 }
8119 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8120 {
8121 *tofree = NULL;
8122 r = (char_u *)"{...}";
8123 }
8124 else
8125 {
8126 tv->vval.v_dict->dv_copyID = copyID;
8127 *tofree = dict2string(tv, copyID);
8128 r = *tofree;
8129 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008130 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 case VAR_STRING:
8133 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008134 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008135 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008136 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 *tofree = NULL;
8138 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008140
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008142#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008143 *tofree = NULL;
8144 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8145 r = numbuf;
8146 break;
8147#endif
8148
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008149 case VAR_SPECIAL:
8150 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008151 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008152 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008153 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008154
Bram Moolenaar8502c702014-06-17 12:51:16 +02008155 if (--recurse == 0)
8156 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008157 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008158}
8159
8160/*
8161 * Return a string with the string representation of a variable.
8162 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8163 * "numbuf" is used for a number.
8164 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008165 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008166 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008167 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008168tv2string(
8169 typval_T *tv,
8170 char_u **tofree,
8171 char_u *numbuf,
8172 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008173{
8174 switch (tv->v_type)
8175 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008176 case VAR_FUNC:
8177 *tofree = string_quote(tv->vval.v_string, TRUE);
8178 return *tofree;
8179 case VAR_STRING:
8180 *tofree = string_quote(tv->vval.v_string, FALSE);
8181 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008183#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184 *tofree = NULL;
8185 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8186 return numbuf;
8187#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008188 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008189 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008190 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008191 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008192 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008193 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008194 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008195 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008197 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008198 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008199}
8200
8201/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008202 * Return string "str" in ' quotes, doubling ' characters.
8203 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008204 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008205 */
8206 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008207string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008208{
Bram Moolenaar33570922005-01-25 22:26:29 +00008209 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008210 char_u *p, *r, *s;
8211
Bram Moolenaar33570922005-01-25 22:26:29 +00008212 len = (function ? 13 : 3);
8213 if (str != NULL)
8214 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008215 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008216 for (p = str; *p != NUL; mb_ptr_adv(p))
8217 if (*p == '\'')
8218 ++len;
8219 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008220 s = r = alloc(len);
8221 if (r != NULL)
8222 {
8223 if (function)
8224 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008225 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226 r += 10;
8227 }
8228 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008229 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008230 if (str != NULL)
8231 for (p = str; *p != NUL; )
8232 {
8233 if (*p == '\'')
8234 *r++ = '\'';
8235 MB_COPY_CHAR(p, r);
8236 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 if (function)
8239 *r++ = ')';
8240 *r++ = NUL;
8241 }
8242 return s;
8243}
8244
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008245#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008246/*
8247 * Convert the string "text" to a floating point number.
8248 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8249 * this always uses a decimal point.
8250 * Returns the length of the text that was consumed.
8251 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008252 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008253string2float(
8254 char_u *text,
8255 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008256{
8257 char *s = (char *)text;
8258 float_T f;
8259
8260 f = strtod(s, &s);
8261 *value = f;
8262 return (int)((char_u *)s - text);
8263}
8264#endif
8265
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 * Get the value of an environment variable.
8268 * "arg" is pointing to the '$'. It is advanced to after the name.
8269 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008270 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 */
8272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008273get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 char_u *string = NULL;
8276 int len;
8277 int cc;
8278 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008279 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
8281 ++*arg;
8282 name = *arg;
8283 len = get_env_len(arg);
8284 if (evaluate)
8285 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008286 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008287 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008288
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008289 cc = name[len];
8290 name[len] = NUL;
8291 /* first try vim_getenv(), fast for normal environment vars */
8292 string = vim_getenv(name, &mustfree);
8293 if (string != NULL && *string != NUL)
8294 {
8295 if (!mustfree)
8296 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008298 else
8299 {
8300 if (mustfree)
8301 vim_free(string);
8302
8303 /* next try expanding things like $VIM and ${HOME} */
8304 string = expand_env_save(name - 1);
8305 if (string != NULL && *string == '$')
8306 {
8307 vim_free(string);
8308 string = NULL;
8309 }
8310 }
8311 name[len] = cc;
8312
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008313 rettv->v_type = VAR_STRING;
8314 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 }
8316
8317 return OK;
8318}
8319
8320/*
8321 * Array with names and number of arguments of all internal functions
8322 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8323 */
8324static struct fst
8325{
8326 char *f_name; /* function name */
8327 char f_min_argc; /* minimal number of arguments */
8328 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008329 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008330 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331} functions[] =
8332{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008333#ifdef FEAT_FLOAT
8334 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008335 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008336#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008337 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008338 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008339 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"append", 2, 2, f_append},
8341 {"argc", 0, 0, f_argc},
8342 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008343 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008344 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008345#ifdef FEAT_FLOAT
8346 {"asin", 1, 1, f_asin}, /* WJMc */
8347#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008348 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008349 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008350 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008351 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008352 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008353 {"assert_notequal", 2, 3, f_assert_notequal},
8354 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008355 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008356#ifdef FEAT_FLOAT
8357 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008358 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008361 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 {"bufexists", 1, 1, f_bufexists},
8363 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8364 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8365 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8366 {"buflisted", 1, 1, f_buflisted},
8367 {"bufloaded", 1, 1, f_bufloaded},
8368 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008369 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 {"bufwinnr", 1, 1, f_bufwinnr},
8371 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008372 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008373 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008374 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008375#ifdef FEAT_FLOAT
8376 {"ceil", 1, 1, f_ceil},
8377#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008378#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008379 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008380 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8381 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008382 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008383 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008384 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008385 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008386 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008387 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008388 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008389 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008390 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8391 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008392 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008393 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008394#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008395 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008396 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008398 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008400#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008401 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008402 {"complete_add", 1, 1, f_complete_add},
8403 {"complete_check", 0, 0, f_complete_check},
8404#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008406 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008407#ifdef FEAT_FLOAT
8408 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008409 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008410#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008411 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008413 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008414 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008415 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008417 {"diff_filler", 1, 1, f_diff_filler},
8418 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008419 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008420 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008422 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"eventhandler", 0, 0, f_eventhandler},
8424 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008425 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008427#ifdef FEAT_FLOAT
8428 {"exp", 1, 1, f_exp},
8429#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008430 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008431 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008432 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8434 {"filereadable", 1, 1, f_filereadable},
8435 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008436 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008437 {"finddir", 1, 3, f_finddir},
8438 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008439#ifdef FEAT_FLOAT
8440 {"float2nr", 1, 1, f_float2nr},
8441 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008442 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008443#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008444 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"fnamemodify", 2, 2, f_fnamemodify},
8446 {"foldclosed", 1, 1, f_foldclosed},
8447 {"foldclosedend", 1, 1, f_foldclosedend},
8448 {"foldlevel", 1, 1, f_foldlevel},
8449 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008450 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008452 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008453 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008454 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008455 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008456 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008457 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"getchar", 0, 1, f_getchar},
8459 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008460 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"getcmdline", 0, 0, f_getcmdline},
8462 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008463 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008464 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008465 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008466 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008467 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008468 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"getfsize", 1, 1, f_getfsize},
8470 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008471 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008472 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008473 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008474 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008475 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008476 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008477 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008478 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008480 {"gettabvar", 2, 3, f_gettabvar},
8481 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"getwinposx", 0, 0, f_getwinposx},
8483 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008484 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008485 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008486 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008487 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008489 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008490 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008491 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8493 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8494 {"histadd", 2, 2, f_histadd},
8495 {"histdel", 1, 2, f_histdel},
8496 {"histget", 1, 2, f_histget},
8497 {"histnr", 1, 1, f_histnr},
8498 {"hlID", 1, 1, f_hlID},
8499 {"hlexists", 1, 1, f_hlexists},
8500 {"hostname", 0, 0, f_hostname},
8501 {"iconv", 3, 3, f_iconv},
8502 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008503 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008504 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008506 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"inputrestore", 0, 0, f_inputrestore},
8508 {"inputsave", 0, 0, f_inputsave},
8509 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008510 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008511 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008513 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008514#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8515 {"isnan", 1, 1, f_isnan},
8516#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008517 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008518#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008519 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008520 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008521 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008522 {"job_start", 1, 2, f_job_start},
8523 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008524 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008525#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008526 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008527 {"js_decode", 1, 1, f_js_decode},
8528 {"js_encode", 1, 1, f_js_encode},
8529 {"json_decode", 1, 1, f_json_decode},
8530 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008531 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"libcall", 3, 3, f_libcall},
8535 {"libcallnr", 3, 3, f_libcallnr},
8536 {"line", 1, 1, f_line},
8537 {"line2byte", 1, 1, f_line2byte},
8538 {"lispindent", 1, 1, f_lispindent},
8539 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008540#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008541 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008542 {"log10", 1, 1, f_log10},
8543#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008544#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008545 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008546#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008547 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008548 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008549 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008550 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008551 {"matchadd", 2, 5, f_matchadd},
8552 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008553 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008554 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008555 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008556 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008557 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008558 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008559 {"max", 1, 1, f_max},
8560 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008561#ifdef vim_mkdir
8562 {"mkdir", 1, 3, f_mkdir},
8563#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008564 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008565#ifdef FEAT_MZSCHEME
8566 {"mzeval", 1, 1, f_mzeval},
8567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008569 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008570 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008571 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008572#ifdef FEAT_PERL
8573 {"perleval", 1, 1, f_perleval},
8574#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008575#ifdef FEAT_FLOAT
8576 {"pow", 2, 2, f_pow},
8577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008579 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008580 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008581#ifdef FEAT_PYTHON3
8582 {"py3eval", 1, 1, f_py3eval},
8583#endif
8584#ifdef FEAT_PYTHON
8585 {"pyeval", 1, 1, f_pyeval},
8586#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008587 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008588 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008589 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008590#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008591 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008592#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008593 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 {"remote_expr", 2, 3, f_remote_expr},
8595 {"remote_foreground", 1, 1, f_remote_foreground},
8596 {"remote_peek", 1, 2, f_remote_peek},
8597 {"remote_read", 1, 1, f_remote_read},
8598 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008599 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008601 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008603 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008604#ifdef FEAT_FLOAT
8605 {"round", 1, 1, f_round},
8606#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008607 {"screenattr", 2, 2, f_screenattr},
8608 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008609 {"screencol", 0, 0, f_screencol},
8610 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008611 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008612 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008613 {"searchpair", 3, 7, f_searchpair},
8614 {"searchpairpos", 3, 7, f_searchpairpos},
8615 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {"server2client", 2, 2, f_server2client},
8617 {"serverlist", 0, 0, f_serverlist},
8618 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008619 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008621 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008623 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008624 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008625 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008626 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008628 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008629 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008631#ifdef FEAT_CRYPT
8632 {"sha256", 1, 1, f_sha256},
8633#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008634 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008635 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637#ifdef FEAT_FLOAT
8638 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008639 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008640#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008641 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008642 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008643 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008644 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008645 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008646#ifdef FEAT_FLOAT
8647 {"sqrt", 1, 1, f_sqrt},
8648 {"str2float", 1, 1, f_str2float},
8649#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008650 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008651 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008652 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008653 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654#ifdef HAVE_STRFTIME
8655 {"strftime", 1, 2, f_strftime},
8656#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008657 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 {"strlen", 1, 1, f_strlen},
8661 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008662 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008664 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008665 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 {"substitute", 4, 4, f_substitute},
8667 {"synID", 3, 3, f_synID},
8668 {"synIDattr", 2, 3, f_synIDattr},
8669 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008670 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008671 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008672 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008673 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008674 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008675 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008676 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008677 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008678 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008679#ifdef FEAT_FLOAT
8680 {"tan", 1, 1, f_tan},
8681 {"tanh", 1, 1, f_tanh},
8682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008684 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008685#ifdef FEAT_TIMERS
8686 {"timer_start", 2, 3, f_timer_start},
8687 {"timer_stop", 1, 1, f_timer_stop},
8688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 {"tolower", 1, 1, f_tolower},
8690 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008691 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008692#ifdef FEAT_FLOAT
8693 {"trunc", 1, 1, f_trunc},
8694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008696 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008697 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008698 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008699 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 {"virtcol", 1, 1, f_virtcol},
8701 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008702 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008703 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008704 {"win_getid", 0, 2, f_win_getid},
8705 {"win_gotoid", 1, 1, f_win_gotoid},
8706 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8707 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {"winbufnr", 1, 1, f_winbufnr},
8709 {"wincol", 0, 0, f_wincol},
8710 {"winheight", 1, 1, f_winheight},
8711 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008712 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008714 {"winrestview", 1, 1, f_winrestview},
8715 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008717 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008718 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008719 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720};
8721
8722#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8723
8724/*
8725 * Function given to ExpandGeneric() to obtain the list of internal
8726 * or user defined function names.
8727 */
8728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008729get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
8731 static int intidx = -1;
8732 char_u *name;
8733
8734 if (idx == 0)
8735 intidx = -1;
8736 if (intidx < 0)
8737 {
8738 name = get_user_func_name(xp, idx);
8739 if (name != NULL)
8740 return name;
8741 }
8742 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8743 {
8744 STRCPY(IObuff, functions[intidx].f_name);
8745 STRCAT(IObuff, "(");
8746 if (functions[intidx].f_max_argc == 0)
8747 STRCAT(IObuff, ")");
8748 return IObuff;
8749 }
8750
8751 return NULL;
8752}
8753
8754/*
8755 * Function given to ExpandGeneric() to obtain the list of internal or
8756 * user defined variable or function names.
8757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008759get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 static int intidx = -1;
8762 char_u *name;
8763
8764 if (idx == 0)
8765 intidx = -1;
8766 if (intidx < 0)
8767 {
8768 name = get_function_name(xp, idx);
8769 if (name != NULL)
8770 return name;
8771 }
8772 return get_user_var_name(xp, ++intidx);
8773}
8774
8775#endif /* FEAT_CMDL_COMPL */
8776
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008777#if defined(EBCDIC) || defined(PROTO)
8778/*
8779 * Compare struct fst by function name.
8780 */
8781 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008782compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008783{
8784 struct fst *p1 = (struct fst *)s1;
8785 struct fst *p2 = (struct fst *)s2;
8786
8787 return STRCMP(p1->f_name, p2->f_name);
8788}
8789
8790/*
8791 * Sort the function table by function name.
8792 * The sorting of the table above is ASCII dependant.
8793 * On machines using EBCDIC we have to sort it.
8794 */
8795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008796sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008797{
8798 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8799
8800 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8801}
8802#endif
8803
8804
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805/*
8806 * Find internal function in table above.
8807 * Return index, or -1 if not found
8808 */
8809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008810find_internal_func(
8811 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812{
8813 int first = 0;
8814 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8815 int cmp;
8816 int x;
8817
8818 /*
8819 * Find the function name in the table. Binary search.
8820 */
8821 while (first <= last)
8822 {
8823 x = first + ((unsigned)(last - first) >> 1);
8824 cmp = STRCMP(name, functions[x].f_name);
8825 if (cmp < 0)
8826 last = x - 1;
8827 else if (cmp > 0)
8828 first = x + 1;
8829 else
8830 return x;
8831 }
8832 return -1;
8833}
8834
8835/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008836 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8837 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008838 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8839 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008840 */
8841 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008842deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008843{
Bram Moolenaar33570922005-01-25 22:26:29 +00008844 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008845 int cc;
8846
Bram Moolenaar65639032016-03-16 21:40:30 +01008847 if (partialp != NULL)
8848 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008849
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008850 cc = name[*lenp];
8851 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008852 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008853 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008854 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008855 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008856 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008857 {
8858 *lenp = 0;
8859 return (char_u *)""; /* just in case */
8860 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008861 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008863 }
8864
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008865 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8866 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008867 partial_T *pt = v->di_tv.vval.v_partial;
8868
8869 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008870 {
8871 *lenp = 0;
8872 return (char_u *)""; /* just in case */
8873 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008874 if (partialp != NULL)
8875 *partialp = pt;
8876 *lenp = (int)STRLEN(pt->pt_name);
8877 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008878 }
8879
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008880 return name;
8881}
8882
8883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 * Allocate a variable for the result of a function.
8885 * Return OK or FAIL.
8886 */
8887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008888get_func_tv(
8889 char_u *name, /* name of the function */
8890 int len, /* length of "name" */
8891 typval_T *rettv,
8892 char_u **arg, /* argument, pointing to the '(' */
8893 linenr_T firstline, /* first line of range */
8894 linenr_T lastline, /* last line of range */
8895 int *doesrange, /* return: function handled range */
8896 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008897 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008898 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 char_u *argp;
8901 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008902 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 int argcount = 0; /* number of arguments found */
8904
8905 /*
8906 * Get the arguments.
8907 */
8908 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008909 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 {
8911 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8912 if (*argp == ')' || *argp == ',' || *argp == NUL)
8913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8915 {
8916 ret = FAIL;
8917 break;
8918 }
8919 ++argcount;
8920 if (*argp != ',')
8921 break;
8922 }
8923 if (*argp == ')')
8924 ++argp;
8925 else
8926 ret = FAIL;
8927
8928 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008929 {
8930 int i = 0;
8931
8932 if (get_vim_var_nr(VV_TESTING))
8933 {
8934 /* Prepare for calling garbagecollect_for_testing(), need to know
8935 * what variables are used on the call stack. */
8936 if (funcargs.ga_itemsize == 0)
8937 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8938 for (i = 0; i < argcount; ++i)
8939 if (ga_grow(&funcargs, 1) == OK)
8940 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8941 &argvars[i];
8942 }
8943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008945 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008946
8947 funcargs.ga_len -= i;
8948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008950 {
8951 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008952 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008953 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008954 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956
8957 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959
8960 *arg = skipwhite(argp);
8961 return ret;
8962}
8963
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008964#define ERROR_UNKNOWN 0
8965#define ERROR_TOOMANY 1
8966#define ERROR_TOOFEW 2
8967#define ERROR_SCRIPT 3
8968#define ERROR_DICT 4
8969#define ERROR_NONE 5
8970#define ERROR_OTHER 6
8971#define FLEN_FIXED 40
8972
8973/*
8974 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8975 * Change <SNR>123_name() to K_SNR 123_name().
8976 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8977 * (slow).
8978 */
8979 static char_u *
8980fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8981{
8982 int llen;
8983 char_u *fname;
8984 int i;
8985
8986 llen = eval_fname_script(name);
8987 if (llen > 0)
8988 {
8989 fname_buf[0] = K_SPECIAL;
8990 fname_buf[1] = KS_EXTRA;
8991 fname_buf[2] = (int)KE_SNR;
8992 i = 3;
8993 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8994 {
8995 if (current_SID <= 0)
8996 *error = ERROR_SCRIPT;
8997 else
8998 {
8999 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9000 i = (int)STRLEN(fname_buf);
9001 }
9002 }
9003 if (i + STRLEN(name + llen) < FLEN_FIXED)
9004 {
9005 STRCPY(fname_buf + i, name + llen);
9006 fname = fname_buf;
9007 }
9008 else
9009 {
9010 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9011 if (fname == NULL)
9012 *error = ERROR_OTHER;
9013 else
9014 {
9015 *tofree = fname;
9016 mch_memmove(fname, fname_buf, (size_t)i);
9017 STRCPY(fname + i, name + llen);
9018 }
9019 }
9020 }
9021 else
9022 fname = name;
9023 return fname;
9024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025
9026/*
9027 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009028 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009029 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009031 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009032call_func(
9033 char_u *funcname, /* name of the function */
9034 int len, /* length of "name" */
9035 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009036 int argcount_in, /* number of "argvars" */
9037 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009038 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009039 linenr_T firstline, /* first line of range */
9040 linenr_T lastline, /* last line of range */
9041 int *doesrange, /* return: function handled range */
9042 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009043 partial_T *partial, /* optional, can be NULL */
9044 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045{
9046 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 int error = ERROR_NONE;
9048 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009051 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009053 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 int argcount = argcount_in;
9055 typval_T *argvars = argvars_in;
9056 dict_T *selfdict = selfdict_in;
9057 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9058 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009059
9060 /* Make a copy of the name, if it comes from a funcref variable it could
9061 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009062 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009063 if (name == NULL)
9064 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009066 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
9068 *doesrange = FALSE;
9069
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009070 if (partial != NULL)
9071 {
9072 if (partial->pt_dict != NULL)
9073 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009074 /* When the function has a partial with a dict and there is a dict
9075 * argument, use the dict argument. That is backwards compatible.
9076 */
9077 if (selfdict_in == NULL)
9078 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009079 }
9080 if (error == ERROR_NONE && partial->pt_argc > 0)
9081 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009082 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9083 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9084 for (i = 0; i < argcount_in; ++i)
9085 argv[i + argv_clear] = argvars_in[i];
9086 argvars = argv;
9087 argcount = partial->pt_argc + argcount_in;
9088 }
9089 }
9090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091
9092 /* execute the function if no errors detected and executing */
9093 if (evaluate && error == ERROR_NONE)
9094 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009095 char_u *rfname = fname;
9096
9097 /* Ignore "g:" before a function name. */
9098 if (fname[0] == 'g' && fname[1] == ':')
9099 rfname = fname + 2;
9100
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009101 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9102 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 error = ERROR_UNKNOWN;
9104
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009105 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 {
9107 /*
9108 * User defined function.
9109 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009110 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009111
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009113 /* Trigger FuncUndefined event, may load the function. */
9114 if (fp == NULL
9115 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009116 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009117 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009119 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009120 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 }
9122#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009123 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009124 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009125 {
9126 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009127 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009128 }
9129
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 if (fp != NULL)
9131 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009132 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009134 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009136 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009138 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009139 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140 else
9141 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009142 int did_save_redo = FALSE;
9143
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 /*
9145 * Call the user function.
9146 * Save and restore search patterns, script variables and
9147 * redo buffer.
9148 */
9149 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009150#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009151 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009152#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009153 {
9154 saveRedobuff();
9155 did_save_redo = TRUE;
9156 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009157 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009159 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009160 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9161 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9162 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009163 /* Function was unreferenced while being used, free it
9164 * now. */
9165 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009166 if (did_save_redo)
9167 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 restore_search_patterns();
9169 error = ERROR_NONE;
9170 }
9171 }
9172 }
9173 else
9174 {
9175 /*
9176 * Find the function name in the table, call its implementation.
9177 */
9178 i = find_internal_func(fname);
9179 if (i >= 0)
9180 {
9181 if (argcount < functions[i].f_min_argc)
9182 error = ERROR_TOOFEW;
9183 else if (argcount > functions[i].f_max_argc)
9184 error = ERROR_TOOMANY;
9185 else
9186 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009187 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009188 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 error = ERROR_NONE;
9190 }
9191 }
9192 }
9193 /*
9194 * The function call (or "FuncUndefined" autocommand sequence) might
9195 * have been aborted by an error, an interrupt, or an explicitly thrown
9196 * exception that has not been caught so far. This situation can be
9197 * tested for by calling aborting(). For an error in an internal
9198 * function or for the "E132" error in call_user_func(), however, the
9199 * throw point at which the "force_abort" flag (temporarily reset by
9200 * emsg()) is normally updated has not been reached yet. We need to
9201 * update that flag first to make aborting() reliable.
9202 */
9203 update_force_abort();
9204 }
9205 if (error == ERROR_NONE)
9206 ret = OK;
9207
9208 /*
9209 * Report an error unless the argument evaluation or function call has been
9210 * cancelled due to an aborting error, an interrupt, or an exception.
9211 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009212 if (!aborting())
9213 {
9214 switch (error)
9215 {
9216 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009217 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009218 break;
9219 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009220 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009221 break;
9222 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009223 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009224 name);
9225 break;
9226 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009227 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009228 name);
9229 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009230 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009231 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009232 name);
9233 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009234 }
9235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009237 while (argv_clear > 0)
9238 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009239 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009240 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 return ret;
9243}
9244
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009245/*
9246 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009247 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009248 */
9249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009250emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009251{
9252 char_u *p;
9253
9254 if (*name == K_SPECIAL)
9255 p = concat_str((char_u *)"<SNR>", name + 3);
9256 else
9257 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009258 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009259 if (p != name)
9260 vim_free(p);
9261}
9262
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009263/*
9264 * Return TRUE for a non-zero Number and a non-empty String.
9265 */
9266 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009267non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009268{
9269 return ((argvars[0].v_type == VAR_NUMBER
9270 && argvars[0].vval.v_number != 0)
9271 || (argvars[0].v_type == VAR_STRING
9272 && argvars[0].vval.v_string != NULL
9273 && *argvars[0].vval.v_string != NUL));
9274}
9275
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276/*********************************************
9277 * Implementation of the built-in functions
9278 */
9279
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009280#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009281static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009282
9283/*
9284 * Get the float value of "argvars[0]" into "f".
9285 * Returns FAIL when the argument is not a Number or Float.
9286 */
9287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009288get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009289{
9290 if (argvars[0].v_type == VAR_FLOAT)
9291 {
9292 *f = argvars[0].vval.v_float;
9293 return OK;
9294 }
9295 if (argvars[0].v_type == VAR_NUMBER)
9296 {
9297 *f = (float_T)argvars[0].vval.v_number;
9298 return OK;
9299 }
9300 EMSG(_("E808: Number or Float required"));
9301 return FAIL;
9302}
9303
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009304/*
9305 * "abs(expr)" function
9306 */
9307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009308f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009309{
9310 if (argvars[0].v_type == VAR_FLOAT)
9311 {
9312 rettv->v_type = VAR_FLOAT;
9313 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9314 }
9315 else
9316 {
9317 varnumber_T n;
9318 int error = FALSE;
9319
9320 n = get_tv_number_chk(&argvars[0], &error);
9321 if (error)
9322 rettv->vval.v_number = -1;
9323 else if (n > 0)
9324 rettv->vval.v_number = n;
9325 else
9326 rettv->vval.v_number = -n;
9327 }
9328}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009329
9330/*
9331 * "acos()" function
9332 */
9333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009334f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009335{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009336 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009337
9338 rettv->v_type = VAR_FLOAT;
9339 if (get_float_arg(argvars, &f) == OK)
9340 rettv->vval.v_float = acos(f);
9341 else
9342 rettv->vval.v_float = 0.0;
9343}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009344#endif
9345
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009347 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348 */
9349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009350f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009354 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009355 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009357 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009358 && !tv_check_lock(l->lv_lock,
9359 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009360 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009362 }
9363 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009364 EMSG(_(e_listreq));
9365}
9366
9367/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009368 * "alloc_fail(id, countdown, repeat)" function
9369 */
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009372{
9373 if (argvars[0].v_type != VAR_NUMBER
9374 || argvars[0].vval.v_number <= 0
9375 || argvars[1].v_type != VAR_NUMBER
9376 || argvars[1].vval.v_number < 0
9377 || argvars[2].v_type != VAR_NUMBER)
9378 EMSG(_(e_invarg));
9379 else
9380 {
9381 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009382 if (alloc_fail_id >= aid_last)
9383 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009384 alloc_fail_countdown = argvars[1].vval.v_number;
9385 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009386 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009387 }
9388}
9389
9390/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009391 * "and(expr, expr)" function
9392 */
9393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009394f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009395{
9396 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9397 & get_tv_number_chk(&argvars[1], NULL);
9398}
9399
9400/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009401 * "append(lnum, string/list)" function
9402 */
9403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009404f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009405{
9406 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009407 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009408 list_T *l = NULL;
9409 listitem_T *li = NULL;
9410 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009411 long added = 0;
9412
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009413 /* When coming here from Insert mode, sync undo, so that this can be
9414 * undone separately from what was previously inserted. */
9415 if (u_sync_once == 2)
9416 {
9417 u_sync_once = 1; /* notify that u_sync() was called */
9418 u_sync(TRUE);
9419 }
9420
Bram Moolenaar0d660222005-01-07 21:51:51 +00009421 lnum = get_tv_lnum(argvars);
9422 if (lnum >= 0
9423 && lnum <= curbuf->b_ml.ml_line_count
9424 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009425 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009426 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009427 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009428 l = argvars[1].vval.v_list;
9429 if (l == NULL)
9430 return;
9431 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009432 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009433 for (;;)
9434 {
9435 if (l == NULL)
9436 tv = &argvars[1]; /* append a string */
9437 else if (li == NULL)
9438 break; /* end of list */
9439 else
9440 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009441 line = get_tv_string_chk(tv);
9442 if (line == NULL) /* type error */
9443 {
9444 rettv->vval.v_number = 1; /* Failed */
9445 break;
9446 }
9447 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009448 ++added;
9449 if (l == NULL)
9450 break;
9451 li = li->li_next;
9452 }
9453
9454 appended_lines_mark(lnum, added);
9455 if (curwin->w_cursor.lnum > lnum)
9456 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009458 else
9459 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462/*
9463 * "argc()" function
9464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009466f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
9472 * "argidx()" function
9473 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009475f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009477 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478}
9479
9480/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009481 * "arglistid()" function
9482 */
9483 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009484f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009485{
9486 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009487
9488 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009489 wp = find_tabwin(&argvars[0], &argvars[1]);
9490 if (wp != NULL)
9491 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009492}
9493
9494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009495 * "argv(nr)" function
9496 */
9497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009498f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009499{
9500 int idx;
9501
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009502 if (argvars[0].v_type != VAR_UNKNOWN)
9503 {
9504 idx = get_tv_number_chk(&argvars[0], NULL);
9505 if (idx >= 0 && idx < ARGCOUNT)
9506 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9507 else
9508 rettv->vval.v_string = NULL;
9509 rettv->v_type = VAR_STRING;
9510 }
9511 else if (rettv_list_alloc(rettv) == OK)
9512 for (idx = 0; idx < ARGCOUNT; ++idx)
9513 list_append_string(rettv->vval.v_list,
9514 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515}
9516
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009517typedef enum
9518{
9519 ASSERT_EQUAL,
9520 ASSERT_NOTEQUAL,
9521 ASSERT_MATCH,
9522 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009523 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009524} assert_type_T;
9525
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009526static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009527static 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 +01009528static void assert_error(garray_T *gap);
9529static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009530
9531/*
9532 * Prepare "gap" for an assert error and add the sourcing position.
9533 */
9534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009535prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009536{
9537 char buf[NUMBUFLEN];
9538
9539 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009540 if (sourcing_name != NULL)
9541 {
9542 ga_concat(gap, sourcing_name);
9543 if (sourcing_lnum > 0)
9544 ga_concat(gap, (char_u *)" ");
9545 }
9546 if (sourcing_lnum > 0)
9547 {
9548 sprintf(buf, "line %ld", (long)sourcing_lnum);
9549 ga_concat(gap, (char_u *)buf);
9550 }
9551 if (sourcing_name != NULL || sourcing_lnum > 0)
9552 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009553}
9554
9555/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009556 * Append "str" to "gap", escaping unprintable characters.
9557 * Changes NL to \n, CR to \r, etc.
9558 */
9559 static void
9560ga_concat_esc(garray_T *gap, char_u *str)
9561{
9562 char_u *p;
9563 char_u buf[NUMBUFLEN];
9564
Bram Moolenaarf1551962016-03-15 12:55:58 +01009565 if (str == NULL)
9566 {
9567 ga_concat(gap, (char_u *)"NULL");
9568 return;
9569 }
9570
Bram Moolenaar23689172016-02-15 22:37:37 +01009571 for (p = str; *p != NUL; ++p)
9572 switch (*p)
9573 {
9574 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9575 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9576 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9577 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9578 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9579 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9580 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9581 default:
9582 if (*p < ' ')
9583 {
9584 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9585 ga_concat(gap, buf);
9586 }
9587 else
9588 ga_append(gap, *p);
9589 break;
9590 }
9591}
9592
9593/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009594 * Fill "gap" with information about an assert error.
9595 */
9596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009597fill_assert_error(
9598 garray_T *gap,
9599 typval_T *opt_msg_tv,
9600 char_u *exp_str,
9601 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009602 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009603 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009604{
9605 char_u numbuf[NUMBUFLEN];
9606 char_u *tofree;
9607
9608 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9609 {
9610 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9611 vim_free(tofree);
9612 }
9613 else
9614 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009615 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009616 ga_concat(gap, (char_u *)"Pattern ");
9617 else
9618 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009619 if (exp_str == NULL)
9620 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009621 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009622 vim_free(tofree);
9623 }
9624 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009625 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009626 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009627 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009628 else if (atype == ASSERT_NOTMATCH)
9629 ga_concat(gap, (char_u *)" does match ");
9630 else if (atype == ASSERT_NOTEQUAL)
9631 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009632 else
9633 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009634 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009635 vim_free(tofree);
9636 }
9637}
Bram Moolenaar43345542015-11-29 17:35:35 +01009638
9639/*
9640 * Add an assert error to v:errors.
9641 */
9642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009644{
9645 struct vimvar *vp = &vimvars[VV_ERRORS];
9646
9647 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9648 /* Make sure v:errors is a list. */
9649 set_vim_var_list(VV_ERRORS, list_alloc());
9650 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9651}
9652
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009653 static void
9654assert_equal_common(typval_T *argvars, assert_type_T atype)
9655{
9656 garray_T ga;
9657
9658 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9659 != (atype == ASSERT_EQUAL))
9660 {
9661 prepare_assert_error(&ga);
9662 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9663 atype);
9664 assert_error(&ga);
9665 ga_clear(&ga);
9666 }
9667}
9668
Bram Moolenaar43345542015-11-29 17:35:35 +01009669/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009670 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009671 */
9672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009673f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009674{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009675 assert_equal_common(argvars, ASSERT_EQUAL);
9676}
Bram Moolenaar43345542015-11-29 17:35:35 +01009677
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009678/*
9679 * "assert_notequal(expected, actual[, msg])" function
9680 */
9681 static void
9682f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9683{
9684 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009685}
9686
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009687/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009688 * "assert_exception(string[, msg])" function
9689 */
9690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009691f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009692{
9693 garray_T ga;
9694 char *error;
9695
9696 error = (char *)get_tv_string_chk(&argvars[0]);
9697 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9698 {
9699 prepare_assert_error(&ga);
9700 ga_concat(&ga, (char_u *)"v:exception is not set");
9701 assert_error(&ga);
9702 ga_clear(&ga);
9703 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009704 else if (error != NULL
9705 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009706 {
9707 prepare_assert_error(&ga);
9708 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009709 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009710 assert_error(&ga);
9711 ga_clear(&ga);
9712 }
9713}
9714
9715/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009716 * "assert_fails(cmd [, error])" function
9717 */
9718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009719f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009720{
9721 char_u *cmd = get_tv_string_chk(&argvars[0]);
9722 garray_T ga;
9723
9724 called_emsg = FALSE;
9725 suppress_errthrow = TRUE;
9726 emsg_silent = TRUE;
9727 do_cmdline_cmd(cmd);
9728 if (!called_emsg)
9729 {
9730 prepare_assert_error(&ga);
9731 ga_concat(&ga, (char_u *)"command did not fail: ");
9732 ga_concat(&ga, cmd);
9733 assert_error(&ga);
9734 ga_clear(&ga);
9735 }
9736 else if (argvars[1].v_type != VAR_UNKNOWN)
9737 {
9738 char_u buf[NUMBUFLEN];
9739 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9740
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009741 if (error == NULL
9742 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009743 {
9744 prepare_assert_error(&ga);
9745 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009746 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009747 assert_error(&ga);
9748 ga_clear(&ga);
9749 }
9750 }
9751
9752 called_emsg = FALSE;
9753 suppress_errthrow = FALSE;
9754 emsg_silent = FALSE;
9755 emsg_on_display = FALSE;
9756 set_vim_var_string(VV_ERRMSG, NULL, 0);
9757}
9758
9759/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009760 * Common for assert_true() and assert_false().
9761 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009763assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009764{
9765 int error = FALSE;
9766 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009767
Bram Moolenaar37127922016-02-06 20:29:28 +01009768 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009769 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009770 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009771 if (argvars[0].v_type != VAR_NUMBER
9772 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9773 || error)
9774 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009775 prepare_assert_error(&ga);
9776 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009777 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009778 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009779 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009780 ga_clear(&ga);
9781 }
9782}
9783
9784/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009785 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009786 */
9787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009788f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009789{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009790 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009791}
9792
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009793 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009794assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009795{
9796 garray_T ga;
9797 char_u buf1[NUMBUFLEN];
9798 char_u buf2[NUMBUFLEN];
9799 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9800 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9801
Bram Moolenaar72188e92016-03-28 22:48:29 +02009802 if (pat == NULL || text == NULL)
9803 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009804 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009805 {
9806 prepare_assert_error(&ga);
9807 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009808 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009809 assert_error(&ga);
9810 ga_clear(&ga);
9811 }
9812}
9813
9814/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009815 * "assert_match(pattern, actual[, msg])" function
9816 */
9817 static void
9818f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9819{
9820 assert_match_common(argvars, ASSERT_MATCH);
9821}
9822
9823/*
9824 * "assert_notmatch(pattern, actual[, msg])" function
9825 */
9826 static void
9827f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9828{
9829 assert_match_common(argvars, ASSERT_NOTMATCH);
9830}
9831
9832/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009833 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009834 */
9835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009836f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009837{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009838 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009839}
9840
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009841#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009842/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009843 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009844 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009846f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009847{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009848 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009849
9850 rettv->v_type = VAR_FLOAT;
9851 if (get_float_arg(argvars, &f) == OK)
9852 rettv->vval.v_float = asin(f);
9853 else
9854 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009855}
9856
9857/*
9858 * "atan()" function
9859 */
9860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009861f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009862{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009863 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009864
9865 rettv->v_type = VAR_FLOAT;
9866 if (get_float_arg(argvars, &f) == OK)
9867 rettv->vval.v_float = atan(f);
9868 else
9869 rettv->vval.v_float = 0.0;
9870}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009871
9872/*
9873 * "atan2()" function
9874 */
9875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009876f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009877{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009878 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009879
9880 rettv->v_type = VAR_FLOAT;
9881 if (get_float_arg(argvars, &fx) == OK
9882 && get_float_arg(&argvars[1], &fy) == OK)
9883 rettv->vval.v_float = atan2(fx, fy);
9884 else
9885 rettv->vval.v_float = 0.0;
9886}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009887#endif
9888
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889/*
9890 * "browse(save, title, initdir, default)" function
9891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894{
9895#ifdef FEAT_BROWSE
9896 int save;
9897 char_u *title;
9898 char_u *initdir;
9899 char_u *defname;
9900 char_u buf[NUMBUFLEN];
9901 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 save = get_tv_number_chk(&argvars[0], &error);
9905 title = get_tv_string_chk(&argvars[1]);
9906 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9907 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009909 if (error || title == NULL || initdir == NULL || defname == NULL)
9910 rettv->vval.v_string = NULL;
9911 else
9912 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009913 do_browse(save ? BROWSE_SAVE : 0,
9914 title, defname, NULL, initdir, NULL, curbuf);
9915#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009916 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009917#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009918 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009919}
9920
9921/*
9922 * "browsedir(title, initdir)" function
9923 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009925f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009926{
9927#ifdef FEAT_BROWSE
9928 char_u *title;
9929 char_u *initdir;
9930 char_u buf[NUMBUFLEN];
9931
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009932 title = get_tv_string_chk(&argvars[0]);
9933 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009934
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009935 if (title == NULL || initdir == NULL)
9936 rettv->vval.v_string = NULL;
9937 else
9938 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009939 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009941 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944}
9945
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009946static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009947
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948/*
9949 * Find a buffer by number or exact name.
9950 */
9951 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953{
9954 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009956 if (avar->v_type == VAR_NUMBER)
9957 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009958 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009960 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009961 if (buf == NULL)
9962 {
9963 /* No full path name match, try a match with a URL or a "nofile"
9964 * buffer, these don't use the full path. */
9965 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9966 if (buf->b_fname != NULL
9967 && (path_with_url(buf->b_fname)
9968#ifdef FEAT_QUICKFIX
9969 || bt_nofile(buf)
9970#endif
9971 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009972 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009973 break;
9974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 }
9976 return buf;
9977}
9978
9979/*
9980 * "bufexists(expr)" function
9981 */
9982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009983f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986}
9987
9988/*
9989 * "buflisted(expr)" function
9990 */
9991 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009992f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993{
9994 buf_T *buf;
9995
9996 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998}
9999
10000/*
10001 * "bufloaded(expr)" function
10002 */
10003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010004f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
10006 buf_T *buf;
10007
10008 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010}
10011
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010012 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010013buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 int save_magic;
10016 char_u *save_cpo;
10017 buf_T *buf;
10018
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10020 save_magic = p_magic;
10021 p_magic = TRUE;
10022 save_cpo = p_cpo;
10023 p_cpo = (char_u *)"";
10024
10025 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010026 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027
10028 p_magic = save_magic;
10029 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010030 return buf;
10031}
10032
10033/*
10034 * Get buffer by number or pattern.
10035 */
10036 static buf_T *
10037get_buf_tv(typval_T *tv, int curtab_only)
10038{
10039 char_u *name = tv->vval.v_string;
10040 buf_T *buf;
10041
10042 if (tv->v_type == VAR_NUMBER)
10043 return buflist_findnr((int)tv->vval.v_number);
10044 if (tv->v_type != VAR_STRING)
10045 return NULL;
10046 if (name == NULL || *name == NUL)
10047 return curbuf;
10048 if (name[0] == '$' && name[1] == NUL)
10049 return lastbuf;
10050
10051 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052
10053 /* If not found, try expanding the name, like done for bufexists(). */
10054 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010055 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056
10057 return buf;
10058}
10059
10060/*
10061 * "bufname(expr)" function
10062 */
10063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010064f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065{
10066 buf_T *buf;
10067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010068 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010070 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010073 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076 --emsg_off;
10077}
10078
10079/*
10080 * "bufnr(expr)" function
10081 */
10082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010083f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084{
10085 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010086 int error = FALSE;
10087 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010089 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010091 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010092 --emsg_off;
10093
10094 /* If the buffer isn't found and the second argument is not zero create a
10095 * new buffer. */
10096 if (buf == NULL
10097 && argvars[1].v_type != VAR_UNKNOWN
10098 && get_tv_number_chk(&argvars[1], &error) != 0
10099 && !error
10100 && (name = get_tv_string_chk(&argvars[0])) != NULL
10101 && !error)
10102 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010105 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108}
10109
10110/*
10111 * "bufwinnr(nr)" function
10112 */
10113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010114f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115{
10116#ifdef FEAT_WINDOWS
10117 win_T *wp;
10118 int winnr = 0;
10119#endif
10120 buf_T *buf;
10121
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010122 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010124 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125#ifdef FEAT_WINDOWS
10126 for (wp = firstwin; wp; wp = wp->w_next)
10127 {
10128 ++winnr;
10129 if (wp->w_buffer == buf)
10130 break;
10131 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010134 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135#endif
10136 --emsg_off;
10137}
10138
10139/*
10140 * "byte2line(byte)" function
10141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010143f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144{
10145#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010146 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147#else
10148 long boff = 0;
10149
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010152 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010154 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 (linenr_T)0, &boff);
10156#endif
10157}
10158
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010160byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010161{
10162#ifdef FEAT_MBYTE
10163 char_u *t;
10164#endif
10165 char_u *str;
10166 long idx;
10167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010168 str = get_tv_string_chk(&argvars[0]);
10169 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010171 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010172 return;
10173
10174#ifdef FEAT_MBYTE
10175 t = str;
10176 for ( ; idx > 0; idx--)
10177 {
10178 if (*t == NUL) /* EOL reached */
10179 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010180 if (enc_utf8 && comp)
10181 t += utf_ptr2len(t);
10182 else
10183 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010184 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010185 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010186#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010187 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010188 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010189#endif
10190}
10191
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010192/*
10193 * "byteidx()" function
10194 */
10195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010196f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010197{
10198 byteidx(argvars, rettv, FALSE);
10199}
10200
10201/*
10202 * "byteidxcomp()" function
10203 */
10204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010205f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010206{
10207 byteidx(argvars, rettv, TRUE);
10208}
10209
Bram Moolenaardb913952012-06-29 12:54:53 +020010210 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010211func_call(
10212 char_u *name,
10213 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010214 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010215 dict_T *selfdict,
10216 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010217{
10218 listitem_T *item;
10219 typval_T argv[MAX_FUNC_ARGS + 1];
10220 int argc = 0;
10221 int dummy;
10222 int r = 0;
10223
10224 for (item = args->vval.v_list->lv_first; item != NULL;
10225 item = item->li_next)
10226 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010227 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010228 {
10229 EMSG(_("E699: Too many arguments"));
10230 break;
10231 }
10232 /* Make a copy of each argument. This is needed to be able to set
10233 * v_lock to VAR_FIXED in the copy without changing the original list.
10234 */
10235 copy_tv(&item->li_tv, &argv[argc++]);
10236 }
10237
10238 if (item == NULL)
10239 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010241 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010242
10243 /* Free the arguments. */
10244 while (argc > 0)
10245 clear_tv(&argv[--argc]);
10246
10247 return r;
10248}
10249
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010250/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010251 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010252 */
10253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010254f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010255{
10256 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010257 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010258 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010259
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010260 if (argvars[1].v_type != VAR_LIST)
10261 {
10262 EMSG(_(e_listreq));
10263 return;
10264 }
10265 if (argvars[1].vval.v_list == NULL)
10266 return;
10267
10268 if (argvars[0].v_type == VAR_FUNC)
10269 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010270 else if (argvars[0].v_type == VAR_PARTIAL)
10271 {
10272 partial = argvars[0].vval.v_partial;
10273 func = partial->pt_name;
10274 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010275 else
10276 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010277 if (*func == NUL)
10278 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010279
Bram Moolenaare9a41262005-01-15 22:18:47 +000010280 if (argvars[2].v_type != VAR_UNKNOWN)
10281 {
10282 if (argvars[2].v_type != VAR_DICT)
10283 {
10284 EMSG(_(e_dictreq));
10285 return;
10286 }
10287 selfdict = argvars[2].vval.v_dict;
10288 }
10289
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010290 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010291}
10292
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010293#ifdef FEAT_FLOAT
10294/*
10295 * "ceil({float})" function
10296 */
10297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010298f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010299{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010300 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010301
10302 rettv->v_type = VAR_FLOAT;
10303 if (get_float_arg(argvars, &f) == OK)
10304 rettv->vval.v_float = ceil(f);
10305 else
10306 rettv->vval.v_float = 0.0;
10307}
10308#endif
10309
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010310#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010311/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010312 * "ch_close()" function
10313 */
10314 static void
10315f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10316{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010317 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010318
10319 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010320 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010321 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010322 channel_clear(channel);
10323 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010324}
10325
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010326/*
10327 * "ch_getbufnr()" function
10328 */
10329 static void
10330f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10331{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010332 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010333
10334 rettv->vval.v_number = -1;
10335 if (channel != NULL)
10336 {
10337 char_u *what = get_tv_string(&argvars[1]);
10338 int part;
10339
10340 if (STRCMP(what, "err") == 0)
10341 part = PART_ERR;
10342 else if (STRCMP(what, "out") == 0)
10343 part = PART_OUT;
10344 else if (STRCMP(what, "in") == 0)
10345 part = PART_IN;
10346 else
10347 part = PART_SOCK;
10348 if (channel->ch_part[part].ch_buffer != NULL)
10349 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10350 }
10351}
10352
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010353/*
10354 * "ch_getjob()" function
10355 */
10356 static void
10357f_ch_getjob(typval_T *argvars, typval_T *rettv)
10358{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010359 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010360
10361 if (channel != NULL)
10362 {
10363 rettv->v_type = VAR_JOB;
10364 rettv->vval.v_job = channel->ch_job;
10365 if (channel->ch_job != NULL)
10366 ++channel->ch_job->jv_refcount;
10367 }
10368}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010369
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010370/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010371 * "ch_info()" function
10372 */
10373 static void
10374f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10375{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010376 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010377
10378 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10379 channel_info(channel, rettv->vval.v_dict);
10380}
10381
10382/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010383 * "ch_log()" function
10384 */
10385 static void
10386f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10387{
10388 char_u *msg = get_tv_string(&argvars[0]);
10389 channel_T *channel = NULL;
10390
10391 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010392 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010393
10394 ch_log(channel, (char *)msg);
10395}
10396
10397/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010398 * "ch_logfile()" function
10399 */
10400 static void
10401f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10402{
10403 char_u *fname;
10404 char_u *opt = (char_u *)"";
10405 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010406
10407 fname = get_tv_string(&argvars[0]);
10408 if (argvars[1].v_type == VAR_STRING)
10409 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010410 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010411}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010412
10413/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010414 * "ch_open()" function
10415 */
10416 static void
10417f_ch_open(typval_T *argvars, typval_T *rettv)
10418{
Bram Moolenaar77073442016-02-13 23:23:53 +010010419 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010420 if (check_restricted() || check_secure())
10421 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010422 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010423}
10424
10425/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010426 * "ch_read()" function
10427 */
10428 static void
10429f_ch_read(typval_T *argvars, typval_T *rettv)
10430{
10431 common_channel_read(argvars, rettv, FALSE);
10432}
10433
10434/*
10435 * "ch_readraw()" function
10436 */
10437 static void
10438f_ch_readraw(typval_T *argvars, typval_T *rettv)
10439{
10440 common_channel_read(argvars, rettv, TRUE);
10441}
10442
10443/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010444 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010445 */
10446 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010447f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10448{
10449 ch_expr_common(argvars, rettv, TRUE);
10450}
10451
10452/*
10453 * "ch_sendexpr()" function
10454 */
10455 static void
10456f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10457{
10458 ch_expr_common(argvars, rettv, FALSE);
10459}
10460
10461/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010462 * "ch_evalraw()" function
10463 */
10464 static void
10465f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10466{
10467 ch_raw_common(argvars, rettv, TRUE);
10468}
10469
10470/*
10471 * "ch_sendraw()" function
10472 */
10473 static void
10474f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10475{
10476 ch_raw_common(argvars, rettv, FALSE);
10477}
10478
10479/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010480 * "ch_setoptions()" function
10481 */
10482 static void
10483f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10484{
10485 channel_T *channel;
10486 jobopt_T opt;
10487
Bram Moolenaar437905c2016-04-26 19:01:05 +020010488 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010489 if (channel == NULL)
10490 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010491 clear_job_options(&opt);
10492 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010493 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10494 channel_set_options(channel, &opt);
10495 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010496}
10497
10498/*
10499 * "ch_status()" function
10500 */
10501 static void
10502f_ch_status(typval_T *argvars, typval_T *rettv)
10503{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010504 channel_T *channel;
10505
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010506 /* return an empty string by default */
10507 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010508 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010509
Bram Moolenaar437905c2016-04-26 19:01:05 +020010510 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010511 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010512}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010513#endif
10514
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010515/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010516 * "changenr()" function
10517 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010519f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010520{
10521 rettv->vval.v_number = curbuf->b_u_seq_cur;
10522}
10523
10524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525 * "char2nr(string)" function
10526 */
10527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010528f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010529{
10530#ifdef FEAT_MBYTE
10531 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010532 {
10533 int utf8 = 0;
10534
10535 if (argvars[1].v_type != VAR_UNKNOWN)
10536 utf8 = get_tv_number_chk(&argvars[1], NULL);
10537
10538 if (utf8)
10539 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10540 else
10541 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 else
10544#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010545 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546}
10547
10548/*
10549 * "cindent(lnum)" function
10550 */
10551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010552f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553{
10554#ifdef FEAT_CINDENT
10555 pos_T pos;
10556 linenr_T lnum;
10557
10558 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10561 {
10562 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010563 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564 curwin->w_cursor = pos;
10565 }
10566 else
10567#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569}
10570
10571/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010572 * "clearmatches()" function
10573 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010575f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010576{
10577#ifdef FEAT_SEARCH_EXTRA
10578 clear_matches(curwin);
10579#endif
10580}
10581
10582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 * "col(string)" function
10584 */
10585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010586f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
10588 colnr_T col = 0;
10589 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010590 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010592 fp = var2fpos(&argvars[0], FALSE, &fnum);
10593 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 {
10595 if (fp->col == MAXCOL)
10596 {
10597 /* '> can be MAXCOL, get the length of the line then */
10598 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010599 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 else
10601 col = MAXCOL;
10602 }
10603 else
10604 {
10605 col = fp->col + 1;
10606#ifdef FEAT_VIRTUALEDIT
10607 /* col(".") when the cursor is on the NUL at the end of the line
10608 * because of "coladd" can be seen as an extra column. */
10609 if (virtual_active() && fp == &curwin->w_cursor)
10610 {
10611 char_u *p = ml_get_cursor();
10612
10613 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10614 curwin->w_virtcol - curwin->w_cursor.coladd))
10615 {
10616# ifdef FEAT_MBYTE
10617 int l;
10618
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010619 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 col += l;
10621# else
10622 if (*p != NUL && p[1] == NUL)
10623 ++col;
10624# endif
10625 }
10626 }
10627#endif
10628 }
10629 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010630 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631}
10632
Bram Moolenaar572cb562005-08-05 21:35:02 +000010633#if defined(FEAT_INS_EXPAND)
10634/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010635 * "complete()" function
10636 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010638f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010639{
10640 int startcol;
10641
10642 if ((State & INSERT) == 0)
10643 {
10644 EMSG(_("E785: complete() can only be used in Insert mode"));
10645 return;
10646 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010647
10648 /* Check for undo allowed here, because if something was already inserted
10649 * the line was already saved for undo and this check isn't done. */
10650 if (!undo_allowed())
10651 return;
10652
Bram Moolenaarade00832006-03-10 21:46:58 +000010653 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10654 {
10655 EMSG(_(e_invarg));
10656 return;
10657 }
10658
10659 startcol = get_tv_number_chk(&argvars[0], NULL);
10660 if (startcol <= 0)
10661 return;
10662
10663 set_completion(startcol - 1, argvars[1].vval.v_list);
10664}
10665
10666/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010667 * "complete_add()" function
10668 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010670f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010671{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010672 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010673}
10674
10675/*
10676 * "complete_check()" function
10677 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010679f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010680{
10681 int saved = RedrawingDisabled;
10682
10683 RedrawingDisabled = 0;
10684 ins_compl_check_keys(0);
10685 rettv->vval.v_number = compl_interrupted;
10686 RedrawingDisabled = saved;
10687}
10688#endif
10689
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690/*
10691 * "confirm(message, buttons[, default [, type]])" function
10692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010694f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695{
10696#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10697 char_u *message;
10698 char_u *buttons = NULL;
10699 char_u buf[NUMBUFLEN];
10700 char_u buf2[NUMBUFLEN];
10701 int def = 1;
10702 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010703 char_u *typestr;
10704 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010706 message = get_tv_string_chk(&argvars[0]);
10707 if (message == NULL)
10708 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010709 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010711 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10712 if (buttons == NULL)
10713 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010716 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010717 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10720 if (typestr == NULL)
10721 error = TRUE;
10722 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010724 switch (TOUPPER_ASC(*typestr))
10725 {
10726 case 'E': type = VIM_ERROR; break;
10727 case 'Q': type = VIM_QUESTION; break;
10728 case 'I': type = VIM_INFO; break;
10729 case 'W': type = VIM_WARNING; break;
10730 case 'G': type = VIM_GENERIC; break;
10731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732 }
10733 }
10734 }
10735 }
10736
10737 if (buttons == NULL || *buttons == NUL)
10738 buttons = (char_u *)_("&Ok");
10739
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010740 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010741 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010742 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743#endif
10744}
10745
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010746/*
10747 * "copy()" function
10748 */
10749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010750f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010751{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010752 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010753}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010755#ifdef FEAT_FLOAT
10756/*
10757 * "cos()" function
10758 */
10759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010760f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010761{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010762 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010763
10764 rettv->v_type = VAR_FLOAT;
10765 if (get_float_arg(argvars, &f) == OK)
10766 rettv->vval.v_float = cos(f);
10767 else
10768 rettv->vval.v_float = 0.0;
10769}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010770
10771/*
10772 * "cosh()" function
10773 */
10774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010775f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010776{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010777 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010778
10779 rettv->v_type = VAR_FLOAT;
10780 if (get_float_arg(argvars, &f) == OK)
10781 rettv->vval.v_float = cosh(f);
10782 else
10783 rettv->vval.v_float = 0.0;
10784}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010785#endif
10786
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010788 * "count()" function
10789 */
10790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010792{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010793 long n = 0;
10794 int ic = FALSE;
10795
Bram Moolenaare9a41262005-01-15 22:18:47 +000010796 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010797 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010798 listitem_T *li;
10799 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010800 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010801
Bram Moolenaare9a41262005-01-15 22:18:47 +000010802 if ((l = argvars[0].vval.v_list) != NULL)
10803 {
10804 li = l->lv_first;
10805 if (argvars[2].v_type != VAR_UNKNOWN)
10806 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010807 int error = FALSE;
10808
10809 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010810 if (argvars[3].v_type != VAR_UNKNOWN)
10811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010812 idx = get_tv_number_chk(&argvars[3], &error);
10813 if (!error)
10814 {
10815 li = list_find(l, idx);
10816 if (li == NULL)
10817 EMSGN(_(e_listidx), idx);
10818 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010819 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010820 if (error)
10821 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010822 }
10823
10824 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010825 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 ++n;
10827 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010828 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010829 else if (argvars[0].v_type == VAR_DICT)
10830 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010831 int todo;
10832 dict_T *d;
10833 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010834
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010835 if ((d = argvars[0].vval.v_dict) != NULL)
10836 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010837 int error = FALSE;
10838
Bram Moolenaare9a41262005-01-15 22:18:47 +000010839 if (argvars[2].v_type != VAR_UNKNOWN)
10840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 if (argvars[3].v_type != VAR_UNKNOWN)
10843 EMSG(_(e_invarg));
10844 }
10845
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010846 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010848 {
10849 if (!HASHITEM_EMPTY(hi))
10850 {
10851 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010852 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010853 ++n;
10854 }
10855 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010856 }
10857 }
10858 else
10859 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010860 rettv->vval.v_number = n;
10861}
10862
10863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10865 *
10866 * Checks the existence of a cscope connection.
10867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010869f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870{
10871#ifdef FEAT_CSCOPE
10872 int num = 0;
10873 char_u *dbpath = NULL;
10874 char_u *prepend = NULL;
10875 char_u buf[NUMBUFLEN];
10876
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010877 if (argvars[0].v_type != VAR_UNKNOWN
10878 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 num = (int)get_tv_number(&argvars[0]);
10881 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
10885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010886 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887#endif
10888}
10889
10890/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010891 * "cursor(lnum, col)" function, or
10892 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010894 * Moves the cursor to the specified line and column.
10895 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010898f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
10900 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010901#ifdef FEAT_VIRTUALEDIT
10902 long coladd = 0;
10903#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010904 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010906 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010907 if (argvars[1].v_type == VAR_UNKNOWN)
10908 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010909 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010910 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010911
Bram Moolenaar493c1782014-05-28 14:34:46 +020010912 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010913 {
10914 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010915 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010916 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010917 line = pos.lnum;
10918 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010919#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010920 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010921#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010922 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010923 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010924 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010925 set_curswant = FALSE;
10926 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010927 }
10928 else
10929 {
10930 line = get_tv_lnum(argvars);
10931 col = get_tv_number_chk(&argvars[1], NULL);
10932#ifdef FEAT_VIRTUALEDIT
10933 if (argvars[2].v_type != VAR_UNKNOWN)
10934 coladd = get_tv_number_chk(&argvars[2], NULL);
10935#endif
10936 }
10937 if (line < 0 || col < 0
10938#ifdef FEAT_VIRTUALEDIT
10939 || coladd < 0
10940#endif
10941 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943 if (line > 0)
10944 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945 if (col > 0)
10946 curwin->w_cursor.col = col - 1;
10947#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010948 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949#endif
10950
10951 /* Make sure the cursor is in a valid position. */
10952 check_cursor();
10953#ifdef FEAT_MBYTE
10954 /* Correct cursor for multi-byte character. */
10955 if (has_mbyte)
10956 mb_adjust_cursor();
10957#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010958
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010959 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010960 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961}
10962
10963/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 */
10966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010967f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010969 int noref = 0;
10970
10971 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010972 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010973 if (noref < 0 || noref > 1)
10974 EMSG(_(e_invarg));
10975 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010976 {
10977 current_copyID += COPYID_INC;
10978 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
10983 * "delete()" function
10984 */
10985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010986f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010988 char_u nbuf[NUMBUFLEN];
10989 char_u *name;
10990 char_u *flags;
10991
10992 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010994 return;
10995
10996 name = get_tv_string(&argvars[0]);
10997 if (name == NULL || *name == NUL)
10998 {
10999 EMSG(_(e_invarg));
11000 return;
11001 }
11002
11003 if (argvars[1].v_type != VAR_UNKNOWN)
11004 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011006 flags = (char_u *)"";
11007
11008 if (*flags == NUL)
11009 /* delete a file */
11010 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11011 else if (STRCMP(flags, "d") == 0)
11012 /* delete an empty directory */
11013 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11014 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011015 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011016 rettv->vval.v_number = delete_recursive(name);
11017 else
11018 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019}
11020
11021/*
11022 * "did_filetype()" function
11023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026{
11027#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029#endif
11030}
11031
11032/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011033 * "diff_filler()" function
11034 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011036f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011037{
11038#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011040#endif
11041}
11042
11043/*
11044 * "diff_hlID()" function
11045 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011047f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011048{
11049#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011050 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011051 static linenr_T prev_lnum = 0;
11052 static int changedtick = 0;
11053 static int fnum = 0;
11054 static int change_start = 0;
11055 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011056 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011057 int filler_lines;
11058 int col;
11059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011060 if (lnum < 0) /* ignore type error in {lnum} arg */
11061 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011062 if (lnum != prev_lnum
11063 || changedtick != curbuf->b_changedtick
11064 || fnum != curbuf->b_fnum)
11065 {
11066 /* New line, buffer, change: need to get the values. */
11067 filler_lines = diff_check(curwin, lnum);
11068 if (filler_lines < 0)
11069 {
11070 if (filler_lines == -1)
11071 {
11072 change_start = MAXCOL;
11073 change_end = -1;
11074 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11075 hlID = HLF_ADD; /* added line */
11076 else
11077 hlID = HLF_CHD; /* changed line */
11078 }
11079 else
11080 hlID = HLF_ADD; /* added line */
11081 }
11082 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011083 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011084 prev_lnum = lnum;
11085 changedtick = curbuf->b_changedtick;
11086 fnum = curbuf->b_fnum;
11087 }
11088
11089 if (hlID == HLF_CHD || hlID == HLF_TXD)
11090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011091 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011092 if (col >= change_start && col <= change_end)
11093 hlID = HLF_TXD; /* changed text */
11094 else
11095 hlID = HLF_CHD; /* changed line */
11096 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011097 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011098#endif
11099}
11100
11101/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011102 * "disable_char_avail_for_testing({expr})" function
11103 */
11104 static void
11105f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11106{
11107 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11108}
11109
11110/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011111 * "empty({expr})" function
11112 */
11113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011114f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011115{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011116 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011117
11118 switch (argvars[0].v_type)
11119 {
11120 case VAR_STRING:
11121 case VAR_FUNC:
11122 n = argvars[0].vval.v_string == NULL
11123 || *argvars[0].vval.v_string == NUL;
11124 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011125 case VAR_PARTIAL:
11126 n = FALSE;
11127 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011128 case VAR_NUMBER:
11129 n = argvars[0].vval.v_number == 0;
11130 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011131 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011132#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011133 n = argvars[0].vval.v_float == 0.0;
11134 break;
11135#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011136 case VAR_LIST:
11137 n = argvars[0].vval.v_list == NULL
11138 || argvars[0].vval.v_list->lv_first == NULL;
11139 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011140 case VAR_DICT:
11141 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011142 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011144 case VAR_SPECIAL:
11145 n = argvars[0].vval.v_number != VVAL_TRUE;
11146 break;
11147
Bram Moolenaar835dc632016-02-07 14:27:38 +010011148 case VAR_JOB:
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_job == NULL
11151 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11152 break;
11153#endif
11154 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011155#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011156 n = argvars[0].vval.v_channel == NULL
11157 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011158 break;
11159#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011160 case VAR_UNKNOWN:
11161 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11162 n = TRUE;
11163 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011164 }
11165
11166 rettv->vval.v_number = n;
11167}
11168
11169/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170 * "escape({string}, {chars})" function
11171 */
11172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011173f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174{
11175 char_u buf[NUMBUFLEN];
11176
Bram Moolenaar758711c2005-02-02 23:11:38 +000011177 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11178 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011180}
11181
11182/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011183 * "eval()" function
11184 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011185 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011186f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011187{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011188 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011189
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190 s = get_tv_string_chk(&argvars[0]);
11191 if (s != NULL)
11192 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011193
Bram Moolenaar615b9972015-01-14 17:15:05 +010011194 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011195 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11196 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011197 if (p != NULL && !aborting())
11198 EMSG2(_(e_invexpr2), p);
11199 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011201 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011202 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011203 else if (*s != NUL)
11204 EMSG(_(e_trailing));
11205}
11206
11207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208 * "eventhandler()" function
11209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011211f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011212{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214}
11215
11216/*
11217 * "executable()" function
11218 */
11219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011220f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011222 char_u *name = get_tv_string(&argvars[0]);
11223
11224 /* Check in $PATH and also check directly if there is a directory name. */
11225 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11226 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011227}
11228
11229/*
11230 * "exepath()" function
11231 */
11232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011233f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011234{
11235 char_u *p = NULL;
11236
Bram Moolenaarb5971142015-03-21 17:32:19 +010011237 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011238 rettv->v_type = VAR_STRING;
11239 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240}
11241
11242/*
11243 * "exists()" function
11244 */
11245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011246f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011247{
11248 char_u *p;
11249 char_u *name;
11250 int n = FALSE;
11251 int len = 0;
11252
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011253 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011254 if (*p == '$') /* environment variable */
11255 {
11256 /* first try "normal" environment variables (fast) */
11257 if (mch_getenv(p + 1) != NULL)
11258 n = TRUE;
11259 else
11260 {
11261 /* try expanding things like $VIM and ${HOME} */
11262 p = expand_env_save(p);
11263 if (p != NULL && *p != '$')
11264 n = TRUE;
11265 vim_free(p);
11266 }
11267 }
11268 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011270 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011271 if (*skipwhite(p) != NUL)
11272 n = FALSE; /* trailing garbage */
11273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274 else if (*p == '*') /* internal or user defined function */
11275 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011276 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277 }
11278 else if (*p == ':')
11279 {
11280 n = cmd_exists(p + 1);
11281 }
11282 else if (*p == '#')
11283 {
11284#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011285 if (p[1] == '#')
11286 n = autocmd_supported(p + 2);
11287 else
11288 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289#endif
11290 }
11291 else /* internal variable */
11292 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011293 char_u *tofree;
11294 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011296 /* get_name_len() takes care of expanding curly braces */
11297 name = p;
11298 len = get_name_len(&p, &tofree, TRUE, FALSE);
11299 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011300 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011301 if (tofree != NULL)
11302 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011303 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011304 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011306 /* handle d.key, l[idx], f(expr) */
11307 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11308 if (n)
11309 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 }
11311 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011312 if (*p != NUL)
11313 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011315 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 }
11317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011318 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319}
11320
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011321#ifdef FEAT_FLOAT
11322/*
11323 * "exp()" function
11324 */
11325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011326f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011327{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011328 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011329
11330 rettv->v_type = VAR_FLOAT;
11331 if (get_float_arg(argvars, &f) == OK)
11332 rettv->vval.v_float = exp(f);
11333 else
11334 rettv->vval.v_float = 0.0;
11335}
11336#endif
11337
Bram Moolenaar071d4272004-06-13 20:20:40 +000011338/*
11339 * "expand()" function
11340 */
11341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011342f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343{
11344 char_u *s;
11345 int len;
11346 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011347 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011349 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011350 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011352 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011353 if (argvars[1].v_type != VAR_UNKNOWN
11354 && argvars[2].v_type != VAR_UNKNOWN
11355 && get_tv_number_chk(&argvars[2], &error)
11356 && !error)
11357 {
11358 rettv->v_type = VAR_LIST;
11359 rettv->vval.v_list = NULL;
11360 }
11361
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363 if (*s == '%' || *s == '#' || *s == '<')
11364 {
11365 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011366 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011368 if (rettv->v_type == VAR_LIST)
11369 {
11370 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11371 list_append_string(rettv->vval.v_list, result, -1);
11372 else
11373 vim_free(result);
11374 }
11375 else
11376 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011377 }
11378 else
11379 {
11380 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011381 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011382 if (argvars[1].v_type != VAR_UNKNOWN
11383 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011384 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011385 if (!error)
11386 {
11387 ExpandInit(&xpc);
11388 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011389 if (p_wic)
11390 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011391 if (rettv->v_type == VAR_STRING)
11392 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11393 options, WILD_ALL);
11394 else if (rettv_list_alloc(rettv) != FAIL)
11395 {
11396 int i;
11397
11398 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11399 for (i = 0; i < xpc.xp_numfiles; i++)
11400 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11401 ExpandCleanup(&xpc);
11402 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 }
11404 else
11405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 }
11407}
11408
11409/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011410 * Go over all entries in "d2" and add them to "d1".
11411 * When "action" is "error" then a duplicate key is an error.
11412 * When "action" is "force" then a duplicate key is overwritten.
11413 * Otherwise duplicate keys are ignored ("action" is "keep").
11414 */
11415 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011416dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011417{
11418 dictitem_T *di1;
11419 hashitem_T *hi2;
11420 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011421 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011422
11423 todo = (int)d2->dv_hashtab.ht_used;
11424 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11425 {
11426 if (!HASHITEM_EMPTY(hi2))
11427 {
11428 --todo;
11429 di1 = dict_find(d1, hi2->hi_key, -1);
11430 if (d1->dv_scope != 0)
11431 {
11432 /* Disallow replacing a builtin function in l: and g:.
11433 * Check the key to be valid when adding to any
11434 * scope. */
11435 if (d1->dv_scope == VAR_DEF_SCOPE
11436 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11437 && var_check_func_name(hi2->hi_key,
11438 di1 == NULL))
11439 break;
11440 if (!valid_varname(hi2->hi_key))
11441 break;
11442 }
11443 if (di1 == NULL)
11444 {
11445 di1 = dictitem_copy(HI2DI(hi2));
11446 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11447 dictitem_free(di1);
11448 }
11449 else if (*action == 'e')
11450 {
11451 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11452 break;
11453 }
11454 else if (*action == 'f' && HI2DI(hi2) != di1)
11455 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011456 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11457 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011458 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011459 clear_tv(&di1->di_tv);
11460 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11461 }
11462 }
11463 }
11464}
11465
11466/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011467 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011468 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011469 */
11470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011471f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011472{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011473 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011474
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011476 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011477 list_T *l1, *l2;
11478 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011479 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011480 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011481
Bram Moolenaare9a41262005-01-15 22:18:47 +000011482 l1 = argvars[0].vval.v_list;
11483 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011484 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011485 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011486 {
11487 if (argvars[2].v_type != VAR_UNKNOWN)
11488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011489 before = get_tv_number_chk(&argvars[2], &error);
11490 if (error)
11491 return; /* type error; errmsg already given */
11492
Bram Moolenaar758711c2005-02-02 23:11:38 +000011493 if (before == l1->lv_len)
11494 item = NULL;
11495 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011496 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011497 item = list_find(l1, before);
11498 if (item == NULL)
11499 {
11500 EMSGN(_(e_listidx), before);
11501 return;
11502 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 }
11504 }
11505 else
11506 item = NULL;
11507 list_extend(l1, l2, item);
11508
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509 copy_tv(&argvars[0], rettv);
11510 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011511 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11513 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011514 dict_T *d1, *d2;
11515 char_u *action;
11516 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011517
11518 d1 = argvars[0].vval.v_dict;
11519 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011520 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011521 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 {
11523 /* Check the third argument. */
11524 if (argvars[2].v_type != VAR_UNKNOWN)
11525 {
11526 static char *(av[]) = {"keep", "force", "error"};
11527
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011528 action = get_tv_string_chk(&argvars[2]);
11529 if (action == NULL)
11530 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 for (i = 0; i < 3; ++i)
11532 if (STRCMP(action, av[i]) == 0)
11533 break;
11534 if (i == 3)
11535 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011536 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011537 return;
11538 }
11539 }
11540 else
11541 action = (char_u *)"force";
11542
Bram Moolenaara9922d62013-05-30 13:01:18 +020011543 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011544
Bram Moolenaare9a41262005-01-15 22:18:47 +000011545 copy_tv(&argvars[0], rettv);
11546 }
11547 }
11548 else
11549 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011550}
11551
11552/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011553 * "feedkeys()" function
11554 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011556f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011557{
11558 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011559 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011560 char_u *keys, *flags;
11561 char_u nbuf[NUMBUFLEN];
11562 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011563 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011564 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011565 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011566
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011567 /* This is not allowed in the sandbox. If the commands would still be
11568 * executed in the sandbox it would be OK, but it probably happens later,
11569 * when "sandbox" is no longer set. */
11570 if (check_secure())
11571 return;
11572
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011573 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011574
11575 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011576 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011577 flags = get_tv_string_buf(&argvars[1], nbuf);
11578 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011579 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011580 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011581 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011582 case 'n': remap = FALSE; break;
11583 case 'm': remap = TRUE; break;
11584 case 't': typed = TRUE; break;
11585 case 'i': insert = TRUE; break;
11586 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011587 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011588 }
11589 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011590 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011591
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011592 if (*keys != NUL || execute)
11593 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011594 /* Need to escape K_SPECIAL and CSI before putting the string in the
11595 * typeahead buffer. */
11596 keys_esc = vim_strsave_escape_csi(keys);
11597 if (keys_esc != NULL)
11598 {
11599 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011600 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011601 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011602 if (vgetc_busy)
11603 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011604 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011605 {
11606 int save_msg_scroll = msg_scroll;
11607
11608 /* Avoid a 1 second delay when the keys start Insert mode. */
11609 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011610
Bram Moolenaar245c4102016-04-20 17:37:41 +020011611 if (!dangerous)
11612 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011613 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011614 if (!dangerous)
11615 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011616 msg_scroll |= save_msg_scroll;
11617 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011618 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011619 }
11620}
11621
11622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 * "filereadable()" function
11624 */
11625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011626f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011628 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011629 char_u *p;
11630 int n;
11631
Bram Moolenaarc236c162008-07-13 17:41:49 +000011632#ifndef O_NONBLOCK
11633# define O_NONBLOCK 0
11634#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011636 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11637 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 {
11639 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011640 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641 }
11642 else
11643 n = FALSE;
11644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646}
11647
11648/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011649 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 * rights to write into.
11651 */
11652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011653f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011655 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656}
11657
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011658 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011659findfilendir(
11660 typval_T *argvars UNUSED,
11661 typval_T *rettv,
11662 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011663{
11664#ifdef FEAT_SEARCHPATH
11665 char_u *fname;
11666 char_u *fresult = NULL;
11667 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11668 char_u *p;
11669 char_u pathbuf[NUMBUFLEN];
11670 int count = 1;
11671 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011672 int error = FALSE;
11673#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011674
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011675 rettv->vval.v_string = NULL;
11676 rettv->v_type = VAR_STRING;
11677
11678#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011680
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011681 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011683 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11684 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011685 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011686 else
11687 {
11688 if (*p != NUL)
11689 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011692 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011693 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011694 }
11695
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011696 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11697 error = TRUE;
11698
11699 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011700 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 do
11702 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011703 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011704 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011705 fresult = find_file_in_path_option(first ? fname : NULL,
11706 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011707 0, first, path,
11708 find_what,
11709 curbuf->b_ffname,
11710 find_what == FINDFILE_DIR
11711 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011712 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011713
11714 if (fresult != NULL && rettv->v_type == VAR_LIST)
11715 list_append_string(rettv->vval.v_list, fresult, -1);
11716
11717 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011718 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011719
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011720 if (rettv->v_type == VAR_STRING)
11721 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011722#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011723}
11724
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011725static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11726static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011727
11728/*
11729 * Implementation of map() and filter().
11730 */
11731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011732filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011733{
11734 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011735 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011736 listitem_T *li, *nli;
11737 list_T *l = NULL;
11738 dictitem_T *di;
11739 hashtab_T *ht;
11740 hashitem_T *hi;
11741 dict_T *d = NULL;
11742 typval_T save_val;
11743 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011744 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011745 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011746 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011747 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011748 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011749 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011750 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011751
Bram Moolenaare9a41262005-01-15 22:18:47 +000011752 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011753 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011754 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011755 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 return;
11757 }
11758 else if (argvars[0].v_type == VAR_DICT)
11759 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011760 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011761 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011762 return;
11763 }
11764 else
11765 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011766 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011767 return;
11768 }
11769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011770 expr = get_tv_string_buf_chk(&argvars[1], buf);
11771 /* On type errors, the preceding call has already displayed an error
11772 * message. Avoid a misleading error message for an empty string that
11773 * was not passed as argument. */
11774 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 prepare_vimvar(VV_VAL, &save_val);
11777 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011778
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011779 /* We reset "did_emsg" to be able to detect whether an error
11780 * occurred during evaluation of the expression. */
11781 save_did_emsg = did_emsg;
11782 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011783
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011784 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 vimvars[VV_KEY].vv_type = VAR_STRING;
11788
11789 ht = &d->dv_hashtab;
11790 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011791 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011793 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011794 if (!HASHITEM_EMPTY(hi))
11795 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011796 int r;
11797
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 --todo;
11799 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011800 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011801 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11802 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 break;
11804 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011805 r = filter_map_one(&di->di_tv, expr, map, &rem);
11806 clear_tv(&vimvars[VV_KEY].vv_tv);
11807 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 break;
11809 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011810 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011811 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11812 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011813 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011815 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 }
11817 }
11818 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 }
11820 else
11821 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011822 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 for (li = l->lv_first; li != NULL; li = nli)
11825 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011826 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011827 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011828 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011829 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011830 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011831 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011832 break;
11833 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011834 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011835 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011836 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011837 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011838
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011839 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011841
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011842 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011844
11845 copy_tv(&argvars[0], rettv);
11846}
11847
11848 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011849filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011850{
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011853 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011854
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011856 s = expr;
11857 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011858 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011859 if (*s != NUL) /* check for trailing chars after expr */
11860 {
11861 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011862 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011863 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011864 }
11865 if (map)
11866 {
11867 /* map(): replace the list item value */
11868 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011869 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870 *tv = rettv;
11871 }
11872 else
11873 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 int error = FALSE;
11875
Bram Moolenaare9a41262005-01-15 22:18:47 +000011876 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 /* On type error, nothing has been removed; return FAIL to stop the
11880 * loop. The error message was given by get_tv_number_chk(). */
11881 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011882 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011883 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011884 retval = OK;
11885theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011886 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011887 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011888}
11889
11890/*
11891 * "filter()" function
11892 */
11893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011894f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011895{
11896 filter_map(argvars, rettv, FALSE);
11897}
11898
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011899/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011900 * "finddir({fname}[, {path}[, {count}]])" function
11901 */
11902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011903f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011905 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011906}
11907
11908/*
11909 * "findfile({fname}[, {path}[, {count}]])" function
11910 */
11911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011912f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011913{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011914 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011915}
11916
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011917#ifdef FEAT_FLOAT
11918/*
11919 * "float2nr({float})" function
11920 */
11921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011922f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011923{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011924 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011925
11926 if (get_float_arg(argvars, &f) == OK)
11927 {
11928 if (f < -0x7fffffff)
11929 rettv->vval.v_number = -0x7fffffff;
11930 else if (f > 0x7fffffff)
11931 rettv->vval.v_number = 0x7fffffff;
11932 else
11933 rettv->vval.v_number = (varnumber_T)f;
11934 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011935}
11936
11937/*
11938 * "floor({float})" function
11939 */
11940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011941f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011942{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011943 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011944
11945 rettv->v_type = VAR_FLOAT;
11946 if (get_float_arg(argvars, &f) == OK)
11947 rettv->vval.v_float = floor(f);
11948 else
11949 rettv->vval.v_float = 0.0;
11950}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011951
11952/*
11953 * "fmod()" function
11954 */
11955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011956f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011957{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011958 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011959
11960 rettv->v_type = VAR_FLOAT;
11961 if (get_float_arg(argvars, &fx) == OK
11962 && get_float_arg(&argvars[1], &fy) == OK)
11963 rettv->vval.v_float = fmod(fx, fy);
11964 else
11965 rettv->vval.v_float = 0.0;
11966}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011967#endif
11968
Bram Moolenaar0d660222005-01-07 21:51:51 +000011969/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011970 * "fnameescape({string})" function
11971 */
11972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011973f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011974{
11975 rettv->vval.v_string = vim_strsave_fnameescape(
11976 get_tv_string(&argvars[0]), FALSE);
11977 rettv->v_type = VAR_STRING;
11978}
11979
11980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 * "fnamemodify({fname}, {mods})" function
11982 */
11983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011984f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985{
11986 char_u *fname;
11987 char_u *mods;
11988 int usedlen = 0;
11989 int len;
11990 char_u *fbuf = NULL;
11991 char_u buf[NUMBUFLEN];
11992
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011993 fname = get_tv_string_chk(&argvars[0]);
11994 mods = get_tv_string_buf_chk(&argvars[1], buf);
11995 if (fname == NULL || mods == NULL)
11996 fname = NULL;
11997 else
11998 {
11999 len = (int)STRLEN(fname);
12000 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012003 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012005 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012007 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 vim_free(fbuf);
12009}
12010
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012011static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012
12013/*
12014 * "foldclosed()" function
12015 */
12016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012017foldclosed_both(
12018 typval_T *argvars UNUSED,
12019 typval_T *rettv,
12020 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021{
12022#ifdef FEAT_FOLDING
12023 linenr_T lnum;
12024 linenr_T first, last;
12025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012026 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012027 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12028 {
12029 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12030 {
12031 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012032 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012033 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012034 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012035 return;
12036 }
12037 }
12038#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012040}
12041
12042/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043 * "foldclosed()" function
12044 */
12045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012046f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012047{
12048 foldclosed_both(argvars, rettv, FALSE);
12049}
12050
12051/*
12052 * "foldclosedend()" function
12053 */
12054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012055f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012056{
12057 foldclosed_both(argvars, rettv, TRUE);
12058}
12059
12060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061 * "foldlevel()" function
12062 */
12063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012064f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012065{
12066#ifdef FEAT_FOLDING
12067 linenr_T lnum;
12068
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012070 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012071 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012073}
12074
12075/*
12076 * "foldtext()" function
12077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012079f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
12081#ifdef FEAT_FOLDING
12082 linenr_T lnum;
12083 char_u *s;
12084 char_u *r;
12085 int len;
12086 char *txt;
12087#endif
12088
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012089 rettv->v_type = VAR_STRING;
12090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12093 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12094 <= curbuf->b_ml.ml_line_count
12095 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096 {
12097 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012098 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12099 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 {
12101 if (!linewhite(lnum))
12102 break;
12103 ++lnum;
12104 }
12105
12106 /* Find interesting text in this line. */
12107 s = skipwhite(ml_get(lnum));
12108 /* skip C comment-start */
12109 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012110 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012111 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012112 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012113 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012114 {
12115 s = skipwhite(ml_get(lnum + 1));
12116 if (*s == '*')
12117 s = skipwhite(s + 1);
12118 }
12119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012120 txt = _("+-%s%3ld lines: ");
12121 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012122 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 + 20 /* for %3ld */
12124 + STRLEN(s))); /* concatenated */
12125 if (r != NULL)
12126 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012127 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12128 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12129 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 len = (int)STRLEN(r);
12131 STRCAT(r, s);
12132 /* remove 'foldmarker' and 'commentstring' */
12133 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012134 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012135 }
12136 }
12137#endif
12138}
12139
12140/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012141 * "foldtextresult(lnum)" function
12142 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012144f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012145{
12146#ifdef FEAT_FOLDING
12147 linenr_T lnum;
12148 char_u *text;
12149 char_u buf[51];
12150 foldinfo_T foldinfo;
12151 int fold_count;
12152#endif
12153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012154 rettv->v_type = VAR_STRING;
12155 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012156#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012158 /* treat illegal types and illegal string values for {lnum} the same */
12159 if (lnum < 0)
12160 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012161 fold_count = foldedCount(curwin, lnum, &foldinfo);
12162 if (fold_count > 0)
12163 {
12164 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12165 &foldinfo, buf);
12166 if (text == buf)
12167 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012169 }
12170#endif
12171}
12172
12173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 * "foreground()" function
12175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012177f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012178{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179#ifdef FEAT_GUI
12180 if (gui.in_use)
12181 gui_mch_set_foreground();
12182#else
12183# ifdef WIN32
12184 win32_set_foreground();
12185# endif
12186#endif
12187}
12188
12189/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012190 * "function()" function
12191 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012193f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012194{
12195 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012196 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012197 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012198 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012199
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012200 if (argvars[0].v_type == VAR_FUNC)
12201 {
12202 /* function(MyFunc, [arg], dict) */
12203 s = argvars[0].vval.v_string;
12204 }
12205 else if (argvars[0].v_type == VAR_PARTIAL
12206 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012207 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012208 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012209 arg_pt = argvars[0].vval.v_partial;
12210 s = arg_pt->pt_name;
12211 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012212 else
12213 {
12214 /* function('MyFunc', [arg], dict) */
12215 s = get_tv_string(&argvars[0]);
12216 use_string = TRUE;
12217 }
12218
12219 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012220 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012221 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012222 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12223 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012224 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012225 else
12226 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012227 int dict_idx = 0;
12228 int arg_idx = 0;
12229 list_T *list = NULL;
12230
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012231 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012232 {
12233 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012234 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012235
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012236 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12237 * also be called from another script. Using trans_function_name()
12238 * would also work, but some plugins depend on the name being
12239 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012240 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012241 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12242 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012243 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012244 STRCPY(name, sid_buf);
12245 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012246 }
12247 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012248 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012249 name = vim_strsave(s);
12250
12251 if (argvars[1].v_type != VAR_UNKNOWN)
12252 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012253 if (argvars[2].v_type != VAR_UNKNOWN)
12254 {
12255 /* function(name, [args], dict) */
12256 arg_idx = 1;
12257 dict_idx = 2;
12258 }
12259 else if (argvars[1].v_type == VAR_DICT)
12260 /* function(name, dict) */
12261 dict_idx = 1;
12262 else
12263 /* function(name, [args]) */
12264 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012265 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012266 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012267 if (argvars[dict_idx].v_type != VAR_DICT)
12268 {
12269 EMSG(_("E922: expected a dict"));
12270 vim_free(name);
12271 return;
12272 }
12273 if (argvars[dict_idx].vval.v_dict == NULL)
12274 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012275 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012276 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012277 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012278 if (argvars[arg_idx].v_type != VAR_LIST)
12279 {
12280 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12281 vim_free(name);
12282 return;
12283 }
12284 list = argvars[arg_idx].vval.v_list;
12285 if (list == NULL || list->lv_len == 0)
12286 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012287 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012288 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012289 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012290 {
12291 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012293 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012294 if (pt == NULL)
12295 vim_free(name);
12296 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012297 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012298 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012299 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012300 listitem_T *li;
12301 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012302 int arg_len = 0;
12303 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012304
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012305 if (arg_pt != NULL)
12306 arg_len = arg_pt->pt_argc;
12307 if (list != NULL)
12308 lv_len = list->lv_len;
12309 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012310 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012311 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012312 if (pt->pt_argv == NULL)
12313 {
12314 vim_free(pt);
12315 vim_free(name);
12316 return;
12317 }
12318 else
12319 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012320 for (i = 0; i < arg_len; i++)
12321 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12322 if (lv_len > 0)
12323 for (li = list->lv_first; li != NULL;
12324 li = li->li_next)
12325 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 }
12327 }
12328
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012329 /* For "function(dict.func, [], dict)" and "func" is a partial
12330 * use "dict". That is backwards compatible. */
12331 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012332 {
12333 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12334 ++pt->pt_dict->dv_refcount;
12335 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012336 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012337 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012338 pt->pt_dict = arg_pt->pt_dict;
12339 if (pt->pt_dict != NULL)
12340 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012341 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012342
12343 pt->pt_refcount = 1;
12344 pt->pt_name = name;
12345 func_ref(pt->pt_name);
12346 }
12347 rettv->v_type = VAR_PARTIAL;
12348 rettv->vval.v_partial = pt;
12349 }
12350 else
12351 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012352 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012353 rettv->v_type = VAR_FUNC;
12354 rettv->vval.v_string = name;
12355 func_ref(name);
12356 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012357 }
12358}
12359
12360/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012361 * "garbagecollect()" function
12362 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012364f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012365{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012366 /* This is postponed until we are back at the toplevel, because we may be
12367 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12368 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012369
12370 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12371 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012372}
12373
12374/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012375 * "garbagecollect_for_testing()" function
12376 */
12377 static void
12378f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12379{
12380 /* This is dangerous, any Lists and Dicts used internally may be freed
12381 * while still in use. */
12382 garbage_collect(TRUE);
12383}
12384
12385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012386 * "get()" function
12387 */
12388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012389f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012390{
Bram Moolenaar33570922005-01-25 22:26:29 +000012391 listitem_T *li;
12392 list_T *l;
12393 dictitem_T *di;
12394 dict_T *d;
12395 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012396
Bram Moolenaare9a41262005-01-15 22:18:47 +000012397 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012398 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012399 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012401 int error = FALSE;
12402
12403 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12404 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012405 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012406 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 else if (argvars[0].v_type == VAR_DICT)
12409 {
12410 if ((d = argvars[0].vval.v_dict) != NULL)
12411 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012412 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012413 if (di != NULL)
12414 tv = &di->di_tv;
12415 }
12416 }
12417 else
12418 EMSG2(_(e_listdictarg), "get()");
12419
12420 if (tv == NULL)
12421 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012422 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423 copy_tv(&argvars[2], rettv);
12424 }
12425 else
12426 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012427}
12428
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012429static 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 +000012430
12431/*
12432 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012433 * Return a range (from start to end) of lines in rettv from the specified
12434 * buffer.
12435 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012436 */
12437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012438get_buffer_lines(
12439 buf_T *buf,
12440 linenr_T start,
12441 linenr_T end,
12442 int retlist,
12443 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012444{
12445 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012446
Bram Moolenaar959a1432013-12-14 12:17:38 +010012447 rettv->v_type = VAR_STRING;
12448 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012449 if (retlist && rettv_list_alloc(rettv) == FAIL)
12450 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012451
12452 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12453 return;
12454
12455 if (!retlist)
12456 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012457 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12458 p = ml_get_buf(buf, start, FALSE);
12459 else
12460 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012461 rettv->vval.v_string = vim_strsave(p);
12462 }
12463 else
12464 {
12465 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012466 return;
12467
12468 if (start < 1)
12469 start = 1;
12470 if (end > buf->b_ml.ml_line_count)
12471 end = buf->b_ml.ml_line_count;
12472 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012473 if (list_append_string(rettv->vval.v_list,
12474 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012475 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012476 }
12477}
12478
12479/*
12480 * "getbufline()" function
12481 */
12482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012483f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012484{
12485 linenr_T lnum;
12486 linenr_T end;
12487 buf_T *buf;
12488
12489 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12490 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012491 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012492 --emsg_off;
12493
Bram Moolenaar661b1822005-07-28 22:36:45 +000012494 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012495 if (argvars[2].v_type == VAR_UNKNOWN)
12496 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012497 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012498 end = get_tv_lnum_buf(&argvars[2], buf);
12499
Bram Moolenaar342337a2005-07-21 21:11:17 +000012500 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012501}
12502
Bram Moolenaar0d660222005-01-07 21:51:51 +000012503/*
12504 * "getbufvar()" function
12505 */
12506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012507f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508{
12509 buf_T *buf;
12510 buf_T *save_curbuf;
12511 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012512 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012513 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012515 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12516 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012518 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012519
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012520 rettv->v_type = VAR_STRING;
12521 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012522
12523 if (buf != NULL && varname != NULL)
12524 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012525 /* set curbuf to be our buf, temporarily */
12526 save_curbuf = curbuf;
12527 curbuf = buf;
12528
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012530 {
12531 if (get_option_tv(&varname, rettv, TRUE) == OK)
12532 done = TRUE;
12533 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012534 else if (STRCMP(varname, "changedtick") == 0)
12535 {
12536 rettv->v_type = VAR_NUMBER;
12537 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012538 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012539 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 else
12541 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012542 /* Look up the variable. */
12543 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12544 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12545 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012546 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012547 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012548 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012549 done = TRUE;
12550 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012551 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012552
12553 /* restore previous notion of curbuf */
12554 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555 }
12556
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12558 /* use the default value */
12559 copy_tv(&argvars[2], rettv);
12560
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561 --emsg_off;
12562}
12563
12564/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 * "getchar()" function
12566 */
12567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012568f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012569{
12570 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012571 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012573 /* Position the cursor. Needed after a message that ends in a space. */
12574 windgoto(msg_row, msg_col);
12575
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 ++no_mapping;
12577 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012578 for (;;)
12579 {
12580 if (argvars[0].v_type == VAR_UNKNOWN)
12581 /* getchar(): blocking wait. */
12582 n = safe_vgetc();
12583 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12584 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012585 n = vpeekc_any();
12586 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012587 /* illegal argument or getchar(0) and no char avail: return zero */
12588 n = 0;
12589 else
12590 /* getchar(0) and char avail: return char */
12591 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012592
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012593 if (n == K_IGNORE)
12594 continue;
12595 break;
12596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 --no_mapping;
12598 --allow_keys;
12599
Bram Moolenaar219b8702006-11-01 14:32:36 +000012600 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12601 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12602 vimvars[VV_MOUSE_COL].vv_nr = 0;
12603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012604 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 if (IS_SPECIAL(n) || mod_mask != 0)
12606 {
12607 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12608 int i = 0;
12609
12610 /* Turn a special key into three bytes, plus modifier. */
12611 if (mod_mask != 0)
12612 {
12613 temp[i++] = K_SPECIAL;
12614 temp[i++] = KS_MODIFIER;
12615 temp[i++] = mod_mask;
12616 }
12617 if (IS_SPECIAL(n))
12618 {
12619 temp[i++] = K_SPECIAL;
12620 temp[i++] = K_SECOND(n);
12621 temp[i++] = K_THIRD(n);
12622 }
12623#ifdef FEAT_MBYTE
12624 else if (has_mbyte)
12625 i += (*mb_char2bytes)(n, temp + i);
12626#endif
12627 else
12628 temp[i++] = n;
12629 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630 rettv->v_type = VAR_STRING;
12631 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012632
12633#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012634 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012635 {
12636 int row = mouse_row;
12637 int col = mouse_col;
12638 win_T *win;
12639 linenr_T lnum;
12640# ifdef FEAT_WINDOWS
12641 win_T *wp;
12642# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012643 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012644
12645 if (row >= 0 && col >= 0)
12646 {
12647 /* Find the window at the mouse coordinates and compute the
12648 * text position. */
12649 win = mouse_find_win(&row, &col);
12650 (void)mouse_comp_pos(win, &row, &col, &lnum);
12651# ifdef FEAT_WINDOWS
12652 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012653 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012654# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012655 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012656 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12657 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12658 }
12659 }
12660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661 }
12662}
12663
12664/*
12665 * "getcharmod()" function
12666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012668f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671}
12672
12673/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012674 * "getcharsearch()" function
12675 */
12676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012677f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012678{
12679 if (rettv_dict_alloc(rettv) != FAIL)
12680 {
12681 dict_T *dict = rettv->vval.v_dict;
12682
12683 dict_add_nr_str(dict, "char", 0L, last_csearch());
12684 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12685 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12686 }
12687}
12688
12689/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012690 * "getcmdline()" function
12691 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012693f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->v_type = VAR_STRING;
12696 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012697}
12698
12699/*
12700 * "getcmdpos()" function
12701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012703f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706}
12707
12708/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012709 * "getcmdtype()" function
12710 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012713{
12714 rettv->v_type = VAR_STRING;
12715 rettv->vval.v_string = alloc(2);
12716 if (rettv->vval.v_string != NULL)
12717 {
12718 rettv->vval.v_string[0] = get_cmdline_type();
12719 rettv->vval.v_string[1] = NUL;
12720 }
12721}
12722
12723/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012724 * "getcmdwintype()" function
12725 */
12726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012727f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012728{
12729 rettv->v_type = VAR_STRING;
12730 rettv->vval.v_string = NULL;
12731#ifdef FEAT_CMDWIN
12732 rettv->vval.v_string = alloc(2);
12733 if (rettv->vval.v_string != NULL)
12734 {
12735 rettv->vval.v_string[0] = cmdwin_type;
12736 rettv->vval.v_string[1] = NUL;
12737 }
12738#endif
12739}
12740
12741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 * "getcwd()" function
12743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012745f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012747 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012748 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012750 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012751 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012752
12753 wp = find_tabwin(&argvars[0], &argvars[1]);
12754 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012756 if (wp->w_localdir != NULL)
12757 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12758 else if(globaldir != NULL)
12759 rettv->vval.v_string = vim_strsave(globaldir);
12760 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012761 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012762 cwd = alloc(MAXPATHL);
12763 if (cwd != NULL)
12764 {
12765 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12766 rettv->vval.v_string = vim_strsave(cwd);
12767 vim_free(cwd);
12768 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012769 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012770#ifdef BACKSLASH_IN_FILENAME
12771 if (rettv->vval.v_string != NULL)
12772 slash_adjust(rettv->vval.v_string);
12773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 }
12775}
12776
12777/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012778 * "getfontname()" function
12779 */
12780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012781f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012782{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012783 rettv->v_type = VAR_STRING;
12784 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012785#ifdef FEAT_GUI
12786 if (gui.in_use)
12787 {
12788 GuiFont font;
12789 char_u *name = NULL;
12790
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012791 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012792 {
12793 /* Get the "Normal" font. Either the name saved by
12794 * hl_set_font_name() or from the font ID. */
12795 font = gui.norm_font;
12796 name = hl_get_font_name();
12797 }
12798 else
12799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012801 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12802 return;
12803 font = gui_mch_get_font(name, FALSE);
12804 if (font == NOFONT)
12805 return; /* Invalid font name, return empty string. */
12806 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012807 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012808 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012809 gui_mch_free_font(font);
12810 }
12811#endif
12812}
12813
12814/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012815 * "getfperm({fname})" function
12816 */
12817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012818f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012819{
12820 char_u *fname;
12821 struct stat st;
12822 char_u *perm = NULL;
12823 char_u flags[] = "rwx";
12824 int i;
12825
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012826 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012827
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012828 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012829 if (mch_stat((char *)fname, &st) >= 0)
12830 {
12831 perm = vim_strsave((char_u *)"---------");
12832 if (perm != NULL)
12833 {
12834 for (i = 0; i < 9; i++)
12835 {
12836 if (st.st_mode & (1 << (8 - i)))
12837 perm[i] = flags[i % 3];
12838 }
12839 }
12840 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012842}
12843
12844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 * "getfsize({fname})" function
12846 */
12847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012848f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849{
12850 char_u *fname;
12851 struct stat st;
12852
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012855 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856
12857 if (mch_stat((char *)fname, &st) >= 0)
12858 {
12859 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012863 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012864
12865 /* non-perfect check for overflow */
12866 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12867 rettv->vval.v_number = -2;
12868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869 }
12870 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872}
12873
12874/*
12875 * "getftime({fname})" function
12876 */
12877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012878f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879{
12880 char_u *fname;
12881 struct stat st;
12882
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884
12885 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889}
12890
12891/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012892 * "getftype({fname})" function
12893 */
12894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012895f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012896{
12897 char_u *fname;
12898 struct stat st;
12899 char_u *type = NULL;
12900 char *t;
12901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012905 if (mch_lstat((char *)fname, &st) >= 0)
12906 {
12907#ifdef S_ISREG
12908 if (S_ISREG(st.st_mode))
12909 t = "file";
12910 else if (S_ISDIR(st.st_mode))
12911 t = "dir";
12912# ifdef S_ISLNK
12913 else if (S_ISLNK(st.st_mode))
12914 t = "link";
12915# endif
12916# ifdef S_ISBLK
12917 else if (S_ISBLK(st.st_mode))
12918 t = "bdev";
12919# endif
12920# ifdef S_ISCHR
12921 else if (S_ISCHR(st.st_mode))
12922 t = "cdev";
12923# endif
12924# ifdef S_ISFIFO
12925 else if (S_ISFIFO(st.st_mode))
12926 t = "fifo";
12927# endif
12928# ifdef S_ISSOCK
12929 else if (S_ISSOCK(st.st_mode))
12930 t = "fifo";
12931# endif
12932 else
12933 t = "other";
12934#else
12935# ifdef S_IFMT
12936 switch (st.st_mode & S_IFMT)
12937 {
12938 case S_IFREG: t = "file"; break;
12939 case S_IFDIR: t = "dir"; break;
12940# ifdef S_IFLNK
12941 case S_IFLNK: t = "link"; break;
12942# endif
12943# ifdef S_IFBLK
12944 case S_IFBLK: t = "bdev"; break;
12945# endif
12946# ifdef S_IFCHR
12947 case S_IFCHR: t = "cdev"; break;
12948# endif
12949# ifdef S_IFIFO
12950 case S_IFIFO: t = "fifo"; break;
12951# endif
12952# ifdef S_IFSOCK
12953 case S_IFSOCK: t = "socket"; break;
12954# endif
12955 default: t = "other";
12956 }
12957# else
12958 if (mch_isdir(fname))
12959 t = "dir";
12960 else
12961 t = "file";
12962# endif
12963#endif
12964 type = vim_strsave((char_u *)t);
12965 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012967}
12968
12969/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012970 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971 */
12972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012973f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974{
12975 linenr_T lnum;
12976 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012977 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012978
12979 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012980 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012981 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012982 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012983 retlist = FALSE;
12984 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012985 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012986 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012987 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012988 retlist = TRUE;
12989 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012990
Bram Moolenaar342337a2005-07-21 21:11:17 +000012991 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012992}
12993
12994/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012995 * "getmatches()" function
12996 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012998f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012999{
13000#ifdef FEAT_SEARCH_EXTRA
13001 dict_T *dict;
13002 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013003 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013004
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013005 if (rettv_list_alloc(rettv) == OK)
13006 {
13007 while (cur != NULL)
13008 {
13009 dict = dict_alloc();
13010 if (dict == NULL)
13011 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013012 if (cur->match.regprog == NULL)
13013 {
13014 /* match added with matchaddpos() */
13015 for (i = 0; i < MAXPOSMATCH; ++i)
13016 {
13017 llpos_T *llpos;
13018 char buf[6];
13019 list_T *l;
13020
13021 llpos = &cur->pos.pos[i];
13022 if (llpos->lnum == 0)
13023 break;
13024 l = list_alloc();
13025 if (l == NULL)
13026 break;
13027 list_append_number(l, (varnumber_T)llpos->lnum);
13028 if (llpos->col > 0)
13029 {
13030 list_append_number(l, (varnumber_T)llpos->col);
13031 list_append_number(l, (varnumber_T)llpos->len);
13032 }
13033 sprintf(buf, "pos%d", i + 1);
13034 dict_add_list(dict, buf, l);
13035 }
13036 }
13037 else
13038 {
13039 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13040 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013041 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013042 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13043 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013044# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013045 if (cur->conceal_char)
13046 {
13047 char_u buf[MB_MAXBYTES + 1];
13048
13049 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13050 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13051 }
13052# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013053 list_append_dict(rettv->vval.v_list, dict);
13054 cur = cur->next;
13055 }
13056 }
13057#endif
13058}
13059
13060/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013061 * "getpid()" function
13062 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013064f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013065{
13066 rettv->vval.v_number = mch_get_pid();
13067}
13068
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013070getpos_both(
13071 typval_T *argvars,
13072 typval_T *rettv,
13073 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013074{
Bram Moolenaara5525202006-03-02 22:52:09 +000013075 pos_T *fp;
13076 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013077 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013078
13079 if (rettv_list_alloc(rettv) == OK)
13080 {
13081 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013082 if (getcurpos)
13083 fp = &curwin->w_cursor;
13084 else
13085 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013086 if (fnum != -1)
13087 list_append_number(l, (varnumber_T)fnum);
13088 else
13089 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013090 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13091 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013092 list_append_number(l, (fp != NULL)
13093 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013094 : (varnumber_T)0);
13095 list_append_number(l,
13096#ifdef FEAT_VIRTUALEDIT
13097 (fp != NULL) ? (varnumber_T)fp->coladd :
13098#endif
13099 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013100 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013101 {
13102 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013103 list_append_number(l, curwin->w_curswant == MAXCOL ?
13104 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013105 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013106 }
13107 else
13108 rettv->vval.v_number = FALSE;
13109}
13110
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013111
13112/*
13113 * "getcurpos()" function
13114 */
13115 static void
13116f_getcurpos(typval_T *argvars, typval_T *rettv)
13117{
13118 getpos_both(argvars, rettv, TRUE);
13119}
13120
13121/*
13122 * "getpos(string)" function
13123 */
13124 static void
13125f_getpos(typval_T *argvars, typval_T *rettv)
13126{
13127 getpos_both(argvars, rettv, FALSE);
13128}
13129
Bram Moolenaara5525202006-03-02 22:52:09 +000013130/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013131 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013132 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013134f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013135{
13136#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013137 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013138#endif
13139
Bram Moolenaar2641f772005-03-25 21:58:17 +000013140#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013141 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013142 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013143 wp = NULL;
13144 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13145 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013146 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013147 if (wp == NULL)
13148 return;
13149 }
13150
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013151 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013152 }
13153#endif
13154}
13155
13156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 * "getreg()" function
13158 */
13159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013160f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161{
13162 char_u *strregname;
13163 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013164 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013165 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013166 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013168 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013170 strregname = get_tv_string_chk(&argvars[0]);
13171 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013172 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013173 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013174 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013175 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13176 return_list = get_tv_number_chk(&argvars[2], &error);
13177 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013180 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013181
13182 if (error)
13183 return;
13184
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 regname = (strregname == NULL ? '"' : *strregname);
13186 if (regname == 0)
13187 regname = '"';
13188
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013189 if (return_list)
13190 {
13191 rettv->v_type = VAR_LIST;
13192 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13193 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013194 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013195 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013196 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013197 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013198 }
13199 else
13200 {
13201 rettv->v_type = VAR_STRING;
13202 rettv->vval.v_string = get_reg_contents(regname,
13203 arg2 ? GREG_EXPR_SRC : 0);
13204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205}
13206
13207/*
13208 * "getregtype()" function
13209 */
13210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013211f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212{
13213 char_u *strregname;
13214 int regname;
13215 char_u buf[NUMBUFLEN + 2];
13216 long reglen = 0;
13217
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013218 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013219 {
13220 strregname = get_tv_string_chk(&argvars[0]);
13221 if (strregname == NULL) /* type error; errmsg already given */
13222 {
13223 rettv->v_type = VAR_STRING;
13224 rettv->vval.v_string = NULL;
13225 return;
13226 }
13227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 else
13229 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013230 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231
13232 regname = (strregname == NULL ? '"' : *strregname);
13233 if (regname == 0)
13234 regname = '"';
13235
13236 buf[0] = NUL;
13237 buf[1] = NUL;
13238 switch (get_reg_type(regname, &reglen))
13239 {
13240 case MLINE: buf[0] = 'V'; break;
13241 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 case MBLOCK:
13243 buf[0] = Ctrl_V;
13244 sprintf((char *)buf + 1, "%ld", reglen + 1);
13245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013247 rettv->v_type = VAR_STRING;
13248 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013249}
13250
13251/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013252 * "gettabvar()" function
13253 */
13254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013255f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013256{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013257 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013258 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013259 dictitem_T *v;
13260 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013261 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013262
13263 rettv->v_type = VAR_STRING;
13264 rettv->vval.v_string = NULL;
13265
13266 varname = get_tv_string_chk(&argvars[1]);
13267 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13268 if (tp != NULL && varname != NULL)
13269 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013270 /* Set tp to be our tabpage, temporarily. Also set the window to the
13271 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013272 if (switch_win(&oldcurwin, &oldtabpage,
13273 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013274 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013275 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013276 /* look up the variable */
13277 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13278 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13279 if (v != NULL)
13280 {
13281 copy_tv(&v->di_tv, rettv);
13282 done = TRUE;
13283 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013284 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013285
13286 /* restore previous notion of curwin */
13287 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013288 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013289
13290 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013291 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013292}
13293
13294/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013295 * "gettabwinvar()" function
13296 */
13297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013298f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013299{
13300 getwinvar(argvars, rettv, 1);
13301}
13302
13303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 * "getwinposx()" function
13305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013307f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013309 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef FEAT_GUI
13311 if (gui.in_use)
13312 {
13313 int x, y;
13314
13315 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013316 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 }
13318#endif
13319}
13320
13321/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013322 * "win_findbuf()" function
13323 */
13324 static void
13325f_win_findbuf(typval_T *argvars, typval_T *rettv)
13326{
13327 if (rettv_list_alloc(rettv) != FAIL)
13328 win_findbuf(argvars, rettv->vval.v_list);
13329}
13330
13331/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013332 * "win_getid()" function
13333 */
13334 static void
13335f_win_getid(typval_T *argvars, typval_T *rettv)
13336{
13337 rettv->vval.v_number = win_getid(argvars);
13338}
13339
13340/*
13341 * "win_gotoid()" function
13342 */
13343 static void
13344f_win_gotoid(typval_T *argvars, typval_T *rettv)
13345{
13346 rettv->vval.v_number = win_gotoid(argvars);
13347}
13348
13349/*
13350 * "win_id2tabwin()" function
13351 */
13352 static void
13353f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13354{
13355 if (rettv_list_alloc(rettv) != FAIL)
13356 win_id2tabwin(argvars, rettv->vval.v_list);
13357}
13358
13359/*
13360 * "win_id2win()" function
13361 */
13362 static void
13363f_win_id2win(typval_T *argvars, typval_T *rettv)
13364{
13365 rettv->vval.v_number = win_id2win(argvars);
13366}
13367
13368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369 * "getwinposy()" function
13370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013372f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375#ifdef FEAT_GUI
13376 if (gui.in_use)
13377 {
13378 int x, y;
13379
13380 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013381 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382 }
13383#endif
13384}
13385
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013386/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013387 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013388 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013389 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013390find_win_by_nr(
13391 typval_T *vp,
13392 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013393{
13394#ifdef FEAT_WINDOWS
13395 win_T *wp;
13396#endif
13397 int nr;
13398
13399 nr = get_tv_number_chk(vp, NULL);
13400
13401#ifdef FEAT_WINDOWS
13402 if (nr < 0)
13403 return NULL;
13404 if (nr == 0)
13405 return curwin;
13406
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013407 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13408 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013409 if (--nr <= 0)
13410 break;
13411 return wp;
13412#else
13413 if (nr == 0 || nr == 1)
13414 return curwin;
13415 return NULL;
13416#endif
13417}
13418
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013420 * Find window specified by "wvp" in tabpage "tvp".
13421 */
13422 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013423find_tabwin(
13424 typval_T *wvp, /* VAR_UNKNOWN for current window */
13425 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013426{
13427 win_T *wp = NULL;
13428 tabpage_T *tp = NULL;
13429 long n;
13430
13431 if (wvp->v_type != VAR_UNKNOWN)
13432 {
13433 if (tvp->v_type != VAR_UNKNOWN)
13434 {
13435 n = get_tv_number(tvp);
13436 if (n >= 0)
13437 tp = find_tabpage(n);
13438 }
13439 else
13440 tp = curtab;
13441
13442 if (tp != NULL)
13443 wp = find_win_by_nr(wvp, tp);
13444 }
13445 else
13446 wp = curwin;
13447
13448 return wp;
13449}
13450
13451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452 * "getwinvar()" function
13453 */
13454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013455f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013457 getwinvar(argvars, rettv, 0);
13458}
13459
13460/*
13461 * getwinvar() and gettabwinvar()
13462 */
13463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013464getwinvar(
13465 typval_T *argvars,
13466 typval_T *rettv,
13467 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013468{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013469 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013471 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013472 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013473 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013474#ifdef FEAT_WINDOWS
13475 win_T *oldcurwin;
13476 tabpage_T *oldtabpage;
13477 int need_switch_win;
13478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013480#ifdef FEAT_WINDOWS
13481 if (off == 1)
13482 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13483 else
13484 tp = curtab;
13485#endif
13486 win = find_win_by_nr(&argvars[off], tp);
13487 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013488 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013489
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013490 rettv->v_type = VAR_STRING;
13491 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492
13493 if (win != NULL && varname != NULL)
13494 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013495#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013496 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013497 * otherwise the window is not valid. Only do this when needed,
13498 * autocommands get blocked. */
13499 need_switch_win = !(tp == curtab && win == curwin);
13500 if (!need_switch_win
13501 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13502#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013503 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013504 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013505 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013506 if (get_option_tv(&varname, rettv, 1) == OK)
13507 done = TRUE;
13508 }
13509 else
13510 {
13511 /* Look up the variable. */
13512 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13513 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13514 varname, FALSE);
13515 if (v != NULL)
13516 {
13517 copy_tv(&v->di_tv, rettv);
13518 done = TRUE;
13519 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013522
Bram Moolenaarba117c22015-09-29 16:53:22 +020013523#ifdef FEAT_WINDOWS
13524 if (need_switch_win)
13525 /* restore previous notion of curwin */
13526 restore_win(oldcurwin, oldtabpage, TRUE);
13527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 }
13529
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013530 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13531 /* use the default return value */
13532 copy_tv(&argvars[off + 2], rettv);
13533
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 --emsg_off;
13535}
13536
13537/*
13538 * "glob()" function
13539 */
13540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013541f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013543 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013545 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013546
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013547 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013548 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013549 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013550 if (argvars[1].v_type != VAR_UNKNOWN)
13551 {
13552 if (get_tv_number_chk(&argvars[1], &error))
13553 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013554 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013555 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013556 if (get_tv_number_chk(&argvars[2], &error))
13557 {
13558 rettv->v_type = VAR_LIST;
13559 rettv->vval.v_list = NULL;
13560 }
13561 if (argvars[3].v_type != VAR_UNKNOWN
13562 && get_tv_number_chk(&argvars[3], &error))
13563 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013564 }
13565 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013566 if (!error)
13567 {
13568 ExpandInit(&xpc);
13569 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013570 if (p_wic)
13571 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013572 if (rettv->v_type == VAR_STRING)
13573 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013574 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013575 else if (rettv_list_alloc(rettv) != FAIL)
13576 {
13577 int i;
13578
13579 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13580 NULL, options, WILD_ALL_KEEP);
13581 for (i = 0; i < xpc.xp_numfiles; i++)
13582 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13583
13584 ExpandCleanup(&xpc);
13585 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013586 }
13587 else
13588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589}
13590
13591/*
13592 * "globpath()" function
13593 */
13594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013595f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013596{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013597 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013599 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013600 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013601 garray_T ga;
13602 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013604 /* When the optional second argument is non-zero, don't remove matches
13605 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013606 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013607 if (argvars[2].v_type != VAR_UNKNOWN)
13608 {
13609 if (get_tv_number_chk(&argvars[2], &error))
13610 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013611 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013612 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013613 if (get_tv_number_chk(&argvars[3], &error))
13614 {
13615 rettv->v_type = VAR_LIST;
13616 rettv->vval.v_list = NULL;
13617 }
13618 if (argvars[4].v_type != VAR_UNKNOWN
13619 && get_tv_number_chk(&argvars[4], &error))
13620 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013621 }
13622 }
13623 if (file != NULL && !error)
13624 {
13625 ga_init2(&ga, (int)sizeof(char_u *), 10);
13626 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13627 if (rettv->v_type == VAR_STRING)
13628 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13629 else if (rettv_list_alloc(rettv) != FAIL)
13630 for (i = 0; i < ga.ga_len; ++i)
13631 list_append_string(rettv->vval.v_list,
13632 ((char_u **)(ga.ga_data))[i], -1);
13633 ga_clear_strings(&ga);
13634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013635 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637}
13638
13639/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013640 * "glob2regpat()" function
13641 */
13642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013643f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013644{
13645 char_u *pat = get_tv_string_chk(&argvars[0]);
13646
13647 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013648 rettv->vval.v_string = (pat == NULL)
13649 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013650}
13651
13652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 * "has()" function
13654 */
13655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013656f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657{
13658 int i;
13659 char_u *name;
13660 int n = FALSE;
13661 static char *(has_list[]) =
13662 {
13663#ifdef AMIGA
13664 "amiga",
13665# ifdef FEAT_ARP
13666 "arp",
13667# endif
13668#endif
13669#ifdef __BEOS__
13670 "beos",
13671#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013672#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673 "mac",
13674#endif
13675#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013676 "macunix", /* built with 'darwin' enabled */
13677#endif
13678#if defined(__APPLE__) && __APPLE__ == 1
13679 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681#ifdef __QNX__
13682 "qnx",
13683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684#ifdef UNIX
13685 "unix",
13686#endif
13687#ifdef VMS
13688 "vms",
13689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690#ifdef WIN32
13691 "win32",
13692#endif
13693#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13694 "win32unix",
13695#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013696#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697 "win64",
13698#endif
13699#ifdef EBCDIC
13700 "ebcdic",
13701#endif
13702#ifndef CASE_INSENSITIVE_FILENAME
13703 "fname_case",
13704#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013705#ifdef HAVE_ACL
13706 "acl",
13707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#ifdef FEAT_ARABIC
13709 "arabic",
13710#endif
13711#ifdef FEAT_AUTOCMD
13712 "autocmd",
13713#endif
13714#ifdef FEAT_BEVAL
13715 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013716# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13717 "balloon_multiline",
13718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719#endif
13720#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13721 "builtin_terms",
13722# ifdef ALL_BUILTIN_TCAPS
13723 "all_builtin_terms",
13724# endif
13725#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013726#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13727 || defined(FEAT_GUI_W32) \
13728 || defined(FEAT_GUI_MOTIF))
13729 "browsefilter",
13730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731#ifdef FEAT_BYTEOFF
13732 "byte_offset",
13733#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013734#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013735 "channel",
13736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737#ifdef FEAT_CINDENT
13738 "cindent",
13739#endif
13740#ifdef FEAT_CLIENTSERVER
13741 "clientserver",
13742#endif
13743#ifdef FEAT_CLIPBOARD
13744 "clipboard",
13745#endif
13746#ifdef FEAT_CMDL_COMPL
13747 "cmdline_compl",
13748#endif
13749#ifdef FEAT_CMDHIST
13750 "cmdline_hist",
13751#endif
13752#ifdef FEAT_COMMENTS
13753 "comments",
13754#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013755#ifdef FEAT_CONCEAL
13756 "conceal",
13757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#ifdef FEAT_CRYPT
13759 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013760 "crypt-blowfish",
13761 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762#endif
13763#ifdef FEAT_CSCOPE
13764 "cscope",
13765#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013766#ifdef FEAT_CURSORBIND
13767 "cursorbind",
13768#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013769#ifdef CURSOR_SHAPE
13770 "cursorshape",
13771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772#ifdef DEBUG
13773 "debug",
13774#endif
13775#ifdef FEAT_CON_DIALOG
13776 "dialog_con",
13777#endif
13778#ifdef FEAT_GUI_DIALOG
13779 "dialog_gui",
13780#endif
13781#ifdef FEAT_DIFF
13782 "diff",
13783#endif
13784#ifdef FEAT_DIGRAPHS
13785 "digraphs",
13786#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013787#ifdef FEAT_DIRECTX
13788 "directx",
13789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790#ifdef FEAT_DND
13791 "dnd",
13792#endif
13793#ifdef FEAT_EMACS_TAGS
13794 "emacs_tags",
13795#endif
13796 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013797 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798#ifdef FEAT_SEARCH_EXTRA
13799 "extra_search",
13800#endif
13801#ifdef FEAT_FKMAP
13802 "farsi",
13803#endif
13804#ifdef FEAT_SEARCHPATH
13805 "file_in_path",
13806#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013807#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013808 "filterpipe",
13809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013810#ifdef FEAT_FIND_ID
13811 "find_in_path",
13812#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013813#ifdef FEAT_FLOAT
13814 "float",
13815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816#ifdef FEAT_FOLDING
13817 "folding",
13818#endif
13819#ifdef FEAT_FOOTER
13820 "footer",
13821#endif
13822#if !defined(USE_SYSTEM) && defined(UNIX)
13823 "fork",
13824#endif
13825#ifdef FEAT_GETTEXT
13826 "gettext",
13827#endif
13828#ifdef FEAT_GUI
13829 "gui",
13830#endif
13831#ifdef FEAT_GUI_ATHENA
13832# ifdef FEAT_GUI_NEXTAW
13833 "gui_neXtaw",
13834# else
13835 "gui_athena",
13836# endif
13837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#ifdef FEAT_GUI_GTK
13839 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013840# ifdef USE_GTK3
13841 "gui_gtk3",
13842# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013844# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013846#ifdef FEAT_GUI_GNOME
13847 "gui_gnome",
13848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849#ifdef FEAT_GUI_MAC
13850 "gui_mac",
13851#endif
13852#ifdef FEAT_GUI_MOTIF
13853 "gui_motif",
13854#endif
13855#ifdef FEAT_GUI_PHOTON
13856 "gui_photon",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_GUI_W32
13859 "gui_win32",
13860#endif
13861#ifdef FEAT_HANGULIN
13862 "hangul_input",
13863#endif
13864#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13865 "iconv",
13866#endif
13867#ifdef FEAT_INS_EXPAND
13868 "insert_expand",
13869#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013870#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013871 "job",
13872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013873#ifdef FEAT_JUMPLIST
13874 "jumplist",
13875#endif
13876#ifdef FEAT_KEYMAP
13877 "keymap",
13878#endif
13879#ifdef FEAT_LANGMAP
13880 "langmap",
13881#endif
13882#ifdef FEAT_LIBCALL
13883 "libcall",
13884#endif
13885#ifdef FEAT_LINEBREAK
13886 "linebreak",
13887#endif
13888#ifdef FEAT_LISP
13889 "lispindent",
13890#endif
13891#ifdef FEAT_LISTCMDS
13892 "listcmds",
13893#endif
13894#ifdef FEAT_LOCALMAP
13895 "localmap",
13896#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013897#ifdef FEAT_LUA
13898# ifndef DYNAMIC_LUA
13899 "lua",
13900# endif
13901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902#ifdef FEAT_MENU
13903 "menu",
13904#endif
13905#ifdef FEAT_SESSION
13906 "mksession",
13907#endif
13908#ifdef FEAT_MODIFY_FNAME
13909 "modify_fname",
13910#endif
13911#ifdef FEAT_MOUSE
13912 "mouse",
13913#endif
13914#ifdef FEAT_MOUSESHAPE
13915 "mouseshape",
13916#endif
13917#if defined(UNIX) || defined(VMS)
13918# ifdef FEAT_MOUSE_DEC
13919 "mouse_dec",
13920# endif
13921# ifdef FEAT_MOUSE_GPM
13922 "mouse_gpm",
13923# endif
13924# ifdef FEAT_MOUSE_JSB
13925 "mouse_jsbterm",
13926# endif
13927# ifdef FEAT_MOUSE_NET
13928 "mouse_netterm",
13929# endif
13930# ifdef FEAT_MOUSE_PTERM
13931 "mouse_pterm",
13932# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013933# ifdef FEAT_MOUSE_SGR
13934 "mouse_sgr",
13935# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013936# ifdef FEAT_SYSMOUSE
13937 "mouse_sysmouse",
13938# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013939# ifdef FEAT_MOUSE_URXVT
13940 "mouse_urxvt",
13941# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942# ifdef FEAT_MOUSE_XTERM
13943 "mouse_xterm",
13944# endif
13945#endif
13946#ifdef FEAT_MBYTE
13947 "multi_byte",
13948#endif
13949#ifdef FEAT_MBYTE_IME
13950 "multi_byte_ime",
13951#endif
13952#ifdef FEAT_MULTI_LANG
13953 "multi_lang",
13954#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013955#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013956#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013957 "mzscheme",
13958#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960#ifdef FEAT_OLE
13961 "ole",
13962#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013963 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964#ifdef FEAT_PATH_EXTRA
13965 "path_extra",
13966#endif
13967#ifdef FEAT_PERL
13968#ifndef DYNAMIC_PERL
13969 "perl",
13970#endif
13971#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013972#ifdef FEAT_PERSISTENT_UNDO
13973 "persistent_undo",
13974#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013975#ifdef FEAT_PYTHON
13976#ifndef DYNAMIC_PYTHON
13977 "python",
13978#endif
13979#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013980#ifdef FEAT_PYTHON3
13981#ifndef DYNAMIC_PYTHON3
13982 "python3",
13983#endif
13984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013985#ifdef FEAT_POSTSCRIPT
13986 "postscript",
13987#endif
13988#ifdef FEAT_PRINTER
13989 "printer",
13990#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013991#ifdef FEAT_PROFILE
13992 "profile",
13993#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013994#ifdef FEAT_RELTIME
13995 "reltime",
13996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997#ifdef FEAT_QUICKFIX
13998 "quickfix",
13999#endif
14000#ifdef FEAT_RIGHTLEFT
14001 "rightleft",
14002#endif
14003#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14004 "ruby",
14005#endif
14006#ifdef FEAT_SCROLLBIND
14007 "scrollbind",
14008#endif
14009#ifdef FEAT_CMDL_INFO
14010 "showcmd",
14011 "cmdline_info",
14012#endif
14013#ifdef FEAT_SIGNS
14014 "signs",
14015#endif
14016#ifdef FEAT_SMARTINDENT
14017 "smartindent",
14018#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014019#ifdef STARTUPTIME
14020 "startuptime",
14021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022#ifdef FEAT_STL_OPT
14023 "statusline",
14024#endif
14025#ifdef FEAT_SUN_WORKSHOP
14026 "sun_workshop",
14027#endif
14028#ifdef FEAT_NETBEANS_INTG
14029 "netbeans_intg",
14030#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014031#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014032 "spell",
14033#endif
14034#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 "syntax",
14036#endif
14037#if defined(USE_SYSTEM) || !defined(UNIX)
14038 "system",
14039#endif
14040#ifdef FEAT_TAG_BINS
14041 "tag_binary",
14042#endif
14043#ifdef FEAT_TAG_OLDSTATIC
14044 "tag_old_static",
14045#endif
14046#ifdef FEAT_TAG_ANYWHITE
14047 "tag_any_white",
14048#endif
14049#ifdef FEAT_TCL
14050# ifndef DYNAMIC_TCL
14051 "tcl",
14052# endif
14053#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014054#ifdef FEAT_TERMGUICOLORS
14055 "termguicolors",
14056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057#ifdef TERMINFO
14058 "terminfo",
14059#endif
14060#ifdef FEAT_TERMRESPONSE
14061 "termresponse",
14062#endif
14063#ifdef FEAT_TEXTOBJ
14064 "textobjects",
14065#endif
14066#ifdef HAVE_TGETENT
14067 "tgetent",
14068#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014069#ifdef FEAT_TIMERS
14070 "timers",
14071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014072#ifdef FEAT_TITLE
14073 "title",
14074#endif
14075#ifdef FEAT_TOOLBAR
14076 "toolbar",
14077#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014078#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14079 "unnamedplus",
14080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081#ifdef FEAT_USR_CMDS
14082 "user-commands", /* was accidentally included in 5.4 */
14083 "user_commands",
14084#endif
14085#ifdef FEAT_VIMINFO
14086 "viminfo",
14087#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014088#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089 "vertsplit",
14090#endif
14091#ifdef FEAT_VIRTUALEDIT
14092 "virtualedit",
14093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095#ifdef FEAT_VISUALEXTRA
14096 "visualextra",
14097#endif
14098#ifdef FEAT_VREPLACE
14099 "vreplace",
14100#endif
14101#ifdef FEAT_WILDIGN
14102 "wildignore",
14103#endif
14104#ifdef FEAT_WILDMENU
14105 "wildmenu",
14106#endif
14107#ifdef FEAT_WINDOWS
14108 "windows",
14109#endif
14110#ifdef FEAT_WAK
14111 "winaltkeys",
14112#endif
14113#ifdef FEAT_WRITEBACKUP
14114 "writebackup",
14115#endif
14116#ifdef FEAT_XIM
14117 "xim",
14118#endif
14119#ifdef FEAT_XFONTSET
14120 "xfontset",
14121#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014122#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014123 "xpm",
14124 "xpm_w32", /* for backward compatibility */
14125#else
14126# if defined(HAVE_XPM)
14127 "xpm",
14128# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130#ifdef USE_XSMP
14131 "xsmp",
14132#endif
14133#ifdef USE_XSMP_INTERACT
14134 "xsmp_interact",
14135#endif
14136#ifdef FEAT_XCLIPBOARD
14137 "xterm_clipboard",
14138#endif
14139#ifdef FEAT_XTERM_SAVE
14140 "xterm_save",
14141#endif
14142#if defined(UNIX) && defined(FEAT_X11)
14143 "X11",
14144#endif
14145 NULL
14146 };
14147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014148 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014149 for (i = 0; has_list[i] != NULL; ++i)
14150 if (STRICMP(name, has_list[i]) == 0)
14151 {
14152 n = TRUE;
14153 break;
14154 }
14155
14156 if (n == FALSE)
14157 {
14158 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014159 {
14160 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014161 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014162 && vim_isdigit(name[6])
14163 && vim_isdigit(name[8])
14164 && vim_isdigit(name[10]))
14165 {
14166 int major = atoi((char *)name + 6);
14167 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014168
14169 /* Expect "patch-9.9.01234". */
14170 n = (major < VIM_VERSION_MAJOR
14171 || (major == VIM_VERSION_MAJOR
14172 && (minor < VIM_VERSION_MINOR
14173 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014174 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014175 }
14176 else
14177 n = has_patch(atoi((char *)name + 5));
14178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179 else if (STRICMP(name, "vim_starting") == 0)
14180 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014181#ifdef FEAT_MBYTE
14182 else if (STRICMP(name, "multi_byte_encoding") == 0)
14183 n = has_mbyte;
14184#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014185#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14186 else if (STRICMP(name, "balloon_multiline") == 0)
14187 n = multiline_balloon_available();
14188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014189#ifdef DYNAMIC_TCL
14190 else if (STRICMP(name, "tcl") == 0)
14191 n = tcl_enabled(FALSE);
14192#endif
14193#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14194 else if (STRICMP(name, "iconv") == 0)
14195 n = iconv_enabled(FALSE);
14196#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014197#ifdef DYNAMIC_LUA
14198 else if (STRICMP(name, "lua") == 0)
14199 n = lua_enabled(FALSE);
14200#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014201#ifdef DYNAMIC_MZSCHEME
14202 else if (STRICMP(name, "mzscheme") == 0)
14203 n = mzscheme_enabled(FALSE);
14204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205#ifdef DYNAMIC_RUBY
14206 else if (STRICMP(name, "ruby") == 0)
14207 n = ruby_enabled(FALSE);
14208#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014209#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210#ifdef DYNAMIC_PYTHON
14211 else if (STRICMP(name, "python") == 0)
14212 n = python_enabled(FALSE);
14213#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014214#endif
14215#ifdef FEAT_PYTHON3
14216#ifdef DYNAMIC_PYTHON3
14217 else if (STRICMP(name, "python3") == 0)
14218 n = python3_enabled(FALSE);
14219#endif
14220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014221#ifdef DYNAMIC_PERL
14222 else if (STRICMP(name, "perl") == 0)
14223 n = perl_enabled(FALSE);
14224#endif
14225#ifdef FEAT_GUI
14226 else if (STRICMP(name, "gui_running") == 0)
14227 n = (gui.in_use || gui.starting);
14228# ifdef FEAT_GUI_W32
14229 else if (STRICMP(name, "gui_win32s") == 0)
14230 n = gui_is_win32s();
14231# endif
14232# ifdef FEAT_BROWSE
14233 else if (STRICMP(name, "browse") == 0)
14234 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14235# endif
14236#endif
14237#ifdef FEAT_SYN_HL
14238 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014239 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240#endif
14241#if defined(WIN3264)
14242 else if (STRICMP(name, "win95") == 0)
14243 n = mch_windows95();
14244#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014245#ifdef FEAT_NETBEANS_INTG
14246 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014247 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 }
14250
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014251 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014252}
14253
14254/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014255 * "has_key()" function
14256 */
14257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014258f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014259{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014260 if (argvars[0].v_type != VAR_DICT)
14261 {
14262 EMSG(_(e_dictreq));
14263 return;
14264 }
14265 if (argvars[0].vval.v_dict == NULL)
14266 return;
14267
14268 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014269 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014270}
14271
14272/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014273 * "haslocaldir()" function
14274 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014276f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014277{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014278 win_T *wp = NULL;
14279
14280 wp = find_tabwin(&argvars[0], &argvars[1]);
14281 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014282}
14283
14284/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 * "hasmapto()" function
14286 */
14287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014289{
14290 char_u *name;
14291 char_u *mode;
14292 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014293 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014296 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 mode = (char_u *)"nvo";
14298 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014301 if (argvars[2].v_type != VAR_UNKNOWN)
14302 abbr = get_tv_number(&argvars[2]);
14303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304
Bram Moolenaar2c932302006-03-18 21:42:09 +000014305 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014306 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309}
14310
14311/*
14312 * "histadd()" function
14313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014315f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316{
14317#ifdef FEAT_CMDHIST
14318 int histype;
14319 char_u *str;
14320 char_u buf[NUMBUFLEN];
14321#endif
14322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014323 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 if (check_restricted() || check_secure())
14325 return;
14326#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014327 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14328 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329 if (histype >= 0)
14330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 if (*str != NUL)
14333 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014334 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337 return;
14338 }
14339 }
14340#endif
14341}
14342
14343/*
14344 * "histdel()" function
14345 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014347f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348{
14349#ifdef FEAT_CMDHIST
14350 int n;
14351 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014354 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14355 if (str == NULL)
14356 n = 0;
14357 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014360 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 else
14365 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 get_tv_string_buf(&argvars[1], buf));
14368 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369#endif
14370}
14371
14372/*
14373 * "histget()" function
14374 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014376f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377{
14378#ifdef FEAT_CMDHIST
14379 int type;
14380 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014381 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014383 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14384 if (str == NULL)
14385 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014387 {
14388 type = get_histtype(str);
14389 if (argvars[1].v_type == VAR_UNKNOWN)
14390 idx = get_history_idx(type);
14391 else
14392 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14393 /* -1 on type error */
14394 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014397 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014399 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400}
14401
14402/*
14403 * "histnr()" function
14404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014406f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407{
14408 int i;
14409
14410#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014411 char_u *history = get_tv_string_chk(&argvars[0]);
14412
14413 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 if (i >= HIST_CMD && i < HIST_COUNT)
14415 i = get_history_idx(i);
14416 else
14417#endif
14418 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420}
14421
14422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 * "highlightID(name)" function
14424 */
14425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014426f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429}
14430
14431/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014432 * "highlight_exists()" function
14433 */
14434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014435f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014436{
14437 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14438}
14439
14440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 * "hostname()" function
14442 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014444f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445{
14446 char_u hostname[256];
14447
14448 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014449 rettv->v_type = VAR_STRING;
14450 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451}
14452
14453/*
14454 * iconv() function
14455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014457f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458{
14459#ifdef FEAT_MBYTE
14460 char_u buf1[NUMBUFLEN];
14461 char_u buf2[NUMBUFLEN];
14462 char_u *from, *to, *str;
14463 vimconv_T vimconv;
14464#endif
14465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 rettv->v_type = VAR_STRING;
14467 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468
14469#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470 str = get_tv_string(&argvars[0]);
14471 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14472 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 vimconv.vc_type = CONV_NONE;
14474 convert_setup(&vimconv, from, to);
14475
14476 /* If the encodings are equal, no conversion needed. */
14477 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481
14482 convert_setup(&vimconv, NULL, NULL);
14483 vim_free(from);
14484 vim_free(to);
14485#endif
14486}
14487
14488/*
14489 * "indent()" function
14490 */
14491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014492f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493{
14494 linenr_T lnum;
14495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014498 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014500 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501}
14502
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014503/*
14504 * "index()" function
14505 */
14506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014507f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014508{
Bram Moolenaar33570922005-01-25 22:26:29 +000014509 list_T *l;
14510 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014511 long idx = 0;
14512 int ic = FALSE;
14513
14514 rettv->vval.v_number = -1;
14515 if (argvars[0].v_type != VAR_LIST)
14516 {
14517 EMSG(_(e_listreq));
14518 return;
14519 }
14520 l = argvars[0].vval.v_list;
14521 if (l != NULL)
14522 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014523 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014526 int error = FALSE;
14527
Bram Moolenaar758711c2005-02-02 23:11:38 +000014528 /* Start at specified item. Use the cached index that list_find()
14529 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014530 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014531 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014532 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014533 ic = get_tv_number_chk(&argvars[3], &error);
14534 if (error)
14535 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014536 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014537
Bram Moolenaar758711c2005-02-02 23:11:38 +000014538 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014539 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014540 {
14541 rettv->vval.v_number = idx;
14542 break;
14543 }
14544 }
14545}
14546
Bram Moolenaar071d4272004-06-13 20:20:40 +000014547static int inputsecret_flag = 0;
14548
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014549static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014550
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014552 * This function is used by f_input() and f_inputdialog() functions. The third
14553 * argument to f_input() specifies the type of completion to use at the
14554 * prompt. The third argument to f_inputdialog() specifies the value to return
14555 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556 */
14557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014558get_user_input(
14559 typval_T *argvars,
14560 typval_T *rettv,
14561 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014563 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564 char_u *p = NULL;
14565 int c;
14566 char_u buf[NUMBUFLEN];
14567 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014568 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014569 int xp_type = EXPAND_NOTHING;
14570 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014572 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014574
14575#ifdef NO_CONSOLE_INPUT
14576 /* While starting up, there is no place to enter text. */
14577 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579#endif
14580
14581 cmd_silent = FALSE; /* Want to see the prompt. */
14582 if (prompt != NULL)
14583 {
14584 /* Only the part of the message after the last NL is considered as
14585 * prompt for the command line */
14586 p = vim_strrchr(prompt, '\n');
14587 if (p == NULL)
14588 p = prompt;
14589 else
14590 {
14591 ++p;
14592 c = *p;
14593 *p = NUL;
14594 msg_start();
14595 msg_clr_eos();
14596 msg_puts_attr(prompt, echo_attr);
14597 msg_didout = FALSE;
14598 msg_starthere();
14599 *p = c;
14600 }
14601 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014603 if (argvars[1].v_type != VAR_UNKNOWN)
14604 {
14605 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14606 if (defstr != NULL)
14607 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014609 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014610 {
14611 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014612 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014613 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014614
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014615 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014616 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014617
Bram Moolenaar4463f292005-09-25 22:20:24 +000014618 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14619 if (xp_name == NULL)
14620 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014621
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014622 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014623
Bram Moolenaar4463f292005-09-25 22:20:24 +000014624 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14625 &xp_arg) == FAIL)
14626 return;
14627 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014628 }
14629
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014630 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014631 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014632 int save_ex_normal_busy = ex_normal_busy;
14633 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014634 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014635 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14636 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014637 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014638 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014639 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014640 && argvars[1].v_type != VAR_UNKNOWN
14641 && argvars[2].v_type != VAR_UNKNOWN)
14642 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14643 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014644
14645 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014647 /* since the user typed this, no need to wait for return */
14648 need_wait_return = FALSE;
14649 msg_didout = FALSE;
14650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014651 cmd_silent = cmd_silent_save;
14652}
14653
14654/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014655 * "input()" function
14656 * Also handles inputsecret() when inputsecret is set.
14657 */
14658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014659f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014660{
14661 get_user_input(argvars, rettv, FALSE);
14662}
14663
14664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665 * "inputdialog()" function
14666 */
14667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014668f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669{
14670#if defined(FEAT_GUI_TEXTDIALOG)
14671 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14672 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14673 {
14674 char_u *message;
14675 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 message = get_tv_string_chk(&argvars[0]);
14679 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014680 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014681 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682 else
14683 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014684 if (message != NULL && defstr != NULL
14685 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014686 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 else
14689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014690 if (message != NULL && defstr != NULL
14691 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014692 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014693 rettv->vval.v_string = vim_strsave(
14694 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014698 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 }
14700 else
14701#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014702 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703}
14704
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014705/*
14706 * "inputlist()" function
14707 */
14708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014709f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014710{
14711 listitem_T *li;
14712 int selected;
14713 int mouse_used;
14714
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014715#ifdef NO_CONSOLE_INPUT
14716 /* While starting up, there is no place to enter text. */
14717 if (no_console_input())
14718 return;
14719#endif
14720 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14721 {
14722 EMSG2(_(e_listarg), "inputlist()");
14723 return;
14724 }
14725
14726 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014727 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014728 lines_left = Rows; /* avoid more prompt */
14729 msg_scroll = TRUE;
14730 msg_clr_eos();
14731
14732 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14733 {
14734 msg_puts(get_tv_string(&li->li_tv));
14735 msg_putchar('\n');
14736 }
14737
14738 /* Ask for choice. */
14739 selected = prompt_for_number(&mouse_used);
14740 if (mouse_used)
14741 selected -= lines_left;
14742
14743 rettv->vval.v_number = selected;
14744}
14745
14746
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14748
14749/*
14750 * "inputrestore()" function
14751 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014753f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014754{
14755 if (ga_userinput.ga_len > 0)
14756 {
14757 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14759 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014760 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 }
14762 else if (p_verbose > 1)
14763 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014764 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014765 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 }
14767}
14768
14769/*
14770 * "inputsave()" function
14771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014773f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014775 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014776 if (ga_grow(&ga_userinput, 1) == OK)
14777 {
14778 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14779 + ga_userinput.ga_len);
14780 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014781 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782 }
14783 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785}
14786
14787/*
14788 * "inputsecret()" function
14789 */
14790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014791f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014792{
14793 ++cmdline_star;
14794 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014795 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014796 --cmdline_star;
14797 --inputsecret_flag;
14798}
14799
14800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014801 * "insert()" function
14802 */
14803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014804f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014805{
14806 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014807 listitem_T *item;
14808 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014809 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014810
14811 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014812 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014813 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014814 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014815 {
14816 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014817 before = get_tv_number_chk(&argvars[2], &error);
14818 if (error)
14819 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014820
Bram Moolenaar758711c2005-02-02 23:11:38 +000014821 if (before == l->lv_len)
14822 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014823 else
14824 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014825 item = list_find(l, before);
14826 if (item == NULL)
14827 {
14828 EMSGN(_(e_listidx), before);
14829 l = NULL;
14830 }
14831 }
14832 if (l != NULL)
14833 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014834 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014835 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014836 }
14837 }
14838}
14839
14840/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014841 * "invert(expr)" function
14842 */
14843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014844f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014845{
14846 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14847}
14848
14849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850 * "isdirectory()" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856}
14857
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014858/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014859 * "islocked()" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014863{
14864 lval_T lv;
14865 char_u *end;
14866 dictitem_T *di;
14867
14868 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014869 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14870 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014871 if (end != NULL && lv.ll_name != NULL)
14872 {
14873 if (*end != NUL)
14874 EMSG(_(e_trailing));
14875 else
14876 {
14877 if (lv.ll_tv == NULL)
14878 {
14879 if (check_changedtick(lv.ll_name))
14880 rettv->vval.v_number = 1; /* always locked */
14881 else
14882 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014883 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014884 if (di != NULL)
14885 {
14886 /* Consider a variable locked when:
14887 * 1. the variable itself is locked
14888 * 2. the value of the variable is locked.
14889 * 3. the List or Dict value is locked.
14890 */
14891 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14892 || tv_islocked(&di->di_tv));
14893 }
14894 }
14895 }
14896 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014897 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014898 else if (lv.ll_newkey != NULL)
14899 EMSG2(_(e_dictkey), lv.ll_newkey);
14900 else if (lv.ll_list != NULL)
14901 /* List item. */
14902 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14903 else
14904 /* Dictionary item. */
14905 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14906 }
14907 }
14908
14909 clear_lval(&lv);
14910}
14911
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014912#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14913/*
14914 * "isnan()" function
14915 */
14916 static void
14917f_isnan(typval_T *argvars, typval_T *rettv)
14918{
14919 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14920 && isnan(argvars[0].vval.v_float);
14921}
14922#endif
14923
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014924static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014925
14926/*
14927 * Turn a dict into a list:
14928 * "what" == 0: list of keys
14929 * "what" == 1: list of values
14930 * "what" == 2: list of items
14931 */
14932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014933dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014934{
Bram Moolenaar33570922005-01-25 22:26:29 +000014935 list_T *l2;
14936 dictitem_T *di;
14937 hashitem_T *hi;
14938 listitem_T *li;
14939 listitem_T *li2;
14940 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014941 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943 if (argvars[0].v_type != VAR_DICT)
14944 {
14945 EMSG(_(e_dictreq));
14946 return;
14947 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014948 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014949 return;
14950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014951 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014952 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014953
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014954 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014955 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014956 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014957 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014958 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014959 --todo;
14960 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014961
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014962 li = listitem_alloc();
14963 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014964 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014965 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014966
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014967 if (what == 0)
14968 {
14969 /* keys() */
14970 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014971 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014972 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14973 }
14974 else if (what == 1)
14975 {
14976 /* values() */
14977 copy_tv(&di->di_tv, &li->li_tv);
14978 }
14979 else
14980 {
14981 /* items() */
14982 l2 = list_alloc();
14983 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014984 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014985 li->li_tv.vval.v_list = l2;
14986 if (l2 == NULL)
14987 break;
14988 ++l2->lv_refcount;
14989
14990 li2 = listitem_alloc();
14991 if (li2 == NULL)
14992 break;
14993 list_append(l2, li2);
14994 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014995 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014996 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14997
14998 li2 = listitem_alloc();
14999 if (li2 == NULL)
15000 break;
15001 list_append(l2, li2);
15002 copy_tv(&di->di_tv, &li2->li_tv);
15003 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015004 }
15005 }
15006}
15007
15008/*
15009 * "items(dict)" function
15010 */
15011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015012f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015013{
15014 dict_list(argvars, rettv, 2);
15015}
15016
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015017#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015018/*
15019 * Get the job from the argument.
15020 * Returns NULL if the job is invalid.
15021 */
15022 static job_T *
15023get_job_arg(typval_T *tv)
15024{
15025 job_T *job;
15026
15027 if (tv->v_type != VAR_JOB)
15028 {
15029 EMSG2(_(e_invarg2), get_tv_string(tv));
15030 return NULL;
15031 }
15032 job = tv->vval.v_job;
15033
15034 if (job == NULL)
15035 EMSG(_("E916: not a valid job"));
15036 return job;
15037}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015038
Bram Moolenaar835dc632016-02-07 14:27:38 +010015039/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015040 * "job_getchannel()" function
15041 */
15042 static void
15043f_job_getchannel(typval_T *argvars, typval_T *rettv)
15044{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015045 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015046
Bram Moolenaar65edff82016-02-21 16:40:11 +010015047 if (job != NULL)
15048 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015049 rettv->v_type = VAR_CHANNEL;
15050 rettv->vval.v_channel = job->jv_channel;
15051 if (job->jv_channel != NULL)
15052 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015053 }
15054}
15055
15056/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015057 * "job_info()" function
15058 */
15059 static void
15060f_job_info(typval_T *argvars, typval_T *rettv)
15061{
15062 job_T *job = get_job_arg(&argvars[0]);
15063
15064 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15065 job_info(job, rettv->vval.v_dict);
15066}
15067
15068/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015069 * "job_setoptions()" function
15070 */
15071 static void
15072f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15073{
15074 job_T *job = get_job_arg(&argvars[0]);
15075 jobopt_T opt;
15076
15077 if (job == NULL)
15078 return;
15079 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015080 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15081 job_set_options(job, &opt);
15082 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015083}
15084
15085/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015086 * "job_start()" function
15087 */
15088 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015089f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015090{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015091 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015092 if (check_restricted() || check_secure())
15093 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015094 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015095}
15096
15097/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098 * "job_status()" function
15099 */
15100 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015101f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015102{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015103 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015104
Bram Moolenaar65edff82016-02-21 16:40:11 +010015105 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015106 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015107 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015108 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015109 }
15110}
15111
15112/*
15113 * "job_stop()" function
15114 */
15115 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015116f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015117{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015118 job_T *job = get_job_arg(&argvars[0]);
15119
15120 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015121 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015122}
15123#endif
15124
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015126 * "join()" function
15127 */
15128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015129f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015130{
15131 garray_T ga;
15132 char_u *sep;
15133
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015134 if (argvars[0].v_type != VAR_LIST)
15135 {
15136 EMSG(_(e_listreq));
15137 return;
15138 }
15139 if (argvars[0].vval.v_list == NULL)
15140 return;
15141 if (argvars[1].v_type == VAR_UNKNOWN)
15142 sep = (char_u *)" ";
15143 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015144 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015145
15146 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015147
15148 if (sep != NULL)
15149 {
15150 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015151 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015152 ga_append(&ga, NUL);
15153 rettv->vval.v_string = (char_u *)ga.ga_data;
15154 }
15155 else
15156 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015157}
15158
15159/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015160 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015161 */
15162 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015163f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015164{
15165 js_read_T reader;
15166
15167 reader.js_buf = get_tv_string(&argvars[0]);
15168 reader.js_fill = NULL;
15169 reader.js_used = 0;
15170 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15171 EMSG(_(e_invarg));
15172}
15173
15174/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015175 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015176 */
15177 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015178f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015179{
15180 rettv->v_type = VAR_STRING;
15181 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15182}
15183
15184/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015185 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015186 */
15187 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015188f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015189{
15190 js_read_T reader;
15191
15192 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015193 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015194 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015195 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015196 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015197}
15198
15199/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015200 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015201 */
15202 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015203f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015204{
15205 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015206 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015207}
15208
15209/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015210 * "keys()" function
15211 */
15212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015213f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015214{
15215 dict_list(argvars, rettv, 0);
15216}
15217
15218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015219 * "last_buffer_nr()" function.
15220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015222f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223{
15224 int n = 0;
15225 buf_T *buf;
15226
15227 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15228 if (n < buf->b_fnum)
15229 n = buf->b_fnum;
15230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015232}
15233
15234/*
15235 * "len()" function
15236 */
15237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015238f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015239{
15240 switch (argvars[0].v_type)
15241 {
15242 case VAR_STRING:
15243 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015244 rettv->vval.v_number = (varnumber_T)STRLEN(
15245 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246 break;
15247 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015248 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015249 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015250 case VAR_DICT:
15251 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15252 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015253 case VAR_UNKNOWN:
15254 case VAR_SPECIAL:
15255 case VAR_FLOAT:
15256 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015257 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015258 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015259 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015260 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015261 break;
15262 }
15263}
15264
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015265static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015266
15267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015268libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015269{
15270#ifdef FEAT_LIBCALL
15271 char_u *string_in;
15272 char_u **string_result;
15273 int nr_result;
15274#endif
15275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015276 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015277 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015278 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279
15280 if (check_restricted() || check_secure())
15281 return;
15282
15283#ifdef FEAT_LIBCALL
15284 /* The first two args must be strings, otherwise its meaningless */
15285 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15286 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015287 string_in = NULL;
15288 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015289 string_in = argvars[2].vval.v_string;
15290 if (type == VAR_NUMBER)
15291 string_result = NULL;
15292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015293 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015294 if (mch_libcall(argvars[0].vval.v_string,
15295 argvars[1].vval.v_string,
15296 string_in,
15297 argvars[2].vval.v_number,
15298 string_result,
15299 &nr_result) == OK
15300 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015301 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015302 }
15303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015304}
15305
15306/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307 * "libcall()" function
15308 */
15309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015310f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015311{
15312 libcall_common(argvars, rettv, VAR_STRING);
15313}
15314
15315/*
15316 * "libcallnr()" function
15317 */
15318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015319f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320{
15321 libcall_common(argvars, rettv, VAR_NUMBER);
15322}
15323
15324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 * "line(string)" function
15326 */
15327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015328f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329{
15330 linenr_T lnum = 0;
15331 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015332 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015334 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 if (fp != NULL)
15336 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338}
15339
15340/*
15341 * "line2byte(lnum)" function
15342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015344f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345{
15346#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348#else
15349 linenr_T lnum;
15350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015355 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15356 if (rettv->vval.v_number >= 0)
15357 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358#endif
15359}
15360
15361/*
15362 * "lispindent(lnum)" function
15363 */
15364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015365f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366{
15367#ifdef FEAT_LISP
15368 pos_T pos;
15369 linenr_T lnum;
15370
15371 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015372 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15374 {
15375 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015376 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015377 curwin->w_cursor = pos;
15378 }
15379 else
15380#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382}
15383
15384/*
15385 * "localtime()" function
15386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015388f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391}
15392
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015393static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394
15395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015396get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 char_u *keys;
15399 char_u *which;
15400 char_u buf[NUMBUFLEN];
15401 char_u *keys_buf = NULL;
15402 char_u *rhs;
15403 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015404 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015405 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015406 mapblock_T *mp;
15407 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015408
15409 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 rettv->v_type = VAR_STRING;
15411 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 if (*keys == NUL)
15415 return;
15416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015417 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015419 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015420 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015421 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015422 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015423 if (argvars[3].v_type != VAR_UNKNOWN)
15424 get_dict = get_tv_number(&argvars[3]);
15425 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 else
15428 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015429 if (which == NULL)
15430 return;
15431
Bram Moolenaar071d4272004-06-13 20:20:40 +000015432 mode = get_map_mode(&which, 0);
15433
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015434 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015435 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015437
15438 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015440 /* Return a string. */
15441 if (rhs != NULL)
15442 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443
Bram Moolenaarbd743252010-10-20 21:23:33 +020015444 }
15445 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15446 {
15447 /* Return a dictionary. */
15448 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15449 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15450 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451
Bram Moolenaarbd743252010-10-20 21:23:33 +020015452 dict_add_nr_str(dict, "lhs", 0L, lhs);
15453 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15454 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15455 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15456 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15457 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15458 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015459 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015460 dict_add_nr_str(dict, "mode", 0L, mapmode);
15461
15462 vim_free(lhs);
15463 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464 }
15465}
15466
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015467#ifdef FEAT_FLOAT
15468/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015469 * "log()" function
15470 */
15471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015472f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015473{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015474 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015475
15476 rettv->v_type = VAR_FLOAT;
15477 if (get_float_arg(argvars, &f) == OK)
15478 rettv->vval.v_float = log(f);
15479 else
15480 rettv->vval.v_float = 0.0;
15481}
15482
15483/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015484 * "log10()" function
15485 */
15486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015487f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015488{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015489 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015490
15491 rettv->v_type = VAR_FLOAT;
15492 if (get_float_arg(argvars, &f) == OK)
15493 rettv->vval.v_float = log10(f);
15494 else
15495 rettv->vval.v_float = 0.0;
15496}
15497#endif
15498
Bram Moolenaar1dced572012-04-05 16:54:08 +020015499#ifdef FEAT_LUA
15500/*
15501 * "luaeval()" function
15502 */
15503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015504f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015505{
15506 char_u *str;
15507 char_u buf[NUMBUFLEN];
15508
15509 str = get_tv_string_buf(&argvars[0], buf);
15510 do_luaeval(str, argvars + 1, rettv);
15511}
15512#endif
15513
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015515 * "map()" function
15516 */
15517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015518f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015519{
15520 filter_map(argvars, rettv, TRUE);
15521}
15522
15523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015524 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 */
15526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530}
15531
15532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015533 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 */
15535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015536f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015538 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015541static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542
15543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015546 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015547 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015548 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549 char_u *pat;
15550 regmatch_T regmatch;
15551 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015552 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 char_u *save_cpo;
15554 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015555 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015556 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015557 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015558 list_T *l = NULL;
15559 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015560 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015561 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562
15563 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15564 save_cpo = p_cpo;
15565 p_cpo = (char_u *)"";
15566
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015567 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015568 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015569 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015570 /* type 3: return empty list when there are no matches.
15571 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015572 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015573 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015574 if (type == 4
15575 && (list_append_string(rettv->vval.v_list,
15576 (char_u *)"", 0) == FAIL
15577 || list_append_number(rettv->vval.v_list,
15578 (varnumber_T)-1) == FAIL
15579 || list_append_number(rettv->vval.v_list,
15580 (varnumber_T)-1) == FAIL
15581 || list_append_number(rettv->vval.v_list,
15582 (varnumber_T)-1) == FAIL))
15583 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015584 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015585 rettv->vval.v_list = NULL;
15586 goto theend;
15587 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015588 }
15589 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015591 rettv->v_type = VAR_STRING;
15592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015595 if (argvars[0].v_type == VAR_LIST)
15596 {
15597 if ((l = argvars[0].vval.v_list) == NULL)
15598 goto theend;
15599 li = l->lv_first;
15600 }
15601 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015602 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015603 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015604 len = (long)STRLEN(str);
15605 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015607 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15608 if (pat == NULL)
15609 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015610
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015611 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015613 int error = FALSE;
15614
15615 start = get_tv_number_chk(&argvars[2], &error);
15616 if (error)
15617 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015618 if (l != NULL)
15619 {
15620 li = list_find(l, start);
15621 if (li == NULL)
15622 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015623 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015624 }
15625 else
15626 {
15627 if (start < 0)
15628 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015629 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015630 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015631 /* When "count" argument is there ignore matches before "start",
15632 * otherwise skip part of the string. Differs when pattern is "^"
15633 * or "\<". */
15634 if (argvars[3].v_type != VAR_UNKNOWN)
15635 startcol = start;
15636 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015637 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015638 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015639 len -= start;
15640 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015641 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015642
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015643 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015644 nth = get_tv_number_chk(&argvars[3], &error);
15645 if (error)
15646 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015647 }
15648
15649 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15650 if (regmatch.regprog != NULL)
15651 {
15652 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015653
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015654 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015655 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015656 if (l != NULL)
15657 {
15658 if (li == NULL)
15659 {
15660 match = FALSE;
15661 break;
15662 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015663 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015664 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015665 if (str == NULL)
15666 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 }
15668
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015669 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015670
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015671 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015672 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015673 if (l == NULL && !match)
15674 break;
15675
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015676 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015677 if (l != NULL)
15678 {
15679 li = li->li_next;
15680 ++idx;
15681 }
15682 else
15683 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015684#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015685 startcol = (colnr_T)(regmatch.startp[0]
15686 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015687#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015688 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015689#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015690 if (startcol > (colnr_T)len
15691 || str + startcol <= regmatch.startp[0])
15692 {
15693 match = FALSE;
15694 break;
15695 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015696 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015697 }
15698
15699 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015701 if (type == 4)
15702 {
15703 listitem_T *li1 = rettv->vval.v_list->lv_first;
15704 listitem_T *li2 = li1->li_next;
15705 listitem_T *li3 = li2->li_next;
15706 listitem_T *li4 = li3->li_next;
15707
15708 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15709 (int)(regmatch.endp[0] - regmatch.startp[0]));
15710 li3->li_tv.vval.v_number =
15711 (varnumber_T)(regmatch.startp[0] - expr);
15712 li4->li_tv.vval.v_number =
15713 (varnumber_T)(regmatch.endp[0] - expr);
15714 if (l != NULL)
15715 li2->li_tv.vval.v_number = (varnumber_T)idx;
15716 }
15717 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015718 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015719 int i;
15720
15721 /* return list with matched string and submatches */
15722 for (i = 0; i < NSUBEXP; ++i)
15723 {
15724 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015725 {
15726 if (list_append_string(rettv->vval.v_list,
15727 (char_u *)"", 0) == FAIL)
15728 break;
15729 }
15730 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015731 regmatch.startp[i],
15732 (int)(regmatch.endp[i] - regmatch.startp[i]))
15733 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015734 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015735 }
15736 }
15737 else if (type == 2)
15738 {
15739 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015740 if (l != NULL)
15741 copy_tv(&li->li_tv, rettv);
15742 else
15743 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015745 }
15746 else if (l != NULL)
15747 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015748 else
15749 {
15750 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015751 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 (varnumber_T)(regmatch.startp[0] - str);
15753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015754 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015756 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 }
15758 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015759 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 }
15761
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015762 if (type == 4 && l == NULL)
15763 /* matchstrpos() without a list: drop the second item. */
15764 listitem_remove(rettv->vval.v_list,
15765 rettv->vval.v_list->lv_first->li_next);
15766
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015768 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 p_cpo = save_cpo;
15770}
15771
15772/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015773 * "match()" function
15774 */
15775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015776f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015777{
15778 find_some_match(argvars, rettv, 1);
15779}
15780
15781/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015782 * "matchadd()" function
15783 */
15784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015785f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015786{
15787#ifdef FEAT_SEARCH_EXTRA
15788 char_u buf[NUMBUFLEN];
15789 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15790 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15791 int prio = 10; /* default priority */
15792 int id = -1;
15793 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015794 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015795
15796 rettv->vval.v_number = -1;
15797
15798 if (grp == NULL || pat == NULL)
15799 return;
15800 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015801 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015802 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015803 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015804 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015805 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015806 if (argvars[4].v_type != VAR_UNKNOWN)
15807 {
15808 if (argvars[4].v_type != VAR_DICT)
15809 {
15810 EMSG(_(e_dictreq));
15811 return;
15812 }
15813 if (dict_find(argvars[4].vval.v_dict,
15814 (char_u *)"conceal", -1) != NULL)
15815 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15816 (char_u *)"conceal", FALSE);
15817 }
15818 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015819 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015820 if (error == TRUE)
15821 return;
15822 if (id >= 1 && id <= 3)
15823 {
15824 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15825 return;
15826 }
15827
Bram Moolenaar6561d522015-07-21 15:48:27 +020015828 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15829 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015830#endif
15831}
15832
15833/*
15834 * "matchaddpos()" function
15835 */
15836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015837f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015838{
15839#ifdef FEAT_SEARCH_EXTRA
15840 char_u buf[NUMBUFLEN];
15841 char_u *group;
15842 int prio = 10;
15843 int id = -1;
15844 int error = FALSE;
15845 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015846 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015847
15848 rettv->vval.v_number = -1;
15849
15850 group = get_tv_string_buf_chk(&argvars[0], buf);
15851 if (group == NULL)
15852 return;
15853
15854 if (argvars[1].v_type != VAR_LIST)
15855 {
15856 EMSG2(_(e_listarg), "matchaddpos()");
15857 return;
15858 }
15859 l = argvars[1].vval.v_list;
15860 if (l == NULL)
15861 return;
15862
15863 if (argvars[2].v_type != VAR_UNKNOWN)
15864 {
15865 prio = get_tv_number_chk(&argvars[2], &error);
15866 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015867 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015868 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015869 if (argvars[4].v_type != VAR_UNKNOWN)
15870 {
15871 if (argvars[4].v_type != VAR_DICT)
15872 {
15873 EMSG(_(e_dictreq));
15874 return;
15875 }
15876 if (dict_find(argvars[4].vval.v_dict,
15877 (char_u *)"conceal", -1) != NULL)
15878 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15879 (char_u *)"conceal", FALSE);
15880 }
15881 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015882 }
15883 if (error == TRUE)
15884 return;
15885
15886 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15887 if (id == 1 || id == 2)
15888 {
15889 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15890 return;
15891 }
15892
Bram Moolenaar6561d522015-07-21 15:48:27 +020015893 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15894 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015895#endif
15896}
15897
15898/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015899 * "matcharg()" function
15900 */
15901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015902f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015903{
15904 if (rettv_list_alloc(rettv) == OK)
15905 {
15906#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015907 int id = get_tv_number(&argvars[0]);
15908 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015909
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015910 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015911 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015912 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15913 {
15914 list_append_string(rettv->vval.v_list,
15915 syn_id2name(m->hlg_id), -1);
15916 list_append_string(rettv->vval.v_list, m->pattern, -1);
15917 }
15918 else
15919 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015920 list_append_string(rettv->vval.v_list, NULL, -1);
15921 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015922 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015923 }
15924#endif
15925 }
15926}
15927
15928/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015929 * "matchdelete()" function
15930 */
15931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015932f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015933{
15934#ifdef FEAT_SEARCH_EXTRA
15935 rettv->vval.v_number = match_delete(curwin,
15936 (int)get_tv_number(&argvars[0]), TRUE);
15937#endif
15938}
15939
15940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941 * "matchend()" function
15942 */
15943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015944f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015945{
15946 find_some_match(argvars, rettv, 0);
15947}
15948
15949/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015950 * "matchlist()" function
15951 */
15952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015953f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015954{
15955 find_some_match(argvars, rettv, 3);
15956}
15957
15958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015959 * "matchstr()" function
15960 */
15961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015962f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015963{
15964 find_some_match(argvars, rettv, 2);
15965}
15966
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015967/*
15968 * "matchstrpos()" function
15969 */
15970 static void
15971f_matchstrpos(typval_T *argvars, typval_T *rettv)
15972{
15973 find_some_match(argvars, rettv, 4);
15974}
15975
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015976static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015977
15978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015979max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015980{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015981 long n = 0;
15982 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015984
15985 if (argvars[0].v_type == VAR_LIST)
15986 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015987 list_T *l;
15988 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015989
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015990 l = argvars[0].vval.v_list;
15991 if (l != NULL)
15992 {
15993 li = l->lv_first;
15994 if (li != NULL)
15995 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015996 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015997 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015998 {
15999 li = li->li_next;
16000 if (li == NULL)
16001 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016002 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016003 if (domax ? i > n : i < n)
16004 n = i;
16005 }
16006 }
16007 }
16008 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016009 else if (argvars[0].v_type == VAR_DICT)
16010 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016011 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016012 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016013 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016014 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016015
16016 d = argvars[0].vval.v_dict;
16017 if (d != NULL)
16018 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016019 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016021 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016022 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016023 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016024 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016025 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016026 if (first)
16027 {
16028 n = i;
16029 first = FALSE;
16030 }
16031 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016032 n = i;
16033 }
16034 }
16035 }
16036 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016037 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016038 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016039 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016040}
16041
16042/*
16043 * "max()" function
16044 */
16045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016046f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016047{
16048 max_min(argvars, rettv, TRUE);
16049}
16050
16051/*
16052 * "min()" function
16053 */
16054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016055f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016056{
16057 max_min(argvars, rettv, FALSE);
16058}
16059
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016060static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016061
16062/*
16063 * Create the directory in which "dir" is located, and higher levels when
16064 * needed.
16065 */
16066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016067mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016068{
16069 char_u *p;
16070 char_u *updir;
16071 int r = FAIL;
16072
16073 /* Get end of directory name in "dir".
16074 * We're done when it's "/" or "c:/". */
16075 p = gettail_sep(dir);
16076 if (p <= get_past_head(dir))
16077 return OK;
16078
16079 /* If the directory exists we're done. Otherwise: create it.*/
16080 updir = vim_strnsave(dir, (int)(p - dir));
16081 if (updir == NULL)
16082 return FAIL;
16083 if (mch_isdir(updir))
16084 r = OK;
16085 else if (mkdir_recurse(updir, prot) == OK)
16086 r = vim_mkdir_emsg(updir, prot);
16087 vim_free(updir);
16088 return r;
16089}
16090
16091#ifdef vim_mkdir
16092/*
16093 * "mkdir()" function
16094 */
16095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016096f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016097{
16098 char_u *dir;
16099 char_u buf[NUMBUFLEN];
16100 int prot = 0755;
16101
16102 rettv->vval.v_number = FAIL;
16103 if (check_restricted() || check_secure())
16104 return;
16105
16106 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016107 if (*dir == NUL)
16108 rettv->vval.v_number = FAIL;
16109 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016110 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016111 if (*gettail(dir) == NUL)
16112 /* remove trailing slashes */
16113 *gettail_sep(dir) = NUL;
16114
16115 if (argvars[1].v_type != VAR_UNKNOWN)
16116 {
16117 if (argvars[2].v_type != VAR_UNKNOWN)
16118 prot = get_tv_number_chk(&argvars[2], NULL);
16119 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16120 mkdir_recurse(dir, prot);
16121 }
16122 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016123 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016124}
16125#endif
16126
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128 * "mode()" function
16129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016131f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016133 char_u buf[3];
16134
16135 buf[1] = NUL;
16136 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137
Bram Moolenaar071d4272004-06-13 20:20:40 +000016138 if (VIsual_active)
16139 {
16140 if (VIsual_select)
16141 buf[0] = VIsual_mode + 's' - 'v';
16142 else
16143 buf[0] = VIsual_mode;
16144 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016145 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016146 || State == CONFIRM)
16147 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016149 if (State == ASKMORE)
16150 buf[1] = 'm';
16151 else if (State == CONFIRM)
16152 buf[1] = '?';
16153 }
16154 else if (State == EXTERNCMD)
16155 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 else if (State & INSERT)
16157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016158#ifdef FEAT_VREPLACE
16159 if (State & VREPLACE_FLAG)
16160 {
16161 buf[0] = 'R';
16162 buf[1] = 'v';
16163 }
16164 else
16165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 if (State & REPLACE_FLAG)
16167 buf[0] = 'R';
16168 else
16169 buf[0] = 'i';
16170 }
16171 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016174 if (exmode_active)
16175 buf[1] = 'v';
16176 }
16177 else if (exmode_active)
16178 {
16179 buf[0] = 'c';
16180 buf[1] = 'e';
16181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016185 if (finish_op)
16186 buf[1] = 'o';
16187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016189 /* Clear out the minor mode when the argument is not a non-zero number or
16190 * non-empty string. */
16191 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016192 buf[1] = NUL;
16193
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016194 rettv->vval.v_string = vim_strsave(buf);
16195 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196}
16197
Bram Moolenaar429fa852013-04-15 12:27:36 +020016198#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016199/*
16200 * "mzeval()" function
16201 */
16202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016203f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016204{
16205 char_u *str;
16206 char_u buf[NUMBUFLEN];
16207
16208 str = get_tv_string_buf(&argvars[0], buf);
16209 do_mzeval(str, rettv);
16210}
Bram Moolenaar75676462013-01-30 14:55:42 +010016211
16212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016213mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016214{
16215 typval_T argvars[3];
16216
16217 argvars[0].v_type = VAR_STRING;
16218 argvars[0].vval.v_string = name;
16219 copy_tv(args, &argvars[1]);
16220 argvars[2].v_type = VAR_UNKNOWN;
16221 f_call(argvars, rettv);
16222 clear_tv(&argvars[1]);
16223}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016224#endif
16225
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016227 * "nextnonblank()" function
16228 */
16229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016230f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016231{
16232 linenr_T lnum;
16233
16234 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16235 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016236 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016237 {
16238 lnum = 0;
16239 break;
16240 }
16241 if (*skipwhite(ml_get(lnum)) != NUL)
16242 break;
16243 }
16244 rettv->vval.v_number = lnum;
16245}
16246
16247/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 * "nr2char()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252{
16253 char_u buf[NUMBUFLEN];
16254
16255#ifdef FEAT_MBYTE
16256 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016257 {
16258 int utf8 = 0;
16259
16260 if (argvars[1].v_type != VAR_UNKNOWN)
16261 utf8 = get_tv_number_chk(&argvars[1], NULL);
16262 if (utf8)
16263 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16264 else
16265 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 else
16268#endif
16269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 buf[1] = NUL;
16272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016273 rettv->v_type = VAR_STRING;
16274 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016275}
16276
16277/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016278 * "or(expr, expr)" function
16279 */
16280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016281f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016282{
16283 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16284 | get_tv_number_chk(&argvars[1], NULL);
16285}
16286
16287/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016288 * "pathshorten()" function
16289 */
16290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016291f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016292{
16293 char_u *p;
16294
16295 rettv->v_type = VAR_STRING;
16296 p = get_tv_string_chk(&argvars[0]);
16297 if (p == NULL)
16298 rettv->vval.v_string = NULL;
16299 else
16300 {
16301 p = vim_strsave(p);
16302 rettv->vval.v_string = p;
16303 if (p != NULL)
16304 shorten_dir(p);
16305 }
16306}
16307
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016308#ifdef FEAT_PERL
16309/*
16310 * "perleval()" function
16311 */
16312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016313f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016314{
16315 char_u *str;
16316 char_u buf[NUMBUFLEN];
16317
16318 str = get_tv_string_buf(&argvars[0], buf);
16319 do_perleval(str, rettv);
16320}
16321#endif
16322
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016323#ifdef FEAT_FLOAT
16324/*
16325 * "pow()" function
16326 */
16327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016328f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016329{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016330 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016331
16332 rettv->v_type = VAR_FLOAT;
16333 if (get_float_arg(argvars, &fx) == OK
16334 && get_float_arg(&argvars[1], &fy) == OK)
16335 rettv->vval.v_float = pow(fx, fy);
16336 else
16337 rettv->vval.v_float = 0.0;
16338}
16339#endif
16340
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016341/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016342 * "prevnonblank()" function
16343 */
16344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016345f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016346{
16347 linenr_T lnum;
16348
16349 lnum = get_tv_lnum(argvars);
16350 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16351 lnum = 0;
16352 else
16353 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16354 --lnum;
16355 rettv->vval.v_number = lnum;
16356}
16357
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016358/* This dummy va_list is here because:
16359 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16360 * - locally in the function results in a "used before set" warning
16361 * - using va_start() to initialize it gives "function with fixed args" error */
16362static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016363
Bram Moolenaar8c711452005-01-14 21:53:12 +000016364/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016365 * "printf()" function
16366 */
16367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016369{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016370 char_u buf[NUMBUFLEN];
16371 int len;
16372 char_u *s;
16373 int saved_did_emsg = did_emsg;
16374 char *fmt;
16375
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016376 rettv->v_type = VAR_STRING;
16377 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016378
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016379 /* Get the required length, allocate the buffer and do it for real. */
16380 did_emsg = FALSE;
16381 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16382 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16383 if (!did_emsg)
16384 {
16385 s = alloc(len + 1);
16386 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016387 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016388 rettv->vval.v_string = s;
16389 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016390 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016391 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016392 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016393}
16394
16395/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016396 * "pumvisible()" function
16397 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016399f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016400{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016401#ifdef FEAT_INS_EXPAND
16402 if (pum_visible())
16403 rettv->vval.v_number = 1;
16404#endif
16405}
16406
Bram Moolenaardb913952012-06-29 12:54:53 +020016407#ifdef FEAT_PYTHON3
16408/*
16409 * "py3eval()" function
16410 */
16411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016412f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016413{
16414 char_u *str;
16415 char_u buf[NUMBUFLEN];
16416
16417 str = get_tv_string_buf(&argvars[0], buf);
16418 do_py3eval(str, rettv);
16419}
16420#endif
16421
16422#ifdef FEAT_PYTHON
16423/*
16424 * "pyeval()" function
16425 */
16426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016427f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016428{
16429 char_u *str;
16430 char_u buf[NUMBUFLEN];
16431
16432 str = get_tv_string_buf(&argvars[0], buf);
16433 do_pyeval(str, rettv);
16434}
16435#endif
16436
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016437/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016438 * "range()" function
16439 */
16440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016441f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016442{
16443 long start;
16444 long end;
16445 long stride = 1;
16446 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016447 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016450 if (argvars[1].v_type == VAR_UNKNOWN)
16451 {
16452 end = start - 1;
16453 start = 0;
16454 }
16455 else
16456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016457 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016458 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016459 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016460 }
16461
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016462 if (error)
16463 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016464 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016465 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016466 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016467 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016468 else
16469 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016470 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016471 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016472 if (list_append_number(rettv->vval.v_list,
16473 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016474 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016475 }
16476}
16477
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016478/*
16479 * "readfile()" function
16480 */
16481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016482f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016483{
16484 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016485 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016486 char_u *fname;
16487 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016488 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16489 int io_size = sizeof(buf);
16490 int readlen; /* size of last fread() */
16491 char_u *prev = NULL; /* previously read bytes, if any */
16492 long prevlen = 0; /* length of data in prev */
16493 long prevsize = 0; /* size of prev buffer */
16494 long maxline = MAXLNUM;
16495 long cnt = 0;
16496 char_u *p; /* position in buf */
16497 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016498
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016499 if (argvars[1].v_type != VAR_UNKNOWN)
16500 {
16501 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16502 binary = TRUE;
16503 if (argvars[2].v_type != VAR_UNKNOWN)
16504 maxline = get_tv_number(&argvars[2]);
16505 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016506
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016507 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016508 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016509
16510 /* Always open the file in binary mode, library functions have a mind of
16511 * their own about CR-LF conversion. */
16512 fname = get_tv_string(&argvars[0]);
16513 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16514 {
16515 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16516 return;
16517 }
16518
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016519 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016520 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016521 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016522
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016523 /* This for loop processes what was read, but is also entered at end
16524 * of file so that either:
16525 * - an incomplete line gets written
16526 * - a "binary" file gets an empty line at the end if it ends in a
16527 * newline. */
16528 for (p = buf, start = buf;
16529 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16530 ++p)
16531 {
16532 if (*p == '\n' || readlen <= 0)
16533 {
16534 listitem_T *li;
16535 char_u *s = NULL;
16536 long_u len = p - start;
16537
16538 /* Finished a line. Remove CRs before NL. */
16539 if (readlen > 0 && !binary)
16540 {
16541 while (len > 0 && start[len - 1] == '\r')
16542 --len;
16543 /* removal may cross back to the "prev" string */
16544 if (len == 0)
16545 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16546 --prevlen;
16547 }
16548 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016549 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016550 else
16551 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016552 /* Change "prev" buffer to be the right size. This way
16553 * the bytes are only copied once, and very long lines are
16554 * allocated only once. */
16555 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016558 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016559 prev = NULL; /* the list will own the string */
16560 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016561 }
16562 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016563 if (s == NULL)
16564 {
16565 do_outofmem_msg((long_u) prevlen + len + 1);
16566 failed = TRUE;
16567 break;
16568 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016569
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016570 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016571 {
16572 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016573 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016574 break;
16575 }
16576 li->li_tv.v_type = VAR_STRING;
16577 li->li_tv.v_lock = 0;
16578 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016579 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016580
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016581 start = p + 1; /* step over newline */
16582 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016583 break;
16584 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016585 else if (*p == NUL)
16586 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016587#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016588 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16589 * when finding the BF and check the previous two bytes. */
16590 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016591 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016592 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16593 * + 1, these may be in the "prev" string. */
16594 char_u back1 = p >= buf + 1 ? p[-1]
16595 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16596 char_u back2 = p >= buf + 2 ? p[-2]
16597 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16598 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016599
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016600 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016601 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016602 char_u *dest = p - 2;
16603
16604 /* Usually a BOM is at the beginning of a file, and so at
16605 * the beginning of a line; then we can just step over it.
16606 */
16607 if (start == dest)
16608 start = p + 1;
16609 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016610 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 /* have to shuffle buf to close gap */
16612 int adjust_prevlen = 0;
16613
16614 if (dest < buf)
16615 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016616 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016617 dest = buf;
16618 }
16619 if (readlen > p - buf + 1)
16620 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16621 readlen -= 3 - adjust_prevlen;
16622 prevlen -= adjust_prevlen;
16623 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016624 }
16625 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016626 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016627#endif
16628 } /* for */
16629
16630 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16631 break;
16632 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016633 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016634 /* There's part of a line in buf, store it in "prev". */
16635 if (p - start + prevlen >= prevsize)
16636 {
16637 /* need bigger "prev" buffer */
16638 char_u *newprev;
16639
16640 /* A common use case is ordinary text files and "prev" gets a
16641 * fragment of a line, so the first allocation is made
16642 * small, to avoid repeatedly 'allocing' large and
16643 * 'reallocing' small. */
16644 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016645 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 else
16647 {
16648 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016649 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016650 prevsize = grow50pc > growmin ? grow50pc : growmin;
16651 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016652 newprev = prev == NULL ? alloc(prevsize)
16653 : vim_realloc(prev, prevsize);
16654 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016655 {
16656 do_outofmem_msg((long_u)prevsize);
16657 failed = TRUE;
16658 break;
16659 }
16660 prev = newprev;
16661 }
16662 /* Add the line part to end of "prev". */
16663 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016664 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016665 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016666 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016667
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016668 /*
16669 * For a negative line count use only the lines at the end of the file,
16670 * free the rest.
16671 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016672 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016673 while (cnt > -maxline)
16674 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016675 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016676 --cnt;
16677 }
16678
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016679 if (failed)
16680 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016681 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016682 /* readfile doc says an empty list is returned on error */
16683 rettv->vval.v_list = list_alloc();
16684 }
16685
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016686 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016687 fclose(fd);
16688}
16689
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016690#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016691static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016692
16693/*
16694 * Convert a List to proftime_T.
16695 * Return FAIL when there is something wrong.
16696 */
16697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016698list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016699{
16700 long n1, n2;
16701 int error = FALSE;
16702
16703 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16704 || arg->vval.v_list->lv_len != 2)
16705 return FAIL;
16706 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16707 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16708# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016709 tm->HighPart = n1;
16710 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016711# else
16712 tm->tv_sec = n1;
16713 tm->tv_usec = n2;
16714# endif
16715 return error ? FAIL : OK;
16716}
16717#endif /* FEAT_RELTIME */
16718
16719/*
16720 * "reltime()" function
16721 */
16722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016723f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016724{
16725#ifdef FEAT_RELTIME
16726 proftime_T res;
16727 proftime_T start;
16728
16729 if (argvars[0].v_type == VAR_UNKNOWN)
16730 {
16731 /* No arguments: get current time. */
16732 profile_start(&res);
16733 }
16734 else if (argvars[1].v_type == VAR_UNKNOWN)
16735 {
16736 if (list2proftime(&argvars[0], &res) == FAIL)
16737 return;
16738 profile_end(&res);
16739 }
16740 else
16741 {
16742 /* Two arguments: compute the difference. */
16743 if (list2proftime(&argvars[0], &start) == FAIL
16744 || list2proftime(&argvars[1], &res) == FAIL)
16745 return;
16746 profile_sub(&res, &start);
16747 }
16748
16749 if (rettv_list_alloc(rettv) == OK)
16750 {
16751 long n1, n2;
16752
16753# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016754 n1 = res.HighPart;
16755 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016756# else
16757 n1 = res.tv_sec;
16758 n2 = res.tv_usec;
16759# endif
16760 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16761 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16762 }
16763#endif
16764}
16765
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016766#ifdef FEAT_FLOAT
16767/*
16768 * "reltimefloat()" function
16769 */
16770 static void
16771f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16772{
16773# ifdef FEAT_RELTIME
16774 proftime_T tm;
16775# endif
16776
16777 rettv->v_type = VAR_FLOAT;
16778 rettv->vval.v_float = 0;
16779# ifdef FEAT_RELTIME
16780 if (list2proftime(&argvars[0], &tm) == OK)
16781 rettv->vval.v_float = profile_float(&tm);
16782# endif
16783}
16784#endif
16785
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016786/*
16787 * "reltimestr()" function
16788 */
16789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016790f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016791{
16792#ifdef FEAT_RELTIME
16793 proftime_T tm;
16794#endif
16795
16796 rettv->v_type = VAR_STRING;
16797 rettv->vval.v_string = NULL;
16798#ifdef FEAT_RELTIME
16799 if (list2proftime(&argvars[0], &tm) == OK)
16800 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16801#endif
16802}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016803
Bram Moolenaar0d660222005-01-07 21:51:51 +000016804#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016805static void make_connection(void);
16806static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016807
16808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016809make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016810{
16811 if (X_DISPLAY == NULL
16812# ifdef FEAT_GUI
16813 && !gui.in_use
16814# endif
16815 )
16816 {
16817 x_force_connect = TRUE;
16818 setup_term_clip();
16819 x_force_connect = FALSE;
16820 }
16821}
16822
16823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016824check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825{
16826 make_connection();
16827 if (X_DISPLAY == NULL)
16828 {
16829 EMSG(_("E240: No connection to Vim server"));
16830 return FAIL;
16831 }
16832 return OK;
16833}
16834#endif
16835
16836#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016838remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016839{
16840 char_u *server_name;
16841 char_u *keys;
16842 char_u *r = NULL;
16843 char_u buf[NUMBUFLEN];
16844# ifdef WIN32
16845 HWND w;
16846# else
16847 Window w;
16848# endif
16849
16850 if (check_restricted() || check_secure())
16851 return;
16852
16853# ifdef FEAT_X11
16854 if (check_connection() == FAIL)
16855 return;
16856# endif
16857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016858 server_name = get_tv_string_chk(&argvars[0]);
16859 if (server_name == NULL)
16860 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016861 keys = get_tv_string_buf(&argvars[1], buf);
16862# ifdef WIN32
16863 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16864# else
16865 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16866 < 0)
16867# endif
16868 {
16869 if (r != NULL)
16870 EMSG(r); /* sending worked but evaluation failed */
16871 else
16872 EMSG2(_("E241: Unable to send to %s"), server_name);
16873 return;
16874 }
16875
16876 rettv->vval.v_string = r;
16877
16878 if (argvars[2].v_type != VAR_UNKNOWN)
16879 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016880 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016881 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016882 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016884 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016885 v.di_tv.v_type = VAR_STRING;
16886 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016887 idvar = get_tv_string_chk(&argvars[2]);
16888 if (idvar != NULL)
16889 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016890 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891 }
16892}
16893#endif
16894
16895/*
16896 * "remote_expr()" function
16897 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016899f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900{
16901 rettv->v_type = VAR_STRING;
16902 rettv->vval.v_string = NULL;
16903#ifdef FEAT_CLIENTSERVER
16904 remote_common(argvars, rettv, TRUE);
16905#endif
16906}
16907
16908/*
16909 * "remote_foreground()" function
16910 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016912f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914#ifdef FEAT_CLIENTSERVER
16915# ifdef WIN32
16916 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016917 {
16918 char_u *server_name = get_tv_string_chk(&argvars[0]);
16919
16920 if (server_name != NULL)
16921 serverForeground(server_name);
16922 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923# else
16924 /* Send a foreground() expression to the server. */
16925 argvars[1].v_type = VAR_STRING;
16926 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16927 argvars[2].v_type = VAR_UNKNOWN;
16928 remote_common(argvars, rettv, TRUE);
16929 vim_free(argvars[1].vval.v_string);
16930# endif
16931#endif
16932}
16933
Bram Moolenaar0d660222005-01-07 21:51:51 +000016934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936{
16937#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016938 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 char_u *s = NULL;
16940# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016941 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016942# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016943 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944
16945 if (check_restricted() || check_secure())
16946 {
16947 rettv->vval.v_number = -1;
16948 return;
16949 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016950 serverid = get_tv_string_chk(&argvars[0]);
16951 if (serverid == NULL)
16952 {
16953 rettv->vval.v_number = -1;
16954 return; /* type error; errmsg already given */
16955 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016957 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958 if (n == 0)
16959 rettv->vval.v_number = -1;
16960 else
16961 {
16962 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16963 rettv->vval.v_number = (s != NULL);
16964 }
16965# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 if (check_connection() == FAIL)
16967 return;
16968
16969 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016970 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971# endif
16972
16973 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016975 char_u *retvar;
16976
Bram Moolenaar33570922005-01-25 22:26:29 +000016977 v.di_tv.v_type = VAR_STRING;
16978 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 retvar = get_tv_string_chk(&argvars[1]);
16980 if (retvar != NULL)
16981 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016982 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 }
16984#else
16985 rettv->vval.v_number = -1;
16986#endif
16987}
16988
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016990f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991{
16992 char_u *r = NULL;
16993
16994#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016995 char_u *serverid = get_tv_string_chk(&argvars[0]);
16996
16997 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 {
16999# ifdef WIN32
17000 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017001 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017003 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017004 if (n != 0)
17005 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17006 if (r == NULL)
17007# else
17008 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017009 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010# endif
17011 EMSG(_("E277: Unable to read a server reply"));
17012 }
17013#endif
17014 rettv->v_type = VAR_STRING;
17015 rettv->vval.v_string = r;
17016}
17017
17018/*
17019 * "remote_send()" function
17020 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017022f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023{
17024 rettv->v_type = VAR_STRING;
17025 rettv->vval.v_string = NULL;
17026#ifdef FEAT_CLIENTSERVER
17027 remote_common(argvars, rettv, FALSE);
17028#endif
17029}
17030
17031/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017032 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017033 */
17034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017035f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017036{
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 list_T *l;
17038 listitem_T *item, *item2;
17039 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017040 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017041 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017042 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017043 dict_T *d;
17044 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017045 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017046
Bram Moolenaar8c711452005-01-14 21:53:12 +000017047 if (argvars[0].v_type == VAR_DICT)
17048 {
17049 if (argvars[2].v_type != VAR_UNKNOWN)
17050 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017051 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017052 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017054 key = get_tv_string_chk(&argvars[1]);
17055 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017056 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017057 di = dict_find(d, key, -1);
17058 if (di == NULL)
17059 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017060 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17061 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017062 {
17063 *rettv = di->di_tv;
17064 init_tv(&di->di_tv);
17065 dictitem_remove(d, di);
17066 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017068 }
17069 }
17070 else if (argvars[0].v_type != VAR_LIST)
17071 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017072 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017073 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017075 int error = FALSE;
17076
17077 idx = get_tv_number_chk(&argvars[1], &error);
17078 if (error)
17079 ; /* type error: do nothing, errmsg already given */
17080 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017081 EMSGN(_(e_listidx), idx);
17082 else
17083 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017084 if (argvars[2].v_type == VAR_UNKNOWN)
17085 {
17086 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017087 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017088 *rettv = item->li_tv;
17089 vim_free(item);
17090 }
17091 else
17092 {
17093 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017094 end = get_tv_number_chk(&argvars[2], &error);
17095 if (error)
17096 ; /* type error: do nothing */
17097 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017098 EMSGN(_(e_listidx), end);
17099 else
17100 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017101 int cnt = 0;
17102
17103 for (li = item; li != NULL; li = li->li_next)
17104 {
17105 ++cnt;
17106 if (li == item2)
17107 break;
17108 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017109 if (li == NULL) /* didn't find "item2" after "item" */
17110 EMSG(_(e_invrange));
17111 else
17112 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017113 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017114 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017115 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017116 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017117 l->lv_first = item;
17118 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017119 item->li_prev = NULL;
17120 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017121 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017122 }
17123 }
17124 }
17125 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017126 }
17127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017128}
17129
17130/*
17131 * "rename({from}, {to})" function
17132 */
17133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017134f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017135{
17136 char_u buf[NUMBUFLEN];
17137
17138 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017139 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017141 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17142 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143}
17144
17145/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017146 * "repeat()" function
17147 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017149f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017150{
17151 char_u *p;
17152 int n;
17153 int slen;
17154 int len;
17155 char_u *r;
17156 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157
17158 n = get_tv_number(&argvars[1]);
17159 if (argvars[0].v_type == VAR_LIST)
17160 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017161 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017162 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163 if (list_extend(rettv->vval.v_list,
17164 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017165 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017166 }
17167 else
17168 {
17169 p = get_tv_string(&argvars[0]);
17170 rettv->v_type = VAR_STRING;
17171 rettv->vval.v_string = NULL;
17172
17173 slen = (int)STRLEN(p);
17174 len = slen * n;
17175 if (len <= 0)
17176 return;
17177
17178 r = alloc(len + 1);
17179 if (r != NULL)
17180 {
17181 for (i = 0; i < n; i++)
17182 mch_memmove(r + i * slen, p, (size_t)slen);
17183 r[len] = NUL;
17184 }
17185
17186 rettv->vval.v_string = r;
17187 }
17188}
17189
17190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 * "resolve()" function
17192 */
17193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017194f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017195{
17196 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017197#ifdef HAVE_READLINK
17198 char_u *buf = NULL;
17199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202#ifdef FEAT_SHORTCUT
17203 {
17204 char_u *v = NULL;
17205
17206 v = mch_resolve_shortcut(p);
17207 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017208 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 }
17212#else
17213# ifdef HAVE_READLINK
17214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 char_u *cpy;
17216 int len;
17217 char_u *remain = NULL;
17218 char_u *q;
17219 int is_relative_to_current = FALSE;
17220 int has_trailing_pathsep = FALSE;
17221 int limit = 100;
17222
17223 p = vim_strsave(p);
17224
17225 if (p[0] == '.' && (vim_ispathsep(p[1])
17226 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17227 is_relative_to_current = TRUE;
17228
17229 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017230 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017232 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017233 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235
17236 q = getnextcomp(p);
17237 if (*q != NUL)
17238 {
17239 /* Separate the first path component in "p", and keep the
17240 * remainder (beginning with the path separator). */
17241 remain = vim_strsave(q - 1);
17242 q[-1] = NUL;
17243 }
17244
Bram Moolenaard9462e32011-04-11 21:35:11 +020017245 buf = alloc(MAXPATHL + 1);
17246 if (buf == NULL)
17247 goto fail;
17248
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 for (;;)
17250 {
17251 for (;;)
17252 {
17253 len = readlink((char *)p, (char *)buf, MAXPATHL);
17254 if (len <= 0)
17255 break;
17256 buf[len] = NUL;
17257
17258 if (limit-- == 0)
17259 {
17260 vim_free(p);
17261 vim_free(remain);
17262 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017263 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017264 goto fail;
17265 }
17266
17267 /* Ensure that the result will have a trailing path separator
17268 * if the argument has one. */
17269 if (remain == NULL && has_trailing_pathsep)
17270 add_pathsep(buf);
17271
17272 /* Separate the first path component in the link value and
17273 * concatenate the remainders. */
17274 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17275 if (*q != NUL)
17276 {
17277 if (remain == NULL)
17278 remain = vim_strsave(q - 1);
17279 else
17280 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017281 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282 if (cpy != NULL)
17283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 vim_free(remain);
17285 remain = cpy;
17286 }
17287 }
17288 q[-1] = NUL;
17289 }
17290
17291 q = gettail(p);
17292 if (q > p && *q == NUL)
17293 {
17294 /* Ignore trailing path separator. */
17295 q[-1] = NUL;
17296 q = gettail(p);
17297 }
17298 if (q > p && !mch_isFullName(buf))
17299 {
17300 /* symlink is relative to directory of argument */
17301 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17302 if (cpy != NULL)
17303 {
17304 STRCPY(cpy, p);
17305 STRCPY(gettail(cpy), buf);
17306 vim_free(p);
17307 p = cpy;
17308 }
17309 }
17310 else
17311 {
17312 vim_free(p);
17313 p = vim_strsave(buf);
17314 }
17315 }
17316
17317 if (remain == NULL)
17318 break;
17319
17320 /* Append the first path component of "remain" to "p". */
17321 q = getnextcomp(remain + 1);
17322 len = q - remain - (*q != NUL);
17323 cpy = vim_strnsave(p, STRLEN(p) + len);
17324 if (cpy != NULL)
17325 {
17326 STRNCAT(cpy, remain, len);
17327 vim_free(p);
17328 p = cpy;
17329 }
17330 /* Shorten "remain". */
17331 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017332 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333 else
17334 {
17335 vim_free(remain);
17336 remain = NULL;
17337 }
17338 }
17339
17340 /* If the result is a relative path name, make it explicitly relative to
17341 * the current directory if and only if the argument had this form. */
17342 if (!vim_ispathsep(*p))
17343 {
17344 if (is_relative_to_current
17345 && *p != NUL
17346 && !(p[0] == '.'
17347 && (p[1] == NUL
17348 || vim_ispathsep(p[1])
17349 || (p[1] == '.'
17350 && (p[2] == NUL
17351 || vim_ispathsep(p[2]))))))
17352 {
17353 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017354 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355 if (cpy != NULL)
17356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 vim_free(p);
17358 p = cpy;
17359 }
17360 }
17361 else if (!is_relative_to_current)
17362 {
17363 /* Strip leading "./". */
17364 q = p;
17365 while (q[0] == '.' && vim_ispathsep(q[1]))
17366 q += 2;
17367 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017368 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369 }
17370 }
17371
17372 /* Ensure that the result will have no trailing path separator
17373 * if the argument had none. But keep "/" or "//". */
17374 if (!has_trailing_pathsep)
17375 {
17376 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017377 if (after_pathsep(p, q))
17378 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017379 }
17380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017381 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 }
17383# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017384 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017385# endif
17386#endif
17387
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017388 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389
17390#ifdef HAVE_READLINK
17391fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017392 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017393#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017394 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017395}
17396
17397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017398 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399 */
17400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017401f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402{
Bram Moolenaar33570922005-01-25 22:26:29 +000017403 list_T *l;
17404 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405
Bram Moolenaar0d660222005-01-07 21:51:51 +000017406 if (argvars[0].v_type != VAR_LIST)
17407 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017408 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017409 && !tv_check_lock(l->lv_lock,
17410 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017411 {
17412 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017413 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017414 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017415 while (li != NULL)
17416 {
17417 ni = li->li_prev;
17418 list_append(l, li);
17419 li = ni;
17420 }
17421 rettv->vval.v_list = l;
17422 rettv->v_type = VAR_LIST;
17423 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017424 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017426}
17427
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017428#define SP_NOMOVE 0x01 /* don't move cursor */
17429#define SP_REPEAT 0x02 /* repeat to find outer pair */
17430#define SP_RETCOUNT 0x04 /* return matchcount */
17431#define SP_SETPCMARK 0x08 /* set previous context mark */
17432#define SP_START 0x10 /* accept match at start position */
17433#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17434#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017435#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017436
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017437static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017438
17439/*
17440 * Get flags for a search function.
17441 * Possibly sets "p_ws".
17442 * Returns BACKWARD, FORWARD or zero (for an error).
17443 */
17444 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017445get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017446{
17447 int dir = FORWARD;
17448 char_u *flags;
17449 char_u nbuf[NUMBUFLEN];
17450 int mask;
17451
17452 if (varp->v_type != VAR_UNKNOWN)
17453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017454 flags = get_tv_string_buf_chk(varp, nbuf);
17455 if (flags == NULL)
17456 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017457 while (*flags != NUL)
17458 {
17459 switch (*flags)
17460 {
17461 case 'b': dir = BACKWARD; break;
17462 case 'w': p_ws = TRUE; break;
17463 case 'W': p_ws = FALSE; break;
17464 default: mask = 0;
17465 if (flagsp != NULL)
17466 switch (*flags)
17467 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017468 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017469 case 'e': mask = SP_END; break;
17470 case 'm': mask = SP_RETCOUNT; break;
17471 case 'n': mask = SP_NOMOVE; break;
17472 case 'p': mask = SP_SUBPAT; break;
17473 case 'r': mask = SP_REPEAT; break;
17474 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017475 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017476 }
17477 if (mask == 0)
17478 {
17479 EMSG2(_(e_invarg2), flags);
17480 dir = 0;
17481 }
17482 else
17483 *flagsp |= mask;
17484 }
17485 if (dir == 0)
17486 break;
17487 ++flags;
17488 }
17489 }
17490 return dir;
17491}
17492
Bram Moolenaar071d4272004-06-13 20:20:40 +000017493/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017494 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017496 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017497search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017499 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017500 char_u *pat;
17501 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017502 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 int save_p_ws = p_ws;
17504 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017505 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017506 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017507 proftime_T tm;
17508#ifdef FEAT_RELTIME
17509 long time_limit = 0;
17510#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017511 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017512 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017515 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017516 if (dir == 0)
17517 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017518 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017519 if (flags & SP_START)
17520 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017521 if (flags & SP_END)
17522 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017523 if (flags & SP_COLUMN)
17524 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017525
Bram Moolenaar76929292008-01-06 19:07:36 +000017526 /* Optional arguments: line number to stop searching and timeout. */
17527 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017528 {
17529 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17530 if (lnum_stop < 0)
17531 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017532#ifdef FEAT_RELTIME
17533 if (argvars[3].v_type != VAR_UNKNOWN)
17534 {
17535 time_limit = get_tv_number_chk(&argvars[3], NULL);
17536 if (time_limit < 0)
17537 goto theend;
17538 }
17539#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017540 }
17541
Bram Moolenaar76929292008-01-06 19:07:36 +000017542#ifdef FEAT_RELTIME
17543 /* Set the time limit, if there is one. */
17544 profile_setlimit(time_limit, &tm);
17545#endif
17546
Bram Moolenaar231334e2005-07-25 20:46:57 +000017547 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017548 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017549 * Check to make sure only those flags are set.
17550 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17551 * flags cannot be set. Check for that condition also.
17552 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017553 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017554 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017555 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017556 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017557 goto theend;
17558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017560 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017561 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017562 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017563 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017565 if (flags & SP_SUBPAT)
17566 retval = subpatnum;
17567 else
17568 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017569 if (flags & SP_SETPCMARK)
17570 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017572 if (match_pos != NULL)
17573 {
17574 /* Store the match cursor position */
17575 match_pos->lnum = pos.lnum;
17576 match_pos->col = pos.col + 1;
17577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 /* "/$" will put the cursor after the end of the line, may need to
17579 * correct that here */
17580 check_cursor();
17581 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017582
17583 /* If 'n' flag is used: restore cursor position. */
17584 if (flags & SP_NOMOVE)
17585 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017586 else
17587 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017588theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017590
17591 return retval;
17592}
17593
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017595
17596/*
17597 * round() is not in C90, use ceil() or floor() instead.
17598 */
17599 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017600vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017601{
17602 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17603}
17604
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017605/*
17606 * "round({float})" function
17607 */
17608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017609f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017610{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017611 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017612
17613 rettv->v_type = VAR_FLOAT;
17614 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017615 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017616 else
17617 rettv->vval.v_float = 0.0;
17618}
17619#endif
17620
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017621/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017622 * "screenattr()" function
17623 */
17624 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017625f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017626{
17627 int row;
17628 int col;
17629 int c;
17630
17631 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17632 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17633 if (row < 0 || row >= screen_Rows
17634 || col < 0 || col >= screen_Columns)
17635 c = -1;
17636 else
17637 c = ScreenAttrs[LineOffset[row] + col];
17638 rettv->vval.v_number = c;
17639}
17640
17641/*
17642 * "screenchar()" function
17643 */
17644 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017645f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017646{
17647 int row;
17648 int col;
17649 int off;
17650 int c;
17651
17652 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17653 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17654 if (row < 0 || row >= screen_Rows
17655 || col < 0 || col >= screen_Columns)
17656 c = -1;
17657 else
17658 {
17659 off = LineOffset[row] + col;
17660#ifdef FEAT_MBYTE
17661 if (enc_utf8 && ScreenLinesUC[off] != 0)
17662 c = ScreenLinesUC[off];
17663 else
17664#endif
17665 c = ScreenLines[off];
17666 }
17667 rettv->vval.v_number = c;
17668}
17669
17670/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017671 * "screencol()" function
17672 *
17673 * First column is 1 to be consistent with virtcol().
17674 */
17675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017676f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017677{
17678 rettv->vval.v_number = screen_screencol() + 1;
17679}
17680
17681/*
17682 * "screenrow()" function
17683 */
17684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017685f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017686{
17687 rettv->vval.v_number = screen_screenrow() + 1;
17688}
17689
17690/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017691 * "search()" function
17692 */
17693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017694f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017695{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017696 int flags = 0;
17697
17698 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699}
17700
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017702 * "searchdecl()" function
17703 */
17704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017705f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017706{
17707 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017708 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017709 int error = FALSE;
17710 char_u *name;
17711
17712 rettv->vval.v_number = 1; /* default: FAIL */
17713
17714 name = get_tv_string_chk(&argvars[0]);
17715 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017716 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017717 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017718 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17719 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17720 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017721 if (!error && name != NULL)
17722 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017723 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017724}
17725
17726/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017727 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017730searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731{
17732 char_u *spat, *mpat, *epat;
17733 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 int dir;
17736 int flags = 0;
17737 char_u nbuf1[NUMBUFLEN];
17738 char_u nbuf2[NUMBUFLEN];
17739 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017740 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017741 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017742 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017745 spat = get_tv_string_chk(&argvars[0]);
17746 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17747 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17748 if (spat == NULL || mpat == NULL || epat == NULL)
17749 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017750
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 /* Handle the optional fourth argument: flags */
17752 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017753 if (dir == 0)
17754 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017755
17756 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017757 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17758 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017759 if ((flags & (SP_END | SP_SUBPAT)) != 0
17760 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017761 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017762 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017763 goto theend;
17764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017766 /* Using 'r' implies 'W', otherwise it doesn't work. */
17767 if (flags & SP_REPEAT)
17768 p_ws = FALSE;
17769
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017770 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017771 if (argvars[3].v_type == VAR_UNKNOWN
17772 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 skip = (char_u *)"";
17774 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017776 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017777 if (argvars[5].v_type != VAR_UNKNOWN)
17778 {
17779 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17780 if (lnum_stop < 0)
17781 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017782#ifdef FEAT_RELTIME
17783 if (argvars[6].v_type != VAR_UNKNOWN)
17784 {
17785 time_limit = get_tv_number_chk(&argvars[6], NULL);
17786 if (time_limit < 0)
17787 goto theend;
17788 }
17789#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017790 }
17791 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017792 if (skip == NULL)
17793 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017794
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017795 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017796 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017797
17798theend:
17799 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017800
17801 return retval;
17802}
17803
17804/*
17805 * "searchpair()" function
17806 */
17807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017808f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809{
17810 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17811}
17812
17813/*
17814 * "searchpairpos()" function
17815 */
17816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017817f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017819 pos_T match_pos;
17820 int lnum = 0;
17821 int col = 0;
17822
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017823 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017824 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017825
17826 if (searchpair_cmn(argvars, &match_pos) > 0)
17827 {
17828 lnum = match_pos.lnum;
17829 col = match_pos.col;
17830 }
17831
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017832 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17833 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017834}
17835
17836/*
17837 * Search for a start/middle/end thing.
17838 * Used by searchpair(), see its documentation for the details.
17839 * Returns 0 or -1 for no match,
17840 */
17841 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017842do_searchpair(
17843 char_u *spat, /* start pattern */
17844 char_u *mpat, /* middle pattern */
17845 char_u *epat, /* end pattern */
17846 int dir, /* BACKWARD or FORWARD */
17847 char_u *skip, /* skip expression */
17848 int flags, /* SP_SETPCMARK and other SP_ values */
17849 pos_T *match_pos,
17850 linenr_T lnum_stop, /* stop at this line if not zero */
17851 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017852{
17853 char_u *save_cpo;
17854 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17855 long retval = 0;
17856 pos_T pos;
17857 pos_T firstpos;
17858 pos_T foundpos;
17859 pos_T save_cursor;
17860 pos_T save_pos;
17861 int n;
17862 int r;
17863 int nest = 1;
17864 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017865 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017866 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017867
17868 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17869 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017870 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017871
Bram Moolenaar76929292008-01-06 19:07:36 +000017872#ifdef FEAT_RELTIME
17873 /* Set the time limit, if there is one. */
17874 profile_setlimit(time_limit, &tm);
17875#endif
17876
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017877 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17878 * start/middle/end (pat3, for the top pair). */
17879 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17880 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17881 if (pat2 == NULL || pat3 == NULL)
17882 goto theend;
17883 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17884 if (*mpat == NUL)
17885 STRCPY(pat3, pat2);
17886 else
17887 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17888 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017889 if (flags & SP_START)
17890 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017891
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 save_cursor = curwin->w_cursor;
17893 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017894 clearpos(&firstpos);
17895 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896 pat = pat3;
17897 for (;;)
17898 {
17899 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017900 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17902 /* didn't find it or found the first match again: FAIL */
17903 break;
17904
17905 if (firstpos.lnum == 0)
17906 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017907 if (equalpos(pos, foundpos))
17908 {
17909 /* Found the same position again. Can happen with a pattern that
17910 * has "\zs" at the end and searching backwards. Advance one
17911 * character and try again. */
17912 if (dir == BACKWARD)
17913 decl(&pos);
17914 else
17915 incl(&pos);
17916 }
17917 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017919 /* clear the start flag to avoid getting stuck here */
17920 options &= ~SEARCH_START;
17921
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922 /* If the skip pattern matches, ignore this match. */
17923 if (*skip != NUL)
17924 {
17925 save_pos = curwin->w_cursor;
17926 curwin->w_cursor = pos;
17927 r = eval_to_bool(skip, &err, NULL, FALSE);
17928 curwin->w_cursor = save_pos;
17929 if (err)
17930 {
17931 /* Evaluating {skip} caused an error, break here. */
17932 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017933 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017934 break;
17935 }
17936 if (r)
17937 continue;
17938 }
17939
17940 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17941 {
17942 /* Found end when searching backwards or start when searching
17943 * forward: nested pair. */
17944 ++nest;
17945 pat = pat2; /* nested, don't search for middle */
17946 }
17947 else
17948 {
17949 /* Found end when searching forward or start when searching
17950 * backward: end of (nested) pair; or found middle in outer pair. */
17951 if (--nest == 1)
17952 pat = pat3; /* outer level, search for middle */
17953 }
17954
17955 if (nest == 0)
17956 {
17957 /* Found the match: return matchcount or line number. */
17958 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017959 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017960 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017961 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017962 if (flags & SP_SETPCMARK)
17963 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 curwin->w_cursor = pos;
17965 if (!(flags & SP_REPEAT))
17966 break;
17967 nest = 1; /* search for next unmatched */
17968 }
17969 }
17970
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017971 if (match_pos != NULL)
17972 {
17973 /* Store the match cursor position */
17974 match_pos->lnum = curwin->w_cursor.lnum;
17975 match_pos->col = curwin->w_cursor.col + 1;
17976 }
17977
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017979 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 curwin->w_cursor = save_cursor;
17981
17982theend:
17983 vim_free(pat2);
17984 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017985 if (p_cpo == empty_option)
17986 p_cpo = save_cpo;
17987 else
17988 /* Darn, evaluating the {skip} expression changed the value. */
17989 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017990
17991 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992}
17993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994/*
17995 * "searchpos()" function
17996 */
17997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017998f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017999{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018000 pos_T match_pos;
18001 int lnum = 0;
18002 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018003 int n;
18004 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018005
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018006 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018007 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018008
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018009 n = search_cmn(argvars, &match_pos, &flags);
18010 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018011 {
18012 lnum = match_pos.lnum;
18013 col = match_pos.col;
18014 }
18015
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018016 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18017 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018018 if (flags & SP_SUBPAT)
18019 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018020}
18021
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018023f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018025#ifdef FEAT_CLIENTSERVER
18026 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018027 char_u *server = get_tv_string_chk(&argvars[0]);
18028 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029
Bram Moolenaar0d660222005-01-07 21:51:51 +000018030 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018031 if (server == NULL || reply == NULL)
18032 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018033 if (check_restricted() || check_secure())
18034 return;
18035# ifdef FEAT_X11
18036 if (check_connection() == FAIL)
18037 return;
18038# endif
18039
18040 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 EMSG(_("E258: Unable to send to client"));
18043 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018045 rettv->vval.v_number = 0;
18046#else
18047 rettv->vval.v_number = -1;
18048#endif
18049}
18050
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018052f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018053{
18054 char_u *r = NULL;
18055
18056#ifdef FEAT_CLIENTSERVER
18057# ifdef WIN32
18058 r = serverGetVimNames();
18059# else
18060 make_connection();
18061 if (X_DISPLAY != NULL)
18062 r = serverGetVimNames(X_DISPLAY);
18063# endif
18064#endif
18065 rettv->v_type = VAR_STRING;
18066 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067}
18068
18069/*
18070 * "setbufvar()" function
18071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018073f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074{
18075 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018078 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 char_u nbuf[NUMBUFLEN];
18080
18081 if (check_restricted() || check_secure())
18082 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18084 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018085 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 varp = &argvars[2];
18087
18088 if (buf != NULL && varname != NULL && varp != NULL)
18089 {
18090 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092
18093 if (*varname == '&')
18094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018095 long numval;
18096 char_u *strval;
18097 int error = FALSE;
18098
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018100 numval = get_tv_number_chk(varp, &error);
18101 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 if (!error && strval != NULL)
18103 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 }
18105 else
18106 {
18107 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18108 if (bufvarname != NULL)
18109 {
18110 STRCPY(bufvarname, "b:");
18111 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018112 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 vim_free(bufvarname);
18114 }
18115 }
18116
18117 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018118 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120}
18121
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018123f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018124{
18125 dict_T *d;
18126 dictitem_T *di;
18127 char_u *csearch;
18128
18129 if (argvars[0].v_type != VAR_DICT)
18130 {
18131 EMSG(_(e_dictreq));
18132 return;
18133 }
18134
18135 if ((d = argvars[0].vval.v_dict) != NULL)
18136 {
18137 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18138 if (csearch != NULL)
18139 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018140#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018141 if (enc_utf8)
18142 {
18143 int pcc[MAX_MCO];
18144 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018145
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018146 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18147 }
18148 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018149#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018150 set_last_csearch(PTR2CHAR(csearch),
18151 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018152 }
18153
18154 di = dict_find(d, (char_u *)"forward", -1);
18155 if (di != NULL)
18156 set_csearch_direction(get_tv_number(&di->di_tv)
18157 ? FORWARD : BACKWARD);
18158
18159 di = dict_find(d, (char_u *)"until", -1);
18160 if (di != NULL)
18161 set_csearch_until(!!get_tv_number(&di->di_tv));
18162 }
18163}
18164
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165/*
18166 * "setcmdpos()" function
18167 */
18168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018169f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018170{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018171 int pos = (int)get_tv_number(&argvars[0]) - 1;
18172
18173 if (pos >= 0)
18174 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018175}
18176
18177/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018178 * "setfperm({fname}, {mode})" function
18179 */
18180 static void
18181f_setfperm(typval_T *argvars, typval_T *rettv)
18182{
18183 char_u *fname;
18184 char_u modebuf[NUMBUFLEN];
18185 char_u *mode_str;
18186 int i;
18187 int mask;
18188 int mode = 0;
18189
18190 rettv->vval.v_number = 0;
18191 fname = get_tv_string_chk(&argvars[0]);
18192 if (fname == NULL)
18193 return;
18194 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18195 if (mode_str == NULL)
18196 return;
18197 if (STRLEN(mode_str) != 9)
18198 {
18199 EMSG2(_(e_invarg2), mode_str);
18200 return;
18201 }
18202
18203 mask = 1;
18204 for (i = 8; i >= 0; --i)
18205 {
18206 if (mode_str[i] != '-')
18207 mode |= mask;
18208 mask = mask << 1;
18209 }
18210 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18211}
18212
18213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 * "setline()" function
18215 */
18216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018217f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218{
18219 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018220 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018221 list_T *l = NULL;
18222 listitem_T *li = NULL;
18223 long added = 0;
18224 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018226 lnum = get_tv_lnum(&argvars[0]);
18227 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018229 l = argvars[1].vval.v_list;
18230 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018232 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018233 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018234
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018235 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018236 for (;;)
18237 {
18238 if (l != NULL)
18239 {
18240 /* list argument, get next string */
18241 if (li == NULL)
18242 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018243 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018244 li = li->li_next;
18245 }
18246
18247 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018248 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018249 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018250
18251 /* When coming here from Insert mode, sync undo, so that this can be
18252 * undone separately from what was previously inserted. */
18253 if (u_sync_once == 2)
18254 {
18255 u_sync_once = 1; /* notify that u_sync() was called */
18256 u_sync(TRUE);
18257 }
18258
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018259 if (lnum <= curbuf->b_ml.ml_line_count)
18260 {
18261 /* existing line, replace it */
18262 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18263 {
18264 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018265 if (lnum == curwin->w_cursor.lnum)
18266 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018267 rettv->vval.v_number = 0; /* OK */
18268 }
18269 }
18270 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18271 {
18272 /* lnum is one past the last line, append the line */
18273 ++added;
18274 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18275 rettv->vval.v_number = 0; /* OK */
18276 }
18277
18278 if (l == NULL) /* only one string argument */
18279 break;
18280 ++lnum;
18281 }
18282
18283 if (added > 0)
18284 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285}
18286
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018287static 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 +000018288
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018290 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018291 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018292 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018293set_qf_ll_list(
18294 win_T *wp UNUSED,
18295 typval_T *list_arg UNUSED,
18296 typval_T *action_arg UNUSED,
18297 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018298{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018299#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018300 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018301 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018302 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018303#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018304
Bram Moolenaar2641f772005-03-25 21:58:17 +000018305 rettv->vval.v_number = -1;
18306
18307#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018308 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018309 EMSG(_(e_listreq));
18310 else
18311 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018312 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018313
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018314 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018315 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018316 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018317 if (act == NULL)
18318 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018319 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018320 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018321 else
18322 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018323 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018324 else if (action_arg->v_type == VAR_UNKNOWN)
18325 action = ' ';
18326 else
18327 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018328
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018329 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018330 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018331 rettv->vval.v_number = 0;
18332 }
18333#endif
18334}
18335
18336/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018337 * "setloclist()" function
18338 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018340f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018341{
18342 win_T *win;
18343
18344 rettv->vval.v_number = -1;
18345
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018346 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018347 if (win != NULL)
18348 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18349}
18350
18351/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018352 * "setmatches()" function
18353 */
18354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018355f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018356{
18357#ifdef FEAT_SEARCH_EXTRA
18358 list_T *l;
18359 listitem_T *li;
18360 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018361 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018362
18363 rettv->vval.v_number = -1;
18364 if (argvars[0].v_type != VAR_LIST)
18365 {
18366 EMSG(_(e_listreq));
18367 return;
18368 }
18369 if ((l = argvars[0].vval.v_list) != NULL)
18370 {
18371
18372 /* To some extent make sure that we are dealing with a list from
18373 * "getmatches()". */
18374 li = l->lv_first;
18375 while (li != NULL)
18376 {
18377 if (li->li_tv.v_type != VAR_DICT
18378 || (d = li->li_tv.vval.v_dict) == NULL)
18379 {
18380 EMSG(_(e_invarg));
18381 return;
18382 }
18383 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018384 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18385 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018386 && dict_find(d, (char_u *)"priority", -1) != NULL
18387 && dict_find(d, (char_u *)"id", -1) != NULL))
18388 {
18389 EMSG(_(e_invarg));
18390 return;
18391 }
18392 li = li->li_next;
18393 }
18394
18395 clear_matches(curwin);
18396 li = l->lv_first;
18397 while (li != NULL)
18398 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018399 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018400 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018401 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018402 char_u *group;
18403 int priority;
18404 int id;
18405 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018406
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018407 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018408 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18409 {
18410 if (s == NULL)
18411 {
18412 s = list_alloc();
18413 if (s == NULL)
18414 return;
18415 }
18416
18417 /* match from matchaddpos() */
18418 for (i = 1; i < 9; i++)
18419 {
18420 sprintf((char *)buf, (char *)"pos%d", i);
18421 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18422 {
18423 if (di->di_tv.v_type != VAR_LIST)
18424 return;
18425
18426 list_append_tv(s, &di->di_tv);
18427 s->lv_refcount++;
18428 }
18429 else
18430 break;
18431 }
18432 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018433
18434 group = get_dict_string(d, (char_u *)"group", FALSE);
18435 priority = (int)get_dict_number(d, (char_u *)"priority");
18436 id = (int)get_dict_number(d, (char_u *)"id");
18437 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18438 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18439 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018440 if (i == 0)
18441 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018442 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018443 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018444 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018445 }
18446 else
18447 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018448 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018449 list_unref(s);
18450 s = NULL;
18451 }
18452
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018453 li = li->li_next;
18454 }
18455 rettv->vval.v_number = 0;
18456 }
18457#endif
18458}
18459
18460/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018461 * "setpos()" function
18462 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018464f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018465{
18466 pos_T pos;
18467 int fnum;
18468 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018469 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018470
Bram Moolenaar08250432008-02-13 11:42:46 +000018471 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018472 name = get_tv_string_chk(argvars);
18473 if (name != NULL)
18474 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018475 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018476 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018477 if (--pos.col < 0)
18478 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018479 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018480 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018481 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018482 if (fnum == curbuf->b_fnum)
18483 {
18484 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018485 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018486 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018487 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018488 curwin->w_set_curswant = FALSE;
18489 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018490 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018491 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018492 }
18493 else
18494 EMSG(_(e_invarg));
18495 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018496 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18497 {
18498 /* set mark */
18499 if (setmark_pos(name[1], &pos, fnum) == OK)
18500 rettv->vval.v_number = 0;
18501 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018502 else
18503 EMSG(_(e_invarg));
18504 }
18505 }
18506}
18507
18508/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018509 * "setqflist()" function
18510 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018512f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018513{
18514 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18515}
18516
18517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018518 * "setreg()" function
18519 */
18520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018521f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018522{
18523 int regname;
18524 char_u *strregname;
18525 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018526 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 int append;
18528 char_u yank_type;
18529 long block_len;
18530
18531 block_len = -1;
18532 yank_type = MAUTO;
18533 append = FALSE;
18534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018535 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018536 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018538 if (strregname == NULL)
18539 return; /* type error; errmsg already given */
18540 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 if (regname == 0 || regname == '@')
18542 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018543
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018544 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018546 stropt = get_tv_string_chk(&argvars[2]);
18547 if (stropt == NULL)
18548 return; /* type error */
18549 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 switch (*stropt)
18551 {
18552 case 'a': case 'A': /* append */
18553 append = TRUE;
18554 break;
18555 case 'v': case 'c': /* character-wise selection */
18556 yank_type = MCHAR;
18557 break;
18558 case 'V': case 'l': /* line-wise selection */
18559 yank_type = MLINE;
18560 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 case 'b': case Ctrl_V: /* block-wise selection */
18562 yank_type = MBLOCK;
18563 if (VIM_ISDIGIT(stropt[1]))
18564 {
18565 ++stropt;
18566 block_len = getdigits(&stropt) - 1;
18567 --stropt;
18568 }
18569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 }
18571 }
18572
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018573 if (argvars[1].v_type == VAR_LIST)
18574 {
18575 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018576 char_u **allocval;
18577 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018578 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018579 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018580 int len = argvars[1].vval.v_list->lv_len;
18581 listitem_T *li;
18582
Bram Moolenaar7d647822014-04-05 21:28:56 +020018583 /* First half: use for pointers to result lines; second half: use for
18584 * pointers to allocated copies. */
18585 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018586 if (lstval == NULL)
18587 return;
18588 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018589 allocval = lstval + len + 2;
18590 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018591
18592 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18593 li = li->li_next)
18594 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018595 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018596 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018597 goto free_lstval;
18598 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018599 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018600 /* Need to make a copy, next get_tv_string_buf_chk() will
18601 * overwrite the string. */
18602 strval = vim_strsave(buf);
18603 if (strval == NULL)
18604 goto free_lstval;
18605 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018606 }
18607 *curval++ = strval;
18608 }
18609 *curval++ = NULL;
18610
18611 write_reg_contents_lst(regname, lstval, -1,
18612 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018613free_lstval:
18614 while (curallocval > allocval)
18615 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018616 vim_free(lstval);
18617 }
18618 else
18619 {
18620 strval = get_tv_string_chk(&argvars[1]);
18621 if (strval == NULL)
18622 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018625 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627}
18628
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018629/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018630 * "settabvar()" function
18631 */
18632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018633f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018634{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018635#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018636 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018637 tabpage_T *tp;
18638#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018639 char_u *varname, *tabvarname;
18640 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018641
18642 rettv->vval.v_number = 0;
18643
18644 if (check_restricted() || check_secure())
18645 return;
18646
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018647#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018648 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018649#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018650 varname = get_tv_string_chk(&argvars[1]);
18651 varp = &argvars[2];
18652
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018653 if (varname != NULL && varp != NULL
18654#ifdef FEAT_WINDOWS
18655 && tp != NULL
18656#endif
18657 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018658 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018659#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018660 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018661 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018662#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018663
18664 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18665 if (tabvarname != NULL)
18666 {
18667 STRCPY(tabvarname, "t:");
18668 STRCPY(tabvarname + 2, varname);
18669 set_var(tabvarname, varp, TRUE);
18670 vim_free(tabvarname);
18671 }
18672
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018673#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018674 /* Restore current tabpage */
18675 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018676 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018677#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018678 }
18679}
18680
18681/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018682 * "settabwinvar()" function
18683 */
18684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018685f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018686{
18687 setwinvar(argvars, rettv, 1);
18688}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689
18690/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018691 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018694f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018696 setwinvar(argvars, rettv, 0);
18697}
18698
18699/*
18700 * "setwinvar()" and "settabwinvar()" functions
18701 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018702
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018704setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018705{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018706 win_T *win;
18707#ifdef FEAT_WINDOWS
18708 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018709 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018710 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018711#endif
18712 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018713 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018715 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
18717 if (check_restricted() || check_secure())
18718 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018719
18720#ifdef FEAT_WINDOWS
18721 if (off == 1)
18722 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18723 else
18724 tp = curtab;
18725#endif
18726 win = find_win_by_nr(&argvars[off], tp);
18727 varname = get_tv_string_chk(&argvars[off + 1]);
18728 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018729
18730 if (win != NULL && varname != NULL && varp != NULL)
18731 {
18732#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018733 need_switch_win = !(tp == curtab && win == curwin);
18734 if (!need_switch_win
18735 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018736#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018738 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018740 long numval;
18741 char_u *strval;
18742 int error = FALSE;
18743
18744 ++varname;
18745 numval = get_tv_number_chk(varp, &error);
18746 strval = get_tv_string_buf_chk(varp, nbuf);
18747 if (!error && strval != NULL)
18748 set_option_value(varname, numval, strval, OPT_LOCAL);
18749 }
18750 else
18751 {
18752 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18753 if (winvarname != NULL)
18754 {
18755 STRCPY(winvarname, "w:");
18756 STRCPY(winvarname + 2, varname);
18757 set_var(winvarname, varp, TRUE);
18758 vim_free(winvarname);
18759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 }
18761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018763 if (need_switch_win)
18764 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765#endif
18766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767}
18768
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018769#ifdef FEAT_CRYPT
18770/*
18771 * "sha256({string})" function
18772 */
18773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018774f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018775{
18776 char_u *p;
18777
18778 p = get_tv_string(&argvars[0]);
18779 rettv->vval.v_string = vim_strsave(
18780 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18781 rettv->v_type = VAR_STRING;
18782}
18783#endif /* FEAT_CRYPT */
18784
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018786 * "shellescape({string})" function
18787 */
18788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018789f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018790{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018791 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018792 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018793 rettv->v_type = VAR_STRING;
18794}
18795
18796/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018797 * shiftwidth() function
18798 */
18799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018800f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018801{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018802 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018803}
18804
18805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018806 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 */
18808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018809f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018810{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018811 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812
Bram Moolenaar0d660222005-01-07 21:51:51 +000018813 p = get_tv_string(&argvars[0]);
18814 rettv->vval.v_string = vim_strsave(p);
18815 simplify_filename(rettv->vval.v_string); /* simplify in place */
18816 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817}
18818
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018819#ifdef FEAT_FLOAT
18820/*
18821 * "sin()" function
18822 */
18823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018824f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018825{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018826 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018827
18828 rettv->v_type = VAR_FLOAT;
18829 if (get_float_arg(argvars, &f) == OK)
18830 rettv->vval.v_float = sin(f);
18831 else
18832 rettv->vval.v_float = 0.0;
18833}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018834
18835/*
18836 * "sinh()" function
18837 */
18838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018839f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018840{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018841 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018842
18843 rettv->v_type = VAR_FLOAT;
18844 if (get_float_arg(argvars, &f) == OK)
18845 rettv->vval.v_float = sinh(f);
18846 else
18847 rettv->vval.v_float = 0.0;
18848}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018849#endif
18850
Bram Moolenaar0d660222005-01-07 21:51:51 +000018851static int
18852#ifdef __BORLANDC__
18853 _RTLENTRYF
18854#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018855 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018856static int
18857#ifdef __BORLANDC__
18858 _RTLENTRYF
18859#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018860 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018861
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018862/* struct used in the array that's given to qsort() */
18863typedef struct
18864{
18865 listitem_T *item;
18866 int idx;
18867} sortItem_T;
18868
Bram Moolenaar0b962472016-02-22 22:51:33 +010018869/* struct storing information about current sort */
18870typedef struct
18871{
18872 int item_compare_ic;
18873 int item_compare_numeric;
18874 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018875#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018876 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018877#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018878 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018879 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018880 dict_T *item_compare_selfdict;
18881 int item_compare_func_err;
18882 int item_compare_keep_zero;
18883} sortinfo_T;
18884static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018885static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018886#define ITEM_COMPARE_FAIL 999
18887
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018889 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018890 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018891 static int
18892#ifdef __BORLANDC__
18893_RTLENTRYF
18894#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018895item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018896{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018897 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018898 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018899 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018900 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018901 int res;
18902 char_u numbuf1[NUMBUFLEN];
18903 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018905 si1 = (sortItem_T *)s1;
18906 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018907 tv1 = &si1->item->li_tv;
18908 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018909
Bram Moolenaar0b962472016-02-22 22:51:33 +010018910 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018911 {
18912 long v1 = get_tv_number(tv1);
18913 long v2 = get_tv_number(tv2);
18914
18915 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18916 }
18917
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018918#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018919 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018920 {
18921 float_T v1 = get_tv_float(tv1);
18922 float_T v2 = get_tv_float(tv2);
18923
18924 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18925 }
18926#endif
18927
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018928 /* tv2string() puts quotes around a string and allocates memory. Don't do
18929 * that for string variables. Use a single quote when comparing with a
18930 * non-string to do what the docs promise. */
18931 if (tv1->v_type == VAR_STRING)
18932 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018933 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018934 p1 = (char_u *)"'";
18935 else
18936 p1 = tv1->vval.v_string;
18937 }
18938 else
18939 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18940 if (tv2->v_type == VAR_STRING)
18941 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018942 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018943 p2 = (char_u *)"'";
18944 else
18945 p2 = tv2->vval.v_string;
18946 }
18947 else
18948 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018949 if (p1 == NULL)
18950 p1 = (char_u *)"";
18951 if (p2 == NULL)
18952 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018953 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018954 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018955 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018956 res = STRICMP(p1, p2);
18957 else
18958 res = STRCMP(p1, p2);
18959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018961 {
18962 double n1, n2;
18963 n1 = strtod((char *)p1, (char **)&p1);
18964 n2 = strtod((char *)p2, (char **)&p2);
18965 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18966 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018967
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018968 /* When the result would be zero, compare the item indexes. Makes the
18969 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018970 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018971 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018972
Bram Moolenaar0d660222005-01-07 21:51:51 +000018973 vim_free(tofree1);
18974 vim_free(tofree2);
18975 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018976}
18977
18978 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018979#ifdef __BORLANDC__
18980_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018981#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018982item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018984 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018986 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018987 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018989 char_u *func_name;
18990 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018992 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018993 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018994 return 0;
18995
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018996 si1 = (sortItem_T *)s1;
18997 si2 = (sortItem_T *)s2;
18998
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018999 if (partial == NULL)
19000 func_name = sortinfo->item_compare_func;
19001 else
19002 func_name = partial->pt_name;
19003
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019004 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019005 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019006 copy_tv(&si1->item->li_tv, &argv[0]);
19007 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008
19009 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019010 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019011 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019012 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 clear_tv(&argv[0]);
19014 clear_tv(&argv[1]);
19015
19016 if (res == FAIL)
19017 res = ITEM_COMPARE_FAIL;
19018 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019019 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19020 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019021 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019022 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019023
19024 /* When the result would be zero, compare the pointers themselves. Makes
19025 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019026 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019027 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019028
Bram Moolenaar0d660222005-01-07 21:51:51 +000019029 return res;
19030}
19031
19032/*
19033 * "sort({list})" function
19034 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019036do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019037{
Bram Moolenaar33570922005-01-25 22:26:29 +000019038 list_T *l;
19039 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019040 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041 sortinfo_T *old_sortinfo;
19042 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043 long len;
19044 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045
Bram Moolenaar0b962472016-02-22 22:51:33 +010019046 /* Pointer to current info struct used in compare function. Save and
19047 * restore the current one for nested calls. */
19048 old_sortinfo = sortinfo;
19049 sortinfo = &info;
19050
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019052 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019053 else
19054 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019055 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019056 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019057 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19058 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019059 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 rettv->vval.v_list = l;
19061 rettv->v_type = VAR_LIST;
19062 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019063
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 len = list_len(l);
19065 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 info.item_compare_ic = FALSE;
19069 info.item_compare_numeric = FALSE;
19070 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019071#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019072 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019073#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019074 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019075 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019076 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 if (argvars[1].v_type != VAR_UNKNOWN)
19078 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019079 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019081 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019082 else if (argvars[1].v_type == VAR_PARTIAL)
19083 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019084 else
19085 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019086 int error = FALSE;
19087
19088 i = get_tv_number_chk(&argvars[1], &error);
19089 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019090 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019091 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019092 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019093 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019095 else if (i != 0)
19096 {
19097 EMSG(_(e_invarg));
19098 goto theend;
19099 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019100 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019101 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019102 if (*info.item_compare_func == NUL)
19103 {
19104 /* empty string means default sort */
19105 info.item_compare_func = NULL;
19106 }
19107 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019108 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 info.item_compare_func = NULL;
19110 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019111 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019112 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019113 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 info.item_compare_func = NULL;
19115 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019116 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019117#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019119 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019120 info.item_compare_func = NULL;
19121 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019122 }
19123#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019125 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 info.item_compare_func = NULL;
19127 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019128 }
19129 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019130 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019131
19132 if (argvars[2].v_type != VAR_UNKNOWN)
19133 {
19134 /* optional third argument: {dict} */
19135 if (argvars[2].v_type != VAR_DICT)
19136 {
19137 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019139 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019140 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019143
Bram Moolenaar0d660222005-01-07 21:51:51 +000019144 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019145 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019146 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019147 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148
Bram Moolenaar327aa022014-03-25 18:24:23 +010019149 i = 0;
19150 if (sort)
19151 {
19152 /* sort(): ptrs will be the list to sort */
19153 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019154 {
19155 ptrs[i].item = li;
19156 ptrs[i].idx = i;
19157 ++i;
19158 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019159
Bram Moolenaar0b962472016-02-22 22:51:33 +010019160 info.item_compare_func_err = FALSE;
19161 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019162 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019163 if ((info.item_compare_func != NULL
19164 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019165 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019166 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019167 EMSG(_("E702: Sort compare function failed"));
19168 else
19169 {
19170 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019173 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019175
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019177 {
19178 /* Clear the List and append the items in sorted order. */
19179 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19180 l->lv_len = 0;
19181 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019182 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019183 }
19184 }
19185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019187 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019188 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019189
19190 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 info.item_compare_func_err = FALSE;
19192 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019193 item_compare_func_ptr = info.item_compare_func != NULL
19194 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019195 ? item_compare2 : item_compare;
19196
19197 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19198 li = li->li_next)
19199 {
19200 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19201 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019202 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019203 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019204 {
19205 EMSG(_("E882: Uniq compare function failed"));
19206 break;
19207 }
19208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019209
Bram Moolenaar0b962472016-02-22 22:51:33 +010019210 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019211 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019212 while (--i >= 0)
19213 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019214 li = ptrs[i].item->li_next;
19215 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019216 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019217 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019218 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019219 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019220 list_fix_watch(l, li);
19221 listitem_free(li);
19222 l->lv_len--;
19223 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019224 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019225 }
19226
19227 vim_free(ptrs);
19228 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019229theend:
19230 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019231}
19232
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019233/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019234 * "sort({list})" function
19235 */
19236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019237f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019238{
19239 do_sort_uniq(argvars, rettv, TRUE);
19240}
19241
19242/*
19243 * "uniq({list})" function
19244 */
19245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019246f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019247{
19248 do_sort_uniq(argvars, rettv, FALSE);
19249}
19250
19251/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019252 * "soundfold({word})" function
19253 */
19254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019255f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019256{
19257 char_u *s;
19258
19259 rettv->v_type = VAR_STRING;
19260 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019261#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019262 rettv->vval.v_string = eval_soundfold(s);
19263#else
19264 rettv->vval.v_string = vim_strsave(s);
19265#endif
19266}
19267
19268/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019269 * "spellbadword()" function
19270 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019272f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019273{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019274 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019275 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019276 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019277
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019278 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019279 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019280
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019281#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019282 if (argvars[0].v_type == VAR_UNKNOWN)
19283 {
19284 /* Find the start and length of the badly spelled word. */
19285 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19286 if (len != 0)
19287 word = ml_get_cursor();
19288 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019289 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019290 {
19291 char_u *str = get_tv_string_chk(&argvars[0]);
19292 int capcol = -1;
19293
19294 if (str != NULL)
19295 {
19296 /* Check the argument for spelling. */
19297 while (*str != NUL)
19298 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019299 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019300 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019301 {
19302 word = str;
19303 break;
19304 }
19305 str += len;
19306 }
19307 }
19308 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019309#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019311 list_append_string(rettv->vval.v_list, word, len);
19312 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019313 attr == HLF_SPB ? "bad" :
19314 attr == HLF_SPR ? "rare" :
19315 attr == HLF_SPL ? "local" :
19316 attr == HLF_SPC ? "caps" :
19317 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019318}
19319
19320/*
19321 * "spellsuggest()" function
19322 */
19323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019324f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019325{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019326#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019328 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019329 int maxcount;
19330 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019331 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019332 listitem_T *li;
19333 int need_capital = FALSE;
19334#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019336 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019337 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019338
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019339#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019340 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019341 {
19342 str = get_tv_string(&argvars[0]);
19343 if (argvars[1].v_type != VAR_UNKNOWN)
19344 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019345 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019346 if (maxcount <= 0)
19347 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019348 if (argvars[2].v_type != VAR_UNKNOWN)
19349 {
19350 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19351 if (typeerr)
19352 return;
19353 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019354 }
19355 else
19356 maxcount = 25;
19357
Bram Moolenaar4770d092006-01-12 23:22:24 +000019358 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019359
19360 for (i = 0; i < ga.ga_len; ++i)
19361 {
19362 str = ((char_u **)ga.ga_data)[i];
19363
19364 li = listitem_alloc();
19365 if (li == NULL)
19366 vim_free(str);
19367 else
19368 {
19369 li->li_tv.v_type = VAR_STRING;
19370 li->li_tv.v_lock = 0;
19371 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019372 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019373 }
19374 }
19375 ga_clear(&ga);
19376 }
19377#endif
19378}
19379
Bram Moolenaar0d660222005-01-07 21:51:51 +000019380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019381f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019382{
19383 char_u *str;
19384 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019385 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 regmatch_T regmatch;
19387 char_u patbuf[NUMBUFLEN];
19388 char_u *save_cpo;
19389 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019391 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019392 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019393
19394 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19395 save_cpo = p_cpo;
19396 p_cpo = (char_u *)"";
19397
19398 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019399 if (argvars[1].v_type != VAR_UNKNOWN)
19400 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019401 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19402 if (pat == NULL)
19403 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019404 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019405 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019406 }
19407 if (pat == NULL || *pat == NUL)
19408 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019410 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019411 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019412 if (typeerr)
19413 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019414
Bram Moolenaar0d660222005-01-07 21:51:51 +000019415 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19416 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019419 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019421 if (*str == NUL)
19422 match = FALSE; /* empty item at the end */
19423 else
19424 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019425 if (match)
19426 end = regmatch.startp[0];
19427 else
19428 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019429 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19430 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019432 if (list_append_string(rettv->vval.v_list, str,
19433 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019435 }
19436 if (!match)
19437 break;
19438 /* Advance to just after the match. */
19439 if (regmatch.endp[0] > str)
19440 col = 0;
19441 else
19442 {
19443 /* Don't get stuck at the same match. */
19444#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019445 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019446#else
19447 col = 1;
19448#endif
19449 }
19450 str = regmatch.endp[0];
19451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019452
Bram Moolenaar473de612013-06-08 18:19:48 +020019453 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455
Bram Moolenaar0d660222005-01-07 21:51:51 +000019456 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457}
19458
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019459#ifdef FEAT_FLOAT
19460/*
19461 * "sqrt()" function
19462 */
19463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019464f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019465{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019466 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019467
19468 rettv->v_type = VAR_FLOAT;
19469 if (get_float_arg(argvars, &f) == OK)
19470 rettv->vval.v_float = sqrt(f);
19471 else
19472 rettv->vval.v_float = 0.0;
19473}
19474
19475/*
19476 * "str2float()" function
19477 */
19478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019479f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019480{
19481 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19482
19483 if (*p == '+')
19484 p = skipwhite(p + 1);
19485 (void)string2float(p, &rettv->vval.v_float);
19486 rettv->v_type = VAR_FLOAT;
19487}
19488#endif
19489
Bram Moolenaar2c932302006-03-18 21:42:09 +000019490/*
19491 * "str2nr()" function
19492 */
19493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019494f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019495{
19496 int base = 10;
19497 char_u *p;
19498 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019499 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019500
19501 if (argvars[1].v_type != VAR_UNKNOWN)
19502 {
19503 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019504 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019505 {
19506 EMSG(_(e_invarg));
19507 return;
19508 }
19509 }
19510
19511 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019512 if (*p == '+')
19513 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019514 switch (base)
19515 {
19516 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19517 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19518 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19519 default: what = 0;
19520 }
19521 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019522 rettv->vval.v_number = n;
19523}
19524
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525#ifdef HAVE_STRFTIME
19526/*
19527 * "strftime({format}[, {time}])" function
19528 */
19529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019530f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531{
19532 char_u result_buf[256];
19533 struct tm *curtime;
19534 time_t seconds;
19535 char_u *p;
19536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019539 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019540 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019541 seconds = time(NULL);
19542 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544 curtime = localtime(&seconds);
19545 /* MSVC returns NULL for an invalid value of seconds. */
19546 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019547 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 else
19549 {
19550# ifdef FEAT_MBYTE
19551 vimconv_T conv;
19552 char_u *enc;
19553
19554 conv.vc_type = CONV_NONE;
19555 enc = enc_locale();
19556 convert_setup(&conv, p_enc, enc);
19557 if (conv.vc_type != CONV_NONE)
19558 p = string_convert(&conv, p, NULL);
19559# endif
19560 if (p != NULL)
19561 (void)strftime((char *)result_buf, sizeof(result_buf),
19562 (char *)p, curtime);
19563 else
19564 result_buf[0] = NUL;
19565
19566# ifdef FEAT_MBYTE
19567 if (conv.vc_type != CONV_NONE)
19568 vim_free(p);
19569 convert_setup(&conv, enc, p_enc);
19570 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 else
19573# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019574 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019575
19576# ifdef FEAT_MBYTE
19577 /* Release conversion descriptors */
19578 convert_setup(&conv, NULL, NULL);
19579 vim_free(enc);
19580# endif
19581 }
19582}
19583#endif
19584
19585/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019586 * "strgetchar()" function
19587 */
19588 static void
19589f_strgetchar(typval_T *argvars, typval_T *rettv)
19590{
19591 char_u *str;
19592 int len;
19593 int error = FALSE;
19594 int charidx;
19595
19596 rettv->vval.v_number = -1;
19597 str = get_tv_string_chk(&argvars[0]);
19598 if (str == NULL)
19599 return;
19600 len = (int)STRLEN(str);
19601 charidx = get_tv_number_chk(&argvars[1], &error);
19602 if (error)
19603 return;
19604#ifdef FEAT_MBYTE
19605 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019606 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019607
19608 while (charidx >= 0 && byteidx < len)
19609 {
19610 if (charidx == 0)
19611 {
19612 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19613 break;
19614 }
19615 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019616 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019617 }
19618 }
19619#else
19620 if (charidx < len)
19621 rettv->vval.v_number = str[charidx];
19622#endif
19623}
19624
19625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626 * "stridx()" function
19627 */
19628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019629f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630{
19631 char_u buf[NUMBUFLEN];
19632 char_u *needle;
19633 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019634 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019636 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019638 needle = get_tv_string_chk(&argvars[1]);
19639 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019640 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019641 if (needle == NULL || haystack == NULL)
19642 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643
Bram Moolenaar33570922005-01-25 22:26:29 +000019644 if (argvars[2].v_type != VAR_UNKNOWN)
19645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019646 int error = FALSE;
19647
19648 start_idx = get_tv_number_chk(&argvars[2], &error);
19649 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019650 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019651 if (start_idx >= 0)
19652 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 }
19654
19655 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19656 if (pos != NULL)
19657 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019658}
19659
19660/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019661 * "string()" function
19662 */
19663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019664f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019665{
19666 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019667 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019669 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019670 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19671 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019672 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019673 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019674 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675}
19676
19677/*
19678 * "strlen()" function
19679 */
19680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019681f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 rettv->vval.v_number = (varnumber_T)(STRLEN(
19684 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019685}
19686
19687/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019688 * "strchars()" function
19689 */
19690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019691f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019692{
19693 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019694 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019695#ifdef FEAT_MBYTE
19696 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019697 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019698#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019699
19700 if (argvars[1].v_type != VAR_UNKNOWN)
19701 skipcc = get_tv_number_chk(&argvars[1], NULL);
19702 if (skipcc < 0 || skipcc > 1)
19703 EMSG(_(e_invarg));
19704 else
19705 {
19706#ifdef FEAT_MBYTE
19707 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19708 while (*s != NUL)
19709 {
19710 func_mb_ptr2char_adv(&s);
19711 ++len;
19712 }
19713 rettv->vval.v_number = len;
19714#else
19715 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19716#endif
19717 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019718}
19719
19720/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019721 * "strdisplaywidth()" function
19722 */
19723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019724f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019725{
19726 char_u *s = get_tv_string(&argvars[0]);
19727 int col = 0;
19728
19729 if (argvars[1].v_type != VAR_UNKNOWN)
19730 col = get_tv_number(&argvars[1]);
19731
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019732 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019733}
19734
19735/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019736 * "strwidth()" function
19737 */
19738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019739f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019740{
19741 char_u *s = get_tv_string(&argvars[0]);
19742
19743 rettv->vval.v_number = (varnumber_T)(
19744#ifdef FEAT_MBYTE
19745 mb_string2cells(s, -1)
19746#else
19747 STRLEN(s)
19748#endif
19749 );
19750}
19751
19752/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019753 * "strcharpart()" function
19754 */
19755 static void
19756f_strcharpart(typval_T *argvars, typval_T *rettv)
19757{
19758#ifdef FEAT_MBYTE
19759 char_u *p;
19760 int nchar;
19761 int nbyte = 0;
19762 int charlen;
19763 int len = 0;
19764 int slen;
19765 int error = FALSE;
19766
19767 p = get_tv_string(&argvars[0]);
19768 slen = (int)STRLEN(p);
19769
19770 nchar = get_tv_number_chk(&argvars[1], &error);
19771 if (!error)
19772 {
19773 if (nchar > 0)
19774 while (nchar > 0 && nbyte < slen)
19775 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019776 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019777 --nchar;
19778 }
19779 else
19780 nbyte = nchar;
19781 if (argvars[2].v_type != VAR_UNKNOWN)
19782 {
19783 charlen = get_tv_number(&argvars[2]);
19784 while (charlen > 0 && nbyte + len < slen)
19785 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019786 int off = nbyte + len;
19787
19788 if (off < 0)
19789 len += 1;
19790 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019791 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019792 --charlen;
19793 }
19794 }
19795 else
19796 len = slen - nbyte; /* default: all bytes that are available. */
19797 }
19798
19799 /*
19800 * Only return the overlap between the specified part and the actual
19801 * string.
19802 */
19803 if (nbyte < 0)
19804 {
19805 len += nbyte;
19806 nbyte = 0;
19807 }
19808 else if (nbyte > slen)
19809 nbyte = slen;
19810 if (len < 0)
19811 len = 0;
19812 else if (nbyte + len > slen)
19813 len = slen - nbyte;
19814
19815 rettv->v_type = VAR_STRING;
19816 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19817#else
19818 f_strpart(argvars, rettv);
19819#endif
19820}
19821
19822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019823 * "strpart()" function
19824 */
19825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019826f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019827{
19828 char_u *p;
19829 int n;
19830 int len;
19831 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019832 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019834 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019835 slen = (int)STRLEN(p);
19836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019837 n = get_tv_number_chk(&argvars[1], &error);
19838 if (error)
19839 len = 0;
19840 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842 else
19843 len = slen - n; /* default len: all bytes that are available. */
19844
19845 /*
19846 * Only return the overlap between the specified part and the actual
19847 * string.
19848 */
19849 if (n < 0)
19850 {
19851 len += n;
19852 n = 0;
19853 }
19854 else if (n > slen)
19855 n = slen;
19856 if (len < 0)
19857 len = 0;
19858 else if (n + len > slen)
19859 len = slen - n;
19860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019861 rettv->v_type = VAR_STRING;
19862 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863}
19864
19865/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019866 * "strridx()" function
19867 */
19868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019869f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019870{
19871 char_u buf[NUMBUFLEN];
19872 char_u *needle;
19873 char_u *haystack;
19874 char_u *rest;
19875 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019876 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019878 needle = get_tv_string_chk(&argvars[1]);
19879 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019880
19881 rettv->vval.v_number = -1;
19882 if (needle == NULL || haystack == NULL)
19883 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019884
19885 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019886 if (argvars[2].v_type != VAR_UNKNOWN)
19887 {
19888 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019889 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019890 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019891 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019892 }
19893 else
19894 end_idx = haystack_len;
19895
Bram Moolenaar0d660222005-01-07 21:51:51 +000019896 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019897 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019898 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019899 lastmatch = haystack + end_idx;
19900 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019901 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019902 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019903 for (rest = haystack; *rest != '\0'; ++rest)
19904 {
19905 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019906 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019907 break;
19908 lastmatch = rest;
19909 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019910 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019911
19912 if (lastmatch == NULL)
19913 rettv->vval.v_number = -1;
19914 else
19915 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19916}
19917
19918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 * "strtrans()" function
19920 */
19921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019922f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019924 rettv->v_type = VAR_STRING;
19925 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019926}
19927
19928/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019929 * "submatch()" function
19930 */
19931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019932f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019933{
Bram Moolenaar41571762014-04-02 19:00:58 +020019934 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019935 int no;
19936 int retList = 0;
19937
19938 no = (int)get_tv_number_chk(&argvars[0], &error);
19939 if (error)
19940 return;
19941 error = FALSE;
19942 if (argvars[1].v_type != VAR_UNKNOWN)
19943 retList = get_tv_number_chk(&argvars[1], &error);
19944 if (error)
19945 return;
19946
19947 if (retList == 0)
19948 {
19949 rettv->v_type = VAR_STRING;
19950 rettv->vval.v_string = reg_submatch(no);
19951 }
19952 else
19953 {
19954 rettv->v_type = VAR_LIST;
19955 rettv->vval.v_list = reg_submatch_list(no);
19956 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019957}
19958
19959/*
19960 * "substitute()" function
19961 */
19962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019963f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019964{
19965 char_u patbuf[NUMBUFLEN];
19966 char_u subbuf[NUMBUFLEN];
19967 char_u flagsbuf[NUMBUFLEN];
19968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019969 char_u *str = get_tv_string_chk(&argvars[0]);
19970 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19971 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19972 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19973
Bram Moolenaar0d660222005-01-07 21:51:51 +000019974 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19976 rettv->vval.v_string = NULL;
19977 else
19978 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019979}
19980
19981/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019982 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019985f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986{
19987 int id = 0;
19988#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019989 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019990 long col;
19991 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019992 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019994 lnum = get_tv_lnum(argvars); /* -1 on type error */
19995 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19996 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019998 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019999 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020000 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001#endif
20002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020003 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004}
20005
20006/*
20007 * "synIDattr(id, what [, mode])" function
20008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020010f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011{
20012 char_u *p = NULL;
20013#ifdef FEAT_SYN_HL
20014 int id;
20015 char_u *what;
20016 char_u *mode;
20017 char_u modebuf[NUMBUFLEN];
20018 int modec;
20019
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020 id = get_tv_number(&argvars[0]);
20021 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020022 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020026 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 modec = 0; /* replace invalid with current */
20028 }
20029 else
20030 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020031#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020032 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 modec = 'g';
20034 else
20035#endif
20036 if (t_colors > 1)
20037 modec = 'c';
20038 else
20039 modec = 't';
20040 }
20041
20042
20043 switch (TOLOWER_ASC(what[0]))
20044 {
20045 case 'b':
20046 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20047 p = highlight_color(id, what, modec);
20048 else /* bold */
20049 p = highlight_has_attr(id, HL_BOLD, modec);
20050 break;
20051
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020052 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053 p = highlight_color(id, what, modec);
20054 break;
20055
20056 case 'i':
20057 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20058 p = highlight_has_attr(id, HL_INVERSE, modec);
20059 else /* italic */
20060 p = highlight_has_attr(id, HL_ITALIC, modec);
20061 break;
20062
20063 case 'n': /* name */
20064 p = get_highlight_name(NULL, id - 1);
20065 break;
20066
20067 case 'r': /* reverse */
20068 p = highlight_has_attr(id, HL_INVERSE, modec);
20069 break;
20070
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020071 case 's':
20072 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20073 p = highlight_color(id, what, modec);
20074 else /* standout */
20075 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020076 break;
20077
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020078 case 'u':
20079 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20080 /* underline */
20081 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20082 else
20083 /* undercurl */
20084 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 break;
20086 }
20087
20088 if (p != NULL)
20089 p = vim_strsave(p);
20090#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020091 rettv->v_type = VAR_STRING;
20092 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093}
20094
20095/*
20096 * "synIDtrans(id)" function
20097 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020099f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100{
20101 int id;
20102
20103#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020104 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020105
20106 if (id > 0)
20107 id = syn_get_final_id(id);
20108 else
20109#endif
20110 id = 0;
20111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020112 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020113}
20114
20115/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020116 * "synconcealed(lnum, col)" function
20117 */
20118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020119f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020120{
20121#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20122 long lnum;
20123 long col;
20124 int syntax_flags = 0;
20125 int cchar;
20126 int matchid = 0;
20127 char_u str[NUMBUFLEN];
20128#endif
20129
20130 rettv->v_type = VAR_LIST;
20131 rettv->vval.v_list = NULL;
20132
20133#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20134 lnum = get_tv_lnum(argvars); /* -1 on type error */
20135 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20136
20137 vim_memset(str, NUL, sizeof(str));
20138
20139 if (rettv_list_alloc(rettv) != FAIL)
20140 {
20141 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20142 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20143 && curwin->w_p_cole > 0)
20144 {
20145 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20146 syntax_flags = get_syntax_info(&matchid);
20147
20148 /* get the conceal character */
20149 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20150 {
20151 cchar = syn_get_sub_char();
20152 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20153 cchar = lcs_conceal;
20154 if (cchar != NUL)
20155 {
20156# ifdef FEAT_MBYTE
20157 if (has_mbyte)
20158 (*mb_char2bytes)(cchar, str);
20159 else
20160# endif
20161 str[0] = cchar;
20162 }
20163 }
20164 }
20165
20166 list_append_number(rettv->vval.v_list,
20167 (syntax_flags & HL_CONCEAL) != 0);
20168 /* -1 to auto-determine strlen */
20169 list_append_string(rettv->vval.v_list, str, -1);
20170 list_append_number(rettv->vval.v_list, matchid);
20171 }
20172#endif
20173}
20174
20175/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020176 * "synstack(lnum, col)" function
20177 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020179f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020180{
20181#ifdef FEAT_SYN_HL
20182 long lnum;
20183 long col;
20184 int i;
20185 int id;
20186#endif
20187
20188 rettv->v_type = VAR_LIST;
20189 rettv->vval.v_list = NULL;
20190
20191#ifdef FEAT_SYN_HL
20192 lnum = get_tv_lnum(argvars); /* -1 on type error */
20193 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20194
20195 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020196 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020197 && rettv_list_alloc(rettv) != FAIL)
20198 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020199 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020200 for (i = 0; ; ++i)
20201 {
20202 id = syn_get_stack_item(i);
20203 if (id < 0)
20204 break;
20205 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20206 break;
20207 }
20208 }
20209#endif
20210}
20211
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020213get_cmd_output_as_rettv(
20214 typval_T *argvars,
20215 typval_T *rettv,
20216 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020217{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020218 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020220 char_u *infile = NULL;
20221 char_u buf[NUMBUFLEN];
20222 int err = FALSE;
20223 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020224 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020225 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020227 rettv->v_type = VAR_STRING;
20228 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020229 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020230 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020231
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020232 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020233 {
20234 /*
20235 * Write the string to a temp file, to be used for input of the shell
20236 * command.
20237 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020238 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020239 {
20240 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020241 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020242 }
20243
20244 fd = mch_fopen((char *)infile, WRITEBIN);
20245 if (fd == NULL)
20246 {
20247 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020248 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020249 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020250 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020251 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020252 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20253 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020254 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020255 else
20256 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020257 size_t len;
20258
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020259 p = get_tv_string_buf_chk(&argvars[1], buf);
20260 if (p == NULL)
20261 {
20262 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020263 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020264 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020265 len = STRLEN(p);
20266 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020267 err = TRUE;
20268 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020269 if (fclose(fd) != 0)
20270 err = TRUE;
20271 if (err)
20272 {
20273 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020275 }
20276 }
20277
Bram Moolenaar52a72462014-08-29 15:53:52 +020020278 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20279 * echoes typeahead, that messes up the display. */
20280 if (!msg_silent)
20281 flags += SHELL_COOKED;
20282
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020283 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020285 int len;
20286 listitem_T *li;
20287 char_u *s = NULL;
20288 char_u *start;
20289 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020290 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291
Bram Moolenaar52a72462014-08-29 15:53:52 +020020292 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020293 if (res == NULL)
20294 goto errret;
20295
20296 list = list_alloc();
20297 if (list == NULL)
20298 goto errret;
20299
20300 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020302 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020303 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020304 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020305 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020306
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020307 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020308 if (s == NULL)
20309 goto errret;
20310
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020311 for (p = s; start < end; ++p, ++start)
20312 *p = *start == NUL ? NL : *start;
20313 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020314
20315 li = listitem_alloc();
20316 if (li == NULL)
20317 {
20318 vim_free(s);
20319 goto errret;
20320 }
20321 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020322 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020323 li->li_tv.vval.v_string = s;
20324 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020325 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020326
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020327 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020328 rettv->v_type = VAR_LIST;
20329 rettv->vval.v_list = list;
20330 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020332 else
20333 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020334 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020335#ifdef USE_CR
20336 /* translate <CR> into <NL> */
20337 if (res != NULL)
20338 {
20339 char_u *s;
20340
20341 for (s = res; *s; ++s)
20342 {
20343 if (*s == CAR)
20344 *s = NL;
20345 }
20346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347#else
20348# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020349 /* translate <CR><NL> into <NL> */
20350 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020351 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020352 char_u *s, *d;
20353
20354 d = res;
20355 for (s = res; *s; ++s)
20356 {
20357 if (s[0] == CAR && s[1] == NL)
20358 ++s;
20359 *d++ = *s;
20360 }
20361 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363# endif
20364#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020365 rettv->vval.v_string = res;
20366 res = NULL;
20367 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020368
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020369errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020370 if (infile != NULL)
20371 {
20372 mch_remove(infile);
20373 vim_free(infile);
20374 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020375 if (res != NULL)
20376 vim_free(res);
20377 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020378 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020379}
20380
20381/*
20382 * "system()" function
20383 */
20384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020385f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386{
20387 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20388}
20389
20390/*
20391 * "systemlist()" function
20392 */
20393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020394f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020395{
20396 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020397}
20398
20399/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020400 * "tabpagebuflist()" function
20401 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020403f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020404{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020405#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020406 tabpage_T *tp;
20407 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020408
20409 if (argvars[0].v_type == VAR_UNKNOWN)
20410 wp = firstwin;
20411 else
20412 {
20413 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20414 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020415 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020416 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020417 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020418 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020419 for (; wp != NULL; wp = wp->w_next)
20420 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020421 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020422 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020423 }
20424#endif
20425}
20426
20427
20428/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020429 * "tabpagenr()" function
20430 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020432f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020433{
20434 int nr = 1;
20435#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020436 char_u *arg;
20437
20438 if (argvars[0].v_type != VAR_UNKNOWN)
20439 {
20440 arg = get_tv_string_chk(&argvars[0]);
20441 nr = 0;
20442 if (arg != NULL)
20443 {
20444 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020445 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020446 else
20447 EMSG2(_(e_invexpr2), arg);
20448 }
20449 }
20450 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020451 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020452#endif
20453 rettv->vval.v_number = nr;
20454}
20455
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020456
20457#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020458static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020459
20460/*
20461 * Common code for tabpagewinnr() and winnr().
20462 */
20463 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020464get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020465{
20466 win_T *twin;
20467 int nr = 1;
20468 win_T *wp;
20469 char_u *arg;
20470
20471 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20472 if (argvar->v_type != VAR_UNKNOWN)
20473 {
20474 arg = get_tv_string_chk(argvar);
20475 if (arg == NULL)
20476 nr = 0; /* type error; errmsg already given */
20477 else if (STRCMP(arg, "$") == 0)
20478 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20479 else if (STRCMP(arg, "#") == 0)
20480 {
20481 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20482 if (twin == NULL)
20483 nr = 0;
20484 }
20485 else
20486 {
20487 EMSG2(_(e_invexpr2), arg);
20488 nr = 0;
20489 }
20490 }
20491
20492 if (nr > 0)
20493 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20494 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020495 {
20496 if (wp == NULL)
20497 {
20498 /* didn't find it in this tabpage */
20499 nr = 0;
20500 break;
20501 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020502 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020503 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020504 return nr;
20505}
20506#endif
20507
20508/*
20509 * "tabpagewinnr()" function
20510 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020512f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020513{
20514 int nr = 1;
20515#ifdef FEAT_WINDOWS
20516 tabpage_T *tp;
20517
20518 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20519 if (tp == NULL)
20520 nr = 0;
20521 else
20522 nr = get_winnr(tp, &argvars[1]);
20523#endif
20524 rettv->vval.v_number = nr;
20525}
20526
20527
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020528/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020529 * "tagfiles()" function
20530 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020533{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020534 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020535 tagname_T tn;
20536 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020537
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020538 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020539 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020540 fname = alloc(MAXPATHL);
20541 if (fname == NULL)
20542 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020543
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020544 for (first = TRUE; ; first = FALSE)
20545 if (get_tagfname(&tn, first, fname) == FAIL
20546 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020547 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020548 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020549 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020550}
20551
20552/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020553 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020554 */
20555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020556f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020557{
20558 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020559
20560 tag_pattern = get_tv_string(&argvars[0]);
20561
20562 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020563 if (*tag_pattern == NUL)
20564 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020565
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020566 if (rettv_list_alloc(rettv) == OK)
20567 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020568}
20569
20570/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020571 * "tempname()" function
20572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020574f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575{
20576 static int x = 'A';
20577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020578 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020579 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580
20581 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20582 * names. Skip 'I' and 'O', they are used for shell redirection. */
20583 do
20584 {
20585 if (x == 'Z')
20586 x = '0';
20587 else if (x == '9')
20588 x = 'A';
20589 else
20590 {
20591#ifdef EBCDIC
20592 if (x == 'I')
20593 x = 'J';
20594 else if (x == 'R')
20595 x = 'S';
20596 else
20597#endif
20598 ++x;
20599 }
20600 } while (x == 'I' || x == 'O');
20601}
20602
20603/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020604 * "test(list)" function: Just checking the walls...
20605 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020607f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020608{
20609 /* Used for unit testing. Change the code below to your liking. */
20610#if 0
20611 listitem_T *li;
20612 list_T *l;
20613 char_u *bad, *good;
20614
20615 if (argvars[0].v_type != VAR_LIST)
20616 return;
20617 l = argvars[0].vval.v_list;
20618 if (l == NULL)
20619 return;
20620 li = l->lv_first;
20621 if (li == NULL)
20622 return;
20623 bad = get_tv_string(&li->li_tv);
20624 li = li->li_next;
20625 if (li == NULL)
20626 return;
20627 good = get_tv_string(&li->li_tv);
20628 rettv->vval.v_number = test_edit_score(bad, good);
20629#endif
20630}
20631
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020632#ifdef FEAT_FLOAT
20633/*
20634 * "tan()" function
20635 */
20636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020637f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020638{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020639 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020640
20641 rettv->v_type = VAR_FLOAT;
20642 if (get_float_arg(argvars, &f) == OK)
20643 rettv->vval.v_float = tan(f);
20644 else
20645 rettv->vval.v_float = 0.0;
20646}
20647
20648/*
20649 * "tanh()" function
20650 */
20651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020652f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020653{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020654 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020655
20656 rettv->v_type = VAR_FLOAT;
20657 if (get_float_arg(argvars, &f) == OK)
20658 rettv->vval.v_float = tanh(f);
20659 else
20660 rettv->vval.v_float = 0.0;
20661}
20662#endif
20663
Bram Moolenaar975b5272016-03-15 23:10:59 +010020664#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20665/*
20666 * Get a callback from "arg". It can be a Funcref or a function name.
20667 * When "arg" is zero return an empty string.
20668 * Return NULL for an invalid argument.
20669 */
20670 char_u *
20671get_callback(typval_T *arg, partial_T **pp)
20672{
20673 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20674 {
20675 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020676 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020677 return (*pp)->pt_name;
20678 }
20679 *pp = NULL;
20680 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20681 return arg->vval.v_string;
20682 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20683 return (char_u *)"";
20684 EMSG(_("E921: Invalid callback argument"));
20685 return NULL;
20686}
20687#endif
20688
20689#ifdef FEAT_TIMERS
20690/*
20691 * "timer_start(time, callback [, options])" function
20692 */
20693 static void
20694f_timer_start(typval_T *argvars, typval_T *rettv)
20695{
20696 long msec = get_tv_number(&argvars[0]);
20697 timer_T *timer;
20698 int repeat = 0;
20699 char_u *callback;
20700 dict_T *dict;
20701
Bram Moolenaar38499922016-04-22 20:46:52 +020020702 if (check_secure())
20703 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020704 if (argvars[2].v_type != VAR_UNKNOWN)
20705 {
20706 if (argvars[2].v_type != VAR_DICT
20707 || (dict = argvars[2].vval.v_dict) == NULL)
20708 {
20709 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20710 return;
20711 }
20712 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20713 repeat = get_dict_number(dict, (char_u *)"repeat");
20714 }
20715
20716 timer = create_timer(msec, repeat);
20717 callback = get_callback(&argvars[1], &timer->tr_partial);
20718 if (callback == NULL)
20719 {
20720 stop_timer(timer);
20721 rettv->vval.v_number = -1;
20722 }
20723 else
20724 {
20725 timer->tr_callback = vim_strsave(callback);
20726 rettv->vval.v_number = timer->tr_id;
20727 }
20728}
20729
20730/*
20731 * "timer_stop(timer)" function
20732 */
20733 static void
20734f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20735{
20736 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20737
20738 if (timer != NULL)
20739 stop_timer(timer);
20740}
20741#endif
20742
Bram Moolenaard52d9742005-08-21 22:20:28 +000020743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020744 * "tolower(string)" function
20745 */
20746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020747f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020748{
20749 char_u *p;
20750
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020751 p = vim_strsave(get_tv_string(&argvars[0]));
20752 rettv->v_type = VAR_STRING;
20753 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754
20755 if (p != NULL)
20756 while (*p != NUL)
20757 {
20758#ifdef FEAT_MBYTE
20759 int l;
20760
20761 if (enc_utf8)
20762 {
20763 int c, lc;
20764
20765 c = utf_ptr2char(p);
20766 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020767 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020768 /* TODO: reallocate string when byte count changes. */
20769 if (utf_char2len(lc) == l)
20770 utf_char2bytes(lc, p);
20771 p += l;
20772 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020773 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 p += l; /* skip multi-byte character */
20775 else
20776#endif
20777 {
20778 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20779 ++p;
20780 }
20781 }
20782}
20783
20784/*
20785 * "toupper(string)" function
20786 */
20787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020788f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020789{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020790 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020791 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793
20794/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020795 * "tr(string, fromstr, tostr)" function
20796 */
20797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020798f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020799{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020800 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020801 char_u *fromstr;
20802 char_u *tostr;
20803 char_u *p;
20804#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020805 int inlen;
20806 int fromlen;
20807 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020808 int idx;
20809 char_u *cpstr;
20810 int cplen;
20811 int first = TRUE;
20812#endif
20813 char_u buf[NUMBUFLEN];
20814 char_u buf2[NUMBUFLEN];
20815 garray_T ga;
20816
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020817 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020818 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20819 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020820
20821 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020822 rettv->v_type = VAR_STRING;
20823 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020824 if (fromstr == NULL || tostr == NULL)
20825 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020826 ga_init2(&ga, (int)sizeof(char), 80);
20827
20828#ifdef FEAT_MBYTE
20829 if (!has_mbyte)
20830#endif
20831 /* not multi-byte: fromstr and tostr must be the same length */
20832 if (STRLEN(fromstr) != STRLEN(tostr))
20833 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020834#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020835error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020836#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020837 EMSG2(_(e_invarg2), fromstr);
20838 ga_clear(&ga);
20839 return;
20840 }
20841
20842 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020843 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020844 {
20845#ifdef FEAT_MBYTE
20846 if (has_mbyte)
20847 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020848 inlen = (*mb_ptr2len)(in_str);
20849 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020850 cplen = inlen;
20851 idx = 0;
20852 for (p = fromstr; *p != NUL; p += fromlen)
20853 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020854 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020855 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020856 {
20857 for (p = tostr; *p != NUL; p += tolen)
20858 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020859 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020860 if (idx-- == 0)
20861 {
20862 cplen = tolen;
20863 cpstr = p;
20864 break;
20865 }
20866 }
20867 if (*p == NUL) /* tostr is shorter than fromstr */
20868 goto error;
20869 break;
20870 }
20871 ++idx;
20872 }
20873
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020874 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020875 {
20876 /* Check that fromstr and tostr have the same number of
20877 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020878 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020879 first = FALSE;
20880 for (p = tostr; *p != NUL; p += tolen)
20881 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020882 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020883 --idx;
20884 }
20885 if (idx != 0)
20886 goto error;
20887 }
20888
Bram Moolenaarcde88542015-08-11 19:14:00 +020020889 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020890 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020891 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020892
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020893 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020894 }
20895 else
20896#endif
20897 {
20898 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020899 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020900 if (p != NULL)
20901 ga_append(&ga, tostr[p - fromstr]);
20902 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020903 ga_append(&ga, *in_str);
20904 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020905 }
20906 }
20907
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020908 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020909 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020910 ga_append(&ga, NUL);
20911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020913}
20914
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020915#ifdef FEAT_FLOAT
20916/*
20917 * "trunc({float})" function
20918 */
20919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020920f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020921{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020922 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020923
20924 rettv->v_type = VAR_FLOAT;
20925 if (get_float_arg(argvars, &f) == OK)
20926 /* trunc() is not in C90, use floor() or ceil() instead. */
20927 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20928 else
20929 rettv->vval.v_float = 0.0;
20930}
20931#endif
20932
Bram Moolenaar8299df92004-07-10 09:47:34 +000020933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 * "type(expr)" function
20935 */
20936 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020937f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020939 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020940
20941 switch (argvars[0].v_type)
20942 {
20943 case VAR_NUMBER: n = 0; break;
20944 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020945 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020946 case VAR_FUNC: n = 2; break;
20947 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020948 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020949 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020950 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020951 if (argvars[0].vval.v_number == VVAL_FALSE
20952 || argvars[0].vval.v_number == VVAL_TRUE)
20953 n = 6;
20954 else
20955 n = 7;
20956 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020957 case VAR_JOB: n = 8; break;
20958 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020959 case VAR_UNKNOWN:
20960 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20961 n = -1;
20962 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020963 }
20964 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965}
20966
20967/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020968 * "undofile(name)" function
20969 */
20970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020971f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020972{
20973 rettv->v_type = VAR_STRING;
20974#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020975 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020976 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020977
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020978 if (*fname == NUL)
20979 {
20980 /* If there is no file name there will be no undo file. */
20981 rettv->vval.v_string = NULL;
20982 }
20983 else
20984 {
20985 char_u *ffname = FullName_save(fname, FALSE);
20986
20987 if (ffname != NULL)
20988 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20989 vim_free(ffname);
20990 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020991 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020992#else
20993 rettv->vval.v_string = NULL;
20994#endif
20995}
20996
20997/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020998 * "undotree()" function
20999 */
21000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021001f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021002{
21003 if (rettv_dict_alloc(rettv) == OK)
21004 {
21005 dict_T *dict = rettv->vval.v_dict;
21006 list_T *list;
21007
Bram Moolenaar730cde92010-06-27 05:18:54 +020021008 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021009 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021010 dict_add_nr_str(dict, "save_last",
21011 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021012 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21013 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021014 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021015
21016 list = list_alloc();
21017 if (list != NULL)
21018 {
21019 u_eval_tree(curbuf->b_u_oldhead, list);
21020 dict_add_list(dict, "entries", list);
21021 }
21022 }
21023}
21024
21025/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021026 * "values(dict)" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021030{
21031 dict_list(argvars, rettv, 1);
21032}
21033
21034/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035 * "virtcol(string)" function
21036 */
21037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021038f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039{
21040 colnr_T vcol = 0;
21041 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021042 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021044 fp = var2fpos(&argvars[0], FALSE, &fnum);
21045 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21046 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 {
21048 getvvcol(curwin, fp, NULL, NULL, &vcol);
21049 ++vcol;
21050 }
21051
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021052 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053}
21054
21055/*
21056 * "visualmode()" function
21057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021059f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021061 char_u str[2];
21062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021063 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021064 str[0] = curbuf->b_visual_mode_eval;
21065 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021066 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021067
21068 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021069 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021070 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021071}
21072
21073/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021074 * "wildmenumode()" function
21075 */
21076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021077f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021078{
21079#ifdef FEAT_WILDMENU
21080 if (wild_menu_showing)
21081 rettv->vval.v_number = 1;
21082#endif
21083}
21084
21085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 * "winbufnr(nr)" function
21087 */
21088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021089f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090{
21091 win_T *wp;
21092
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021093 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021094 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021095 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021097 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098}
21099
21100/*
21101 * "wincol()" function
21102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021104f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105{
21106 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021107 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108}
21109
21110/*
21111 * "winheight(nr)" function
21112 */
21113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021114f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115{
21116 win_T *wp;
21117
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021118 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021119 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021121 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021122 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021123}
21124
21125/*
21126 * "winline()" function
21127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021129f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130{
21131 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021132 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133}
21134
21135/*
21136 * "winnr()" function
21137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021139f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140{
21141 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021142
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021144 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021146 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147}
21148
21149/*
21150 * "winrestcmd()" function
21151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021153f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154{
21155#ifdef FEAT_WINDOWS
21156 win_T *wp;
21157 int winnr = 1;
21158 garray_T ga;
21159 char_u buf[50];
21160
21161 ga_init2(&ga, (int)sizeof(char), 70);
21162 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21163 {
21164 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21165 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21167 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021168 ++winnr;
21169 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021170 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021172 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177}
21178
21179/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021180 * "winrestview()" function
21181 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021183f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021184{
21185 dict_T *dict;
21186
21187 if (argvars[0].v_type != VAR_DICT
21188 || (dict = argvars[0].vval.v_dict) == NULL)
21189 EMSG(_(e_invarg));
21190 else
21191 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021192 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21193 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21194 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21195 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021196#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021197 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21198 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021199#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021200 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21201 {
21202 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21203 curwin->w_set_curswant = FALSE;
21204 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021205
Bram Moolenaar82c25852014-05-28 16:47:16 +020021206 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21207 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021208#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021209 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21210 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021211#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021212 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21213 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21214 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21215 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021216
21217 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021218 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021219# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021220 win_new_width(curwin, W_WIDTH(curwin));
21221# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021222 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021223
Bram Moolenaarb851a962014-10-31 15:45:52 +010021224 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021225 curwin->w_topline = 1;
21226 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21227 curwin->w_topline = curbuf->b_ml.ml_line_count;
21228#ifdef FEAT_DIFF
21229 check_topfill(curwin, TRUE);
21230#endif
21231 }
21232}
21233
21234/*
21235 * "winsaveview()" function
21236 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021238f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021239{
21240 dict_T *dict;
21241
Bram Moolenaara800b422010-06-27 01:15:55 +020021242 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021243 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021244 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021245
21246 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21247 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21248#ifdef FEAT_VIRTUALEDIT
21249 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21250#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021251 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021252 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21253
21254 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21255#ifdef FEAT_DIFF
21256 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21257#endif
21258 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21259 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21260}
21261
21262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 * "winwidth(nr)" function
21264 */
21265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021266f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267{
21268 win_T *wp;
21269
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021270 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021274#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021275 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021277 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278#endif
21279}
21280
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021282 * "wordcount()" function
21283 */
21284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021285f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021286{
21287 if (rettv_dict_alloc(rettv) == FAIL)
21288 return;
21289 cursor_pos_info(rettv->vval.v_dict);
21290}
21291
21292/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021293 * Write list of strings to file
21294 */
21295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021296write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021297{
21298 listitem_T *li;
21299 int c;
21300 int ret = OK;
21301 char_u *s;
21302
21303 for (li = list->lv_first; li != NULL; li = li->li_next)
21304 {
21305 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21306 {
21307 if (*s == '\n')
21308 c = putc(NUL, fd);
21309 else
21310 c = putc(*s, fd);
21311 if (c == EOF)
21312 {
21313 ret = FAIL;
21314 break;
21315 }
21316 }
21317 if (!binary || li->li_next != NULL)
21318 if (putc('\n', fd) == EOF)
21319 {
21320 ret = FAIL;
21321 break;
21322 }
21323 if (ret == FAIL)
21324 {
21325 EMSG(_(e_write));
21326 break;
21327 }
21328 }
21329 return ret;
21330}
21331
21332/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021333 * "writefile()" function
21334 */
21335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021336f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021337{
21338 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021339 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021340 char_u *fname;
21341 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021342 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021343
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021344 if (check_restricted() || check_secure())
21345 return;
21346
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021347 if (argvars[0].v_type != VAR_LIST)
21348 {
21349 EMSG2(_(e_listarg), "writefile()");
21350 return;
21351 }
21352 if (argvars[0].vval.v_list == NULL)
21353 return;
21354
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021355 if (argvars[2].v_type != VAR_UNKNOWN)
21356 {
21357 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21358 binary = TRUE;
21359 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21360 append = TRUE;
21361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021362
21363 /* Always open the file in binary mode, library functions have a mind of
21364 * their own about CR-LF conversion. */
21365 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021366 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21367 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021368 {
21369 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21370 ret = -1;
21371 }
21372 else
21373 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021374 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21375 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021376 fclose(fd);
21377 }
21378
21379 rettv->vval.v_number = ret;
21380}
21381
21382/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021383 * "xor(expr, expr)" function
21384 */
21385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021386f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021387{
21388 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21389 ^ get_tv_number_chk(&argvars[1], NULL);
21390}
21391
21392
21393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021395 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 */
21397 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021398var2fpos(
21399 typval_T *varp,
21400 int dollar_lnum, /* TRUE when $ is last line */
21401 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021402{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021403 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021405 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406
Bram Moolenaara5525202006-03-02 22:52:09 +000021407 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021408 if (varp->v_type == VAR_LIST)
21409 {
21410 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021411 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021412 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021413 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021414
21415 l = varp->vval.v_list;
21416 if (l == NULL)
21417 return NULL;
21418
21419 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021420 pos.lnum = list_find_nr(l, 0L, &error);
21421 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021422 return NULL; /* invalid line number */
21423
21424 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021425 pos.col = list_find_nr(l, 1L, &error);
21426 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021427 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021428 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021429
21430 /* We accept "$" for the column number: last column. */
21431 li = list_find(l, 1L);
21432 if (li != NULL && li->li_tv.v_type == VAR_STRING
21433 && li->li_tv.vval.v_string != NULL
21434 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21435 pos.col = len + 1;
21436
Bram Moolenaara5525202006-03-02 22:52:09 +000021437 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021438 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021439 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021440 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021441
Bram Moolenaara5525202006-03-02 22:52:09 +000021442#ifdef FEAT_VIRTUALEDIT
21443 /* Get the virtual offset. Defaults to zero. */
21444 pos.coladd = list_find_nr(l, 2L, &error);
21445 if (error)
21446 pos.coladd = 0;
21447#endif
21448
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021449 return &pos;
21450 }
21451
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021452 name = get_tv_string_chk(varp);
21453 if (name == NULL)
21454 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021455 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021457 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21458 {
21459 if (VIsual_active)
21460 return &VIsual;
21461 return &curwin->w_cursor;
21462 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021463 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021465 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21467 return NULL;
21468 return pp;
21469 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021470
21471#ifdef FEAT_VIRTUALEDIT
21472 pos.coladd = 0;
21473#endif
21474
Bram Moolenaar477933c2007-07-17 14:32:23 +000021475 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021476 {
21477 pos.col = 0;
21478 if (name[1] == '0') /* "w0": first visible line */
21479 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021480 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021481 pos.lnum = curwin->w_topline;
21482 return &pos;
21483 }
21484 else if (name[1] == '$') /* "w$": last visible line */
21485 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021486 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021487 pos.lnum = curwin->w_botline - 1;
21488 return &pos;
21489 }
21490 }
21491 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021492 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021493 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494 {
21495 pos.lnum = curbuf->b_ml.ml_line_count;
21496 pos.col = 0;
21497 }
21498 else
21499 {
21500 pos.lnum = curwin->w_cursor.lnum;
21501 pos.col = (colnr_T)STRLEN(ml_get_curline());
21502 }
21503 return &pos;
21504 }
21505 return NULL;
21506}
21507
21508/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021509 * Convert list in "arg" into a position and optional file number.
21510 * When "fnump" is NULL there is no file number, only 3 items.
21511 * Note that the column is passed on as-is, the caller may want to decrement
21512 * it to use 1 for the first column.
21513 * Return FAIL when conversion is not possible, doesn't check the position for
21514 * validity.
21515 */
21516 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021517list2fpos(
21518 typval_T *arg,
21519 pos_T *posp,
21520 int *fnump,
21521 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021522{
21523 list_T *l = arg->vval.v_list;
21524 long i = 0;
21525 long n;
21526
Bram Moolenaar493c1782014-05-28 14:34:46 +020021527 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21528 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021529 if (arg->v_type != VAR_LIST
21530 || l == NULL
21531 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021532 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021533 return FAIL;
21534
21535 if (fnump != NULL)
21536 {
21537 n = list_find_nr(l, i++, NULL); /* fnum */
21538 if (n < 0)
21539 return FAIL;
21540 if (n == 0)
21541 n = curbuf->b_fnum; /* current buffer */
21542 *fnump = n;
21543 }
21544
21545 n = list_find_nr(l, i++, NULL); /* lnum */
21546 if (n < 0)
21547 return FAIL;
21548 posp->lnum = n;
21549
21550 n = list_find_nr(l, i++, NULL); /* col */
21551 if (n < 0)
21552 return FAIL;
21553 posp->col = n;
21554
21555#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021556 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021557 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021558 posp->coladd = 0;
21559 else
21560 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021561#endif
21562
Bram Moolenaar493c1782014-05-28 14:34:46 +020021563 if (curswantp != NULL)
21564 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21565
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021566 return OK;
21567}
21568
21569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 * Get the length of an environment variable name.
21571 * Advance "arg" to the first character after the name.
21572 * Return 0 for error.
21573 */
21574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021575get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576{
21577 char_u *p;
21578 int len;
21579
21580 for (p = *arg; vim_isIDc(*p); ++p)
21581 ;
21582 if (p == *arg) /* no name found */
21583 return 0;
21584
21585 len = (int)(p - *arg);
21586 *arg = p;
21587 return len;
21588}
21589
21590/*
21591 * Get the length of the name of a function or internal variable.
21592 * "arg" is advanced to the first non-white character after the name.
21593 * Return 0 if something is wrong.
21594 */
21595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021596get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597{
21598 char_u *p;
21599 int len;
21600
21601 /* Find the end of the name. */
21602 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021603 {
21604 if (*p == ':')
21605 {
21606 /* "s:" is start of "s:var", but "n:" is not and can be used in
21607 * slice "[n:]". Also "xx:" is not a namespace. */
21608 len = (int)(p - *arg);
21609 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21610 || len > 1)
21611 break;
21612 }
21613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 if (p == *arg) /* no name found */
21615 return 0;
21616
21617 len = (int)(p - *arg);
21618 *arg = skipwhite(p);
21619
21620 return len;
21621}
21622
21623/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021624 * Get the length of the name of a variable or function.
21625 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021626 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021627 * Return -1 if curly braces expansion failed.
21628 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 * If the name contains 'magic' {}'s, expand them and return the
21630 * expanded name in an allocated string via 'alias' - caller must free.
21631 */
21632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021633get_name_len(
21634 char_u **arg,
21635 char_u **alias,
21636 int evaluate,
21637 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638{
21639 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640 char_u *p;
21641 char_u *expr_start;
21642 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643
21644 *alias = NULL; /* default to no alias */
21645
21646 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21647 && (*arg)[2] == (int)KE_SNR)
21648 {
21649 /* hard coded <SNR>, already translated */
21650 *arg += 3;
21651 return get_id_len(arg) + 3;
21652 }
21653 len = eval_fname_script(*arg);
21654 if (len > 0)
21655 {
21656 /* literal "<SID>", "s:" or "<SNR>" */
21657 *arg += len;
21658 }
21659
Bram Moolenaar071d4272004-06-13 20:20:40 +000021660 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021661 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021663 p = find_name_end(*arg, &expr_start, &expr_end,
21664 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 if (expr_start != NULL)
21666 {
21667 char_u *temp_string;
21668
21669 if (!evaluate)
21670 {
21671 len += (int)(p - *arg);
21672 *arg = skipwhite(p);
21673 return len;
21674 }
21675
21676 /*
21677 * Include any <SID> etc in the expanded string:
21678 * Thus the -len here.
21679 */
21680 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21681 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021682 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 *alias = temp_string;
21684 *arg = skipwhite(p);
21685 return (int)STRLEN(temp_string);
21686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687
21688 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021689 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021690 EMSG2(_(e_invexpr2), *arg);
21691
21692 return len;
21693}
21694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021695/*
21696 * Find the end of a variable or function name, taking care of magic braces.
21697 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21698 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021699 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021700 * Return a pointer to just after the name. Equal to "arg" if there is no
21701 * valid name.
21702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021704find_name_end(
21705 char_u *arg,
21706 char_u **expr_start,
21707 char_u **expr_end,
21708 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021710 int mb_nest = 0;
21711 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021713 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021715 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021717 *expr_start = NULL;
21718 *expr_end = NULL;
21719 }
21720
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021721 /* Quick check for valid starting character. */
21722 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21723 return arg;
21724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021725 for (p = arg; *p != NUL
21726 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021727 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021728 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021729 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021730 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021731 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021732 if (*p == '\'')
21733 {
21734 /* skip over 'string' to avoid counting [ and ] inside it. */
21735 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21736 ;
21737 if (*p == NUL)
21738 break;
21739 }
21740 else if (*p == '"')
21741 {
21742 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21743 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21744 if (*p == '\\' && p[1] != NUL)
21745 ++p;
21746 if (*p == NUL)
21747 break;
21748 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021749 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21750 {
21751 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021752 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021753 len = (int)(p - arg);
21754 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021755 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021756 break;
21757 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021759 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021761 if (*p == '[')
21762 ++br_nest;
21763 else if (*p == ']')
21764 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021767 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021769 if (*p == '{')
21770 {
21771 mb_nest++;
21772 if (expr_start != NULL && *expr_start == NULL)
21773 *expr_start = p;
21774 }
21775 else if (*p == '}')
21776 {
21777 mb_nest--;
21778 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21779 *expr_end = p;
21780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 }
21783
21784 return p;
21785}
21786
21787/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021788 * Expands out the 'magic' {}'s in a variable/function name.
21789 * Note that this can call itself recursively, to deal with
21790 * constructs like foo{bar}{baz}{bam}
21791 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21792 * "in_start" ^
21793 * "expr_start" ^
21794 * "expr_end" ^
21795 * "in_end" ^
21796 *
21797 * Returns a new allocated string, which the caller must free.
21798 * Returns NULL for failure.
21799 */
21800 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021801make_expanded_name(
21802 char_u *in_start,
21803 char_u *expr_start,
21804 char_u *expr_end,
21805 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021806{
21807 char_u c1;
21808 char_u *retval = NULL;
21809 char_u *temp_result;
21810 char_u *nextcmd = NULL;
21811
21812 if (expr_end == NULL || in_end == NULL)
21813 return NULL;
21814 *expr_start = NUL;
21815 *expr_end = NUL;
21816 c1 = *in_end;
21817 *in_end = NUL;
21818
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021819 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021820 if (temp_result != NULL && nextcmd == NULL)
21821 {
21822 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21823 + (in_end - expr_end) + 1));
21824 if (retval != NULL)
21825 {
21826 STRCPY(retval, in_start);
21827 STRCAT(retval, temp_result);
21828 STRCAT(retval, expr_end + 1);
21829 }
21830 }
21831 vim_free(temp_result);
21832
21833 *in_end = c1; /* put char back for error messages */
21834 *expr_start = '{';
21835 *expr_end = '}';
21836
21837 if (retval != NULL)
21838 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021839 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021840 if (expr_start != NULL)
21841 {
21842 /* Further expansion! */
21843 temp_result = make_expanded_name(retval, expr_start,
21844 expr_end, temp_result);
21845 vim_free(retval);
21846 retval = temp_result;
21847 }
21848 }
21849
21850 return retval;
21851}
21852
21853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021855 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 */
21857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021858eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021860 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21861}
21862
21863/*
21864 * Return TRUE if character "c" can be used as the first character in a
21865 * variable or function name (excluding '{' and '}').
21866 */
21867 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021868eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021869{
21870 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871}
21872
21873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 * Set number v: variable to "val".
21875 */
21876 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021877set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021878{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021879 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880}
21881
21882/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021883 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884 */
21885 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021886get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021887{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021888 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889}
21890
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021891/*
21892 * Get string v: variable value. Uses a static buffer, can only be used once.
21893 */
21894 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021895get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021896{
21897 return get_tv_string(&vimvars[idx].vv_tv);
21898}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021899
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021901 * Get List v: variable value. Caller must take care of reference count when
21902 * needed.
21903 */
21904 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021905get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021906{
21907 return vimvars[idx].vv_list;
21908}
21909
21910/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021911 * Set v:char to character "c".
21912 */
21913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021914set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021915{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021916 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021917
21918#ifdef FEAT_MBYTE
21919 if (has_mbyte)
21920 buf[(*mb_char2bytes)(c, buf)] = NUL;
21921 else
21922#endif
21923 {
21924 buf[0] = c;
21925 buf[1] = NUL;
21926 }
21927 set_vim_var_string(VV_CHAR, buf, -1);
21928}
21929
21930/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021931 * Set v:count to "count" and v:count1 to "count1".
21932 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 */
21934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021935set_vcount(
21936 long count,
21937 long count1,
21938 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021940 if (set_prevcount)
21941 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021942 vimvars[VV_COUNT].vv_nr = count;
21943 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944}
21945
21946/*
21947 * Set string v: variable to a copy of "val".
21948 */
21949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021950set_vim_var_string(
21951 int idx,
21952 char_u *val,
21953 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954{
Bram Moolenaara542c682016-01-31 16:28:04 +010021955 clear_tv(&vimvars[idx].vv_di.di_tv);
21956 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021957 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021958 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021959 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021960 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021962 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963}
21964
21965/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021966 * Set List v: variable to "val".
21967 */
21968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021969set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021970{
Bram Moolenaara542c682016-01-31 16:28:04 +010021971 clear_tv(&vimvars[idx].vv_di.di_tv);
21972 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021973 vimvars[idx].vv_list = val;
21974 if (val != NULL)
21975 ++val->lv_refcount;
21976}
21977
21978/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021979 * Set Dictionary v: variable to "val".
21980 */
21981 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021982set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021983{
21984 int todo;
21985 hashitem_T *hi;
21986
Bram Moolenaara542c682016-01-31 16:28:04 +010021987 clear_tv(&vimvars[idx].vv_di.di_tv);
21988 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021989 vimvars[idx].vv_dict = val;
21990 if (val != NULL)
21991 {
21992 ++val->dv_refcount;
21993
21994 /* Set readonly */
21995 todo = (int)val->dv_hashtab.ht_used;
21996 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21997 {
21998 if (HASHITEM_EMPTY(hi))
21999 continue;
22000 --todo;
22001 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22002 }
22003 }
22004}
22005
22006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022007 * Set v:register if needed.
22008 */
22009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022010set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022011{
22012 char_u regname;
22013
22014 if (c == 0 || c == ' ')
22015 regname = '"';
22016 else
22017 regname = c;
22018 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022019 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 set_vim_var_string(VV_REG, &regname, 1);
22021}
22022
22023/*
22024 * Get or set v:exception. If "oldval" == NULL, return the current value.
22025 * Otherwise, restore the value to "oldval" and return NULL.
22026 * Must always be called in pairs to save and restore v:exception! Does not
22027 * take care of memory allocations.
22028 */
22029 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022030v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031{
22032 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022033 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034
Bram Moolenaare9a41262005-01-15 22:18:47 +000022035 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 return NULL;
22037}
22038
22039/*
22040 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22041 * Otherwise, restore the value to "oldval" and return NULL.
22042 * Must always be called in pairs to save and restore v:throwpoint! Does not
22043 * take care of memory allocations.
22044 */
22045 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022046v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047{
22048 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022049 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050
Bram Moolenaare9a41262005-01-15 22:18:47 +000022051 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 return NULL;
22053}
22054
22055#if defined(FEAT_AUTOCMD) || defined(PROTO)
22056/*
22057 * Set v:cmdarg.
22058 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22059 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22060 * Must always be called in pairs!
22061 */
22062 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022063set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022064{
22065 char_u *oldval;
22066 char_u *newval;
22067 unsigned len;
22068
Bram Moolenaare9a41262005-01-15 22:18:47 +000022069 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022070 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022072 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022073 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022074 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 }
22076
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022077 if (eap->force_bin == FORCE_BIN)
22078 len = 6;
22079 else if (eap->force_bin == FORCE_NOBIN)
22080 len = 8;
22081 else
22082 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022083
22084 if (eap->read_edit)
22085 len += 7;
22086
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022087 if (eap->force_ff != 0)
22088 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22089# ifdef FEAT_MBYTE
22090 if (eap->force_enc != 0)
22091 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022092 if (eap->bad_char != 0)
22093 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022094# endif
22095
22096 newval = alloc(len + 1);
22097 if (newval == NULL)
22098 return NULL;
22099
22100 if (eap->force_bin == FORCE_BIN)
22101 sprintf((char *)newval, " ++bin");
22102 else if (eap->force_bin == FORCE_NOBIN)
22103 sprintf((char *)newval, " ++nobin");
22104 else
22105 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022106
22107 if (eap->read_edit)
22108 STRCAT(newval, " ++edit");
22109
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022110 if (eap->force_ff != 0)
22111 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22112 eap->cmd + eap->force_ff);
22113# ifdef FEAT_MBYTE
22114 if (eap->force_enc != 0)
22115 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22116 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022117 if (eap->bad_char == BAD_KEEP)
22118 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22119 else if (eap->bad_char == BAD_DROP)
22120 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22121 else if (eap->bad_char != 0)
22122 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022123# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022124 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022125 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126}
22127#endif
22128
22129/*
22130 * Get the value of internal variable "name".
22131 * Return OK or FAIL.
22132 */
22133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022134get_var_tv(
22135 char_u *name,
22136 int len, /* length of "name" */
22137 typval_T *rettv, /* NULL when only checking existence */
22138 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22139 int verbose, /* may give error message */
22140 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022141{
22142 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022143 typval_T *tv = NULL;
22144 typval_T atv;
22145 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022146 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022147
22148 /* truncate the name, so that we can use strcmp() */
22149 cc = name[len];
22150 name[len] = NUL;
22151
22152 /*
22153 * Check for "b:changedtick".
22154 */
22155 if (STRCMP(name, "b:changedtick") == 0)
22156 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022157 atv.v_type = VAR_NUMBER;
22158 atv.vval.v_number = curbuf->b_changedtick;
22159 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160 }
22161
22162 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 * Check for user-defined variables.
22164 */
22165 else
22166 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022167 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022169 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022170 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022171 if (dip != NULL)
22172 *dip = v;
22173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174 }
22175
Bram Moolenaare9a41262005-01-15 22:18:47 +000022176 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022177 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022178 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022179 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180 ret = FAIL;
22181 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022182 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022183 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184
22185 name[len] = cc;
22186
22187 return ret;
22188}
22189
22190/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022191 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22192 * Also handle function call with Funcref variable: func(expr)
22193 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22194 */
22195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022196handle_subscript(
22197 char_u **arg,
22198 typval_T *rettv,
22199 int evaluate, /* do more than finding the end */
22200 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022201{
22202 int ret = OK;
22203 dict_T *selfdict = NULL;
22204 char_u *s;
22205 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022206 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022207
22208 while (ret == OK
22209 && (**arg == '['
22210 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022211 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22212 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022213 && !vim_iswhite(*(*arg - 1)))
22214 {
22215 if (**arg == '(')
22216 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022217 partial_T *pt = NULL;
22218
Bram Moolenaard9fba312005-06-26 22:34:35 +000022219 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022220 if (evaluate)
22221 {
22222 functv = *rettv;
22223 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022224
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022225 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022226 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022227 {
22228 pt = functv.vval.v_partial;
22229 s = pt->pt_name;
22230 }
22231 else
22232 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022233 }
22234 else
22235 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022236 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022237 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022238 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022239
22240 /* Clear the funcref afterwards, so that deleting it while
22241 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022242 if (evaluate)
22243 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022244
22245 /* Stop the expression evaluation when immediately aborting on
22246 * error, or when an interrupt occurred or an exception was thrown
22247 * but not caught. */
22248 if (aborting())
22249 {
22250 if (ret == OK)
22251 clear_tv(rettv);
22252 ret = FAIL;
22253 }
22254 dict_unref(selfdict);
22255 selfdict = NULL;
22256 }
22257 else /* **arg == '[' || **arg == '.' */
22258 {
22259 dict_unref(selfdict);
22260 if (rettv->v_type == VAR_DICT)
22261 {
22262 selfdict = rettv->vval.v_dict;
22263 if (selfdict != NULL)
22264 ++selfdict->dv_refcount;
22265 }
22266 else
22267 selfdict = NULL;
22268 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22269 {
22270 clear_tv(rettv);
22271 ret = FAIL;
22272 }
22273 }
22274 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022275
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022276 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22277 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022278 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022279 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22280 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022281 char_u *tofree = NULL;
22282 ufunc_T *fp;
22283 char_u fname_buf[FLEN_FIXED + 1];
22284 int error;
22285
22286 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022287 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022288 fp = find_func(fname);
22289 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022290
22291 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022292 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022293 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022294 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22295
22296 if (pt != NULL)
22297 {
22298 pt->pt_refcount = 1;
22299 pt->pt_dict = selfdict;
22300 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022301 if (rettv->v_type == VAR_FUNC)
22302 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022303 /* Just a function: Take over the function name and use
22304 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022305 pt->pt_name = rettv->vval.v_string;
22306 }
22307 else
22308 {
22309 partial_T *ret_pt = rettv->vval.v_partial;
22310 int i;
22311
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022312 /* Partial: copy the function name, use selfdict and copy
22313 * args. Can't take over name or args, the partial might
22314 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022315 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022316 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022317 if (ret_pt->pt_argc > 0)
22318 {
22319 pt->pt_argv = (typval_T *)alloc(
22320 sizeof(typval_T) * ret_pt->pt_argc);
22321 if (pt->pt_argv == NULL)
22322 /* out of memory: drop the arguments */
22323 pt->pt_argc = 0;
22324 else
22325 {
22326 pt->pt_argc = ret_pt->pt_argc;
22327 for (i = 0; i < pt->pt_argc; i++)
22328 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22329 }
22330 }
22331 partial_unref(ret_pt);
22332 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022333 rettv->v_type = VAR_PARTIAL;
22334 rettv->vval.v_partial = pt;
22335 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022336 }
22337 }
22338
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022339 dict_unref(selfdict);
22340 return ret;
22341}
22342
22343/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022344 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022345 * value).
22346 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022347 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022348alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022349{
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022351}
22352
22353/*
22354 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 * The string "s" must have been allocated, it is consumed.
22356 * Return NULL for out of memory, the variable otherwise.
22357 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022359alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022360{
Bram Moolenaar33570922005-01-25 22:26:29 +000022361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022363 rettv = alloc_tv();
22364 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022366 rettv->v_type = VAR_STRING;
22367 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022368 }
22369 else
22370 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022371 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372}
22373
22374/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022375 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022377 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022378free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379{
22380 if (varp != NULL)
22381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022382 switch (varp->v_type)
22383 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022384 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022385 func_unref(varp->vval.v_string);
22386 /*FALLTHROUGH*/
22387 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022388 vim_free(varp->vval.v_string);
22389 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022390 case VAR_PARTIAL:
22391 partial_unref(varp->vval.v_partial);
22392 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022393 case VAR_LIST:
22394 list_unref(varp->vval.v_list);
22395 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022396 case VAR_DICT:
22397 dict_unref(varp->vval.v_dict);
22398 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022399 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022400#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022401 job_unref(varp->vval.v_job);
22402 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022403#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022404 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022405#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022406 channel_unref(varp->vval.v_channel);
22407 break;
22408#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022409 case VAR_NUMBER:
22410 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022411 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022412 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022413 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 vim_free(varp);
22416 }
22417}
22418
22419/*
22420 * Free the memory for a variable value and set the value to NULL or 0.
22421 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022422 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022423clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424{
22425 if (varp != NULL)
22426 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022429 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022430 func_unref(varp->vval.v_string);
22431 /*FALLTHROUGH*/
22432 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022433 vim_free(varp->vval.v_string);
22434 varp->vval.v_string = NULL;
22435 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022436 case VAR_PARTIAL:
22437 partial_unref(varp->vval.v_partial);
22438 varp->vval.v_partial = NULL;
22439 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022440 case VAR_LIST:
22441 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022442 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022443 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022444 case VAR_DICT:
22445 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022446 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022447 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022448 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022449 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022450 varp->vval.v_number = 0;
22451 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022452 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022453#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022454 varp->vval.v_float = 0.0;
22455 break;
22456#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022457 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022458#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022459 job_unref(varp->vval.v_job);
22460 varp->vval.v_job = NULL;
22461#endif
22462 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022463 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022464#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022465 channel_unref(varp->vval.v_channel);
22466 varp->vval.v_channel = NULL;
22467#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022468 case VAR_UNKNOWN:
22469 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022471 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 }
22473}
22474
22475/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022476 * Set the value of a variable to NULL without freeing items.
22477 */
22478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022479init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022480{
22481 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022483}
22484
22485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 * Get the number value of a variable.
22487 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022488 * For incompatible types, return 0.
22489 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22490 * caller of incompatible types: it sets *denote to TRUE if "denote"
22491 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022493 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022494get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022496 int error = FALSE;
22497
22498 return get_tv_number_chk(varp, &error); /* return 0L on error */
22499}
22500
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022501 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022502get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022503{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022504 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022506 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022507 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022508 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022509 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022510 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022511#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022512 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022513 break;
22514#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022516 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022517 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022518 break;
22519 case VAR_STRING:
22520 if (varp->vval.v_string != NULL)
22521 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022522 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022523 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022524 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022525 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022526 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022527 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022528 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022529 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022530 case VAR_SPECIAL:
22531 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22532 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022533 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022534#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022535 EMSG(_("E910: Using a Job as a Number"));
22536 break;
22537#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022538 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022539#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022540 EMSG(_("E913: Using a Channel as a Number"));
22541 break;
22542#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022543 case VAR_UNKNOWN:
22544 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022547 if (denote == NULL) /* useful for values that must be unsigned */
22548 n = -1;
22549 else
22550 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022551 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552}
22553
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022554#ifdef FEAT_FLOAT
22555 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022556get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022557{
22558 switch (varp->v_type)
22559 {
22560 case VAR_NUMBER:
22561 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022562 case VAR_FLOAT:
22563 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022564 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022565 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022566 EMSG(_("E891: Using a Funcref as a Float"));
22567 break;
22568 case VAR_STRING:
22569 EMSG(_("E892: Using a String as a Float"));
22570 break;
22571 case VAR_LIST:
22572 EMSG(_("E893: Using a List as a Float"));
22573 break;
22574 case VAR_DICT:
22575 EMSG(_("E894: Using a Dictionary as a Float"));
22576 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022577 case VAR_SPECIAL:
22578 EMSG(_("E907: Using a special value as a Float"));
22579 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022580 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022581# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022582 EMSG(_("E911: Using a Job as a Float"));
22583 break;
22584# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022585 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022586# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022587 EMSG(_("E914: Using a Channel as a Float"));
22588 break;
22589# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022590 case VAR_UNKNOWN:
22591 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022592 break;
22593 }
22594 return 0;
22595}
22596#endif
22597
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022599 * Get the lnum from the first argument.
22600 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022601 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022602 */
22603 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022604get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605{
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 linenr_T lnum;
22608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022609 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610 if (lnum == 0) /* no valid number, try using line() */
22611 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022612 rettv.v_type = VAR_NUMBER;
22613 f_line(argvars, &rettv);
22614 lnum = rettv.vval.v_number;
22615 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 }
22617 return lnum;
22618}
22619
22620/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022621 * Get the lnum from the first argument.
22622 * Also accepts "$", then "buf" is used.
22623 * Returns 0 on error.
22624 */
22625 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022626get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022627{
22628 if (argvars[0].v_type == VAR_STRING
22629 && argvars[0].vval.v_string != NULL
22630 && argvars[0].vval.v_string[0] == '$'
22631 && buf != NULL)
22632 return buf->b_ml.ml_line_count;
22633 return get_tv_number_chk(&argvars[0], NULL);
22634}
22635
22636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 * Get the string value of a variable.
22638 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022639 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22640 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 * If the String variable has never been set, return an empty string.
22642 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022643 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22644 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022645 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022646 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022647get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648{
22649 static char_u mybuf[NUMBUFLEN];
22650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022651 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652}
22653
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022654 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022655get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022657 char_u *res = get_tv_string_buf_chk(varp, buf);
22658
22659 return res != NULL ? res : (char_u *)"";
22660}
22661
Bram Moolenaar7d647822014-04-05 21:28:56 +020022662/*
22663 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22664 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022665 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022666get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022667{
22668 static char_u mybuf[NUMBUFLEN];
22669
22670 return get_tv_string_buf_chk(varp, mybuf);
22671}
22672
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022673 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022674get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022675{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022676 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022677 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022678 case VAR_NUMBER:
22679 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22680 return buf;
22681 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022682 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022683 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022684 break;
22685 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022686 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022687 break;
22688 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022689 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022690 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022691 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022692#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022693 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022694 break;
22695#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022696 case VAR_STRING:
22697 if (varp->vval.v_string != NULL)
22698 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022699 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022700 case VAR_SPECIAL:
22701 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22702 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022703 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022704#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022705 {
22706 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022707 char *status;
22708
22709 if (job == NULL)
22710 return (char_u *)"no process";
22711 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022712 : job->jv_status == JOB_ENDED ? "dead"
22713 : "run";
22714# ifdef UNIX
22715 vim_snprintf((char *)buf, NUMBUFLEN,
22716 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022717# elif defined(WIN32)
22718 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022719 "process %ld %s",
22720 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022721 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022722# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022723 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022724 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22725# endif
22726 return buf;
22727 }
22728#endif
22729 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022730 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022731#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022732 {
22733 channel_T *channel = varp->vval.v_channel;
22734 char *status = channel_status(channel);
22735
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022736 if (channel == NULL)
22737 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22738 else
22739 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022740 "channel %d %s", channel->ch_id, status);
22741 return buf;
22742 }
22743#endif
22744 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022745 case VAR_UNKNOWN:
22746 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022747 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022749 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750}
22751
22752/*
22753 * Find variable "name" in the list of variables.
22754 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022755 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022756 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022760find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022763 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764
Bram Moolenaara7043832005-01-21 11:56:39 +000022765 ht = find_var_ht(name, &varname);
22766 if (htp != NULL)
22767 *htp = ht;
22768 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022770 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771}
22772
22773/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022774 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022775 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022777 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022778find_var_in_ht(
22779 hashtab_T *ht,
22780 int htname,
22781 char_u *varname,
22782 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022783{
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 hashitem_T *hi;
22785
22786 if (*varname == NUL)
22787 {
22788 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022789 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022791 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022792 case 'g': return &globvars_var;
22793 case 'v': return &vimvars_var;
22794 case 'b': return &curbuf->b_bufvar;
22795 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022796#ifdef FEAT_WINDOWS
22797 case 't': return &curtab->tp_winvar;
22798#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022799 case 'l': return current_funccal == NULL
22800 ? NULL : &current_funccal->l_vars_var;
22801 case 'a': return current_funccal == NULL
22802 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022803 }
22804 return NULL;
22805 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022806
22807 hi = hash_find(ht, varname);
22808 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022809 {
22810 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022811 * worked find the variable again. Don't auto-load a script if it was
22812 * loaded already, otherwise it would be loaded every time when
22813 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022814 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022815 {
22816 /* Note: script_autoload() may make "hi" invalid. It must either
22817 * be obtained again or not used. */
22818 if (!script_autoload(varname, FALSE) || aborting())
22819 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022820 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022821 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022822 if (HASHITEM_EMPTY(hi))
22823 return NULL;
22824 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022825 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022826}
22827
22828/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022829 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022830 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022831 * Set "varname" to the start of name without ':'.
22832 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022833 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022834find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022835{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022836 hashitem_T *hi;
22837
Bram Moolenaar73627d02015-08-11 15:46:09 +020022838 if (name[0] == NUL)
22839 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840 if (name[1] != ':')
22841 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022842 /* The name must not start with a colon or #. */
22843 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022844 return NULL;
22845 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022846
22847 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022848 hi = hash_find(&compat_hashtab, name);
22849 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022850 return &compat_hashtab;
22851
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022853 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022854 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 }
22856 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022857 if (*name == 'g') /* global variable */
22858 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022859 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22860 */
22861 if (vim_strchr(name + 2, ':') != NULL
22862 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022863 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022865 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022866 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022867 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022868#ifdef FEAT_WINDOWS
22869 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022870 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022871#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022872 if (*name == 'v') /* v: variable */
22873 return &vimvarht;
22874 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022875 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022876 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022877 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 if (*name == 's' /* script variable */
22879 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22880 return &SCRIPT_VARS(current_SID);
22881 return NULL;
22882}
22883
22884/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022885 * Get function call environment based on bactrace debug level
22886 */
22887 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022888get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022889{
22890 int i;
22891 funccall_T *funccal;
22892 funccall_T *temp_funccal;
22893
22894 funccal = current_funccal;
22895 if (debug_backtrace_level > 0)
22896 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022897 for (i = 0; i < debug_backtrace_level; i++)
22898 {
22899 temp_funccal = funccal->caller;
22900 if (temp_funccal)
22901 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022902 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022903 /* backtrace level overflow. reset to max */
22904 debug_backtrace_level = i;
22905 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022906 }
22907 return funccal;
22908}
22909
22910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022912 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 * Returns NULL when it doesn't exist.
22914 */
22915 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022916get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022917{
Bram Moolenaar33570922005-01-25 22:26:29 +000022918 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022920 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 if (v == NULL)
22922 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022924}
22925
22926/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022928 * sourcing this script and when executing functions defined in the script.
22929 */
22930 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022931new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932{
Bram Moolenaara7043832005-01-21 11:56:39 +000022933 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022934 hashtab_T *ht;
22935 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022936
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22938 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022939 /* Re-allocating ga_data means that an ht_array pointing to
22940 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022941 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022942 for (i = 1; i <= ga_scripts.ga_len; ++i)
22943 {
22944 ht = &SCRIPT_VARS(i);
22945 if (ht->ht_mask == HT_INIT_SIZE - 1)
22946 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022947 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022949 }
22950
Bram Moolenaar071d4272004-06-13 20:20:40 +000022951 while (ga_scripts.ga_len < id)
22952 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022953 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022954 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022955 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022956 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 }
22958 }
22959}
22960
22961/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022962 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22963 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 */
22965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022966init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022967{
Bram Moolenaar33570922005-01-25 22:26:29 +000022968 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022969 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022970 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022971 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022972 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022973 dict_var->di_tv.vval.v_dict = dict;
22974 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022975 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22977 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978}
22979
22980/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022981 * Unreference a dictionary initialized by init_var_dict().
22982 */
22983 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022984unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022985{
22986 /* Now the dict needs to be freed if no one else is using it, go back to
22987 * normal reference counting. */
22988 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22989 dict_unref(dict);
22990}
22991
22992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022993 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022994 * Frees all allocated variables and the value they contain.
22995 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022996 */
22997 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022998vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022999{
23000 vars_clear_ext(ht, TRUE);
23001}
23002
23003/*
23004 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23005 */
23006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023007vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008{
Bram Moolenaara7043832005-01-21 11:56:39 +000023009 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023010 hashitem_T *hi;
23011 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012
Bram Moolenaar33570922005-01-25 22:26:29 +000023013 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023014 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023015 for (hi = ht->ht_array; todo > 0; ++hi)
23016 {
23017 if (!HASHITEM_EMPTY(hi))
23018 {
23019 --todo;
23020
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023022 * ht_array might change then. hash_clear() takes care of it
23023 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 v = HI2DI(hi);
23025 if (free_val)
23026 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023027 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023028 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023029 }
23030 }
23031 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023032 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033}
23034
Bram Moolenaara7043832005-01-21 11:56:39 +000023035/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023036 * Delete a variable from hashtab "ht" at item "hi".
23037 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023040delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041{
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023043
23044 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023045 clear_tv(&di->di_tv);
23046 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047}
23048
23049/*
23050 * List the value of one internal variable.
23051 */
23052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023053list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023055 char_u *tofree;
23056 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023057 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023058
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023059 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023060 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023061 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023062 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063}
23064
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023066list_one_var_a(
23067 char_u *prefix,
23068 char_u *name,
23069 int type,
23070 char_u *string,
23071 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023072{
Bram Moolenaar31859182007-08-14 20:41:13 +000023073 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23074 msg_start();
23075 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076 if (name != NULL) /* "a:" vars don't have a name stored */
23077 msg_puts(name);
23078 msg_putchar(' ');
23079 msg_advance(22);
23080 if (type == VAR_NUMBER)
23081 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023082 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023083 msg_putchar('*');
23084 else if (type == VAR_LIST)
23085 {
23086 msg_putchar('[');
23087 if (*string == '[')
23088 ++string;
23089 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023090 else if (type == VAR_DICT)
23091 {
23092 msg_putchar('{');
23093 if (*string == '{')
23094 ++string;
23095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096 else
23097 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023098
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023100
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023101 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023102 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023103 if (*first)
23104 {
23105 msg_clr_eos();
23106 *first = FALSE;
23107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108}
23109
23110/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023111 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 * If the variable already exists, the value is updated.
23113 * Otherwise the variable is created.
23114 */
23115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023116set_var(
23117 char_u *name,
23118 typval_T *tv,
23119 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120{
Bram Moolenaar33570922005-01-25 22:26:29 +000023121 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023123 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023125 ht = find_var_ht(name, &varname);
23126 if (ht == NULL || *varname == NUL)
23127 {
23128 EMSG2(_(e_illvar), name);
23129 return;
23130 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023131 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023132
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023133 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23134 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023135 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023136
Bram Moolenaar33570922005-01-25 22:26:29 +000023137 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023139 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023140 if (var_check_ro(v->di_flags, name, FALSE)
23141 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023142 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023143
23144 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023145 * Handle setting internal v: variables separately where needed to
23146 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 */
23148 if (ht == &vimvarht)
23149 {
23150 if (v->di_tv.v_type == VAR_STRING)
23151 {
23152 vim_free(v->di_tv.vval.v_string);
23153 if (copy || tv->v_type != VAR_STRING)
23154 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23155 else
23156 {
23157 /* Take over the string to avoid an extra alloc/free. */
23158 v->di_tv.vval.v_string = tv->vval.v_string;
23159 tv->vval.v_string = NULL;
23160 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023161 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023163 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023164 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023165 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023166 if (STRCMP(varname, "searchforward") == 0)
23167 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023168#ifdef FEAT_SEARCH_EXTRA
23169 else if (STRCMP(varname, "hlsearch") == 0)
23170 {
23171 no_hlsearch = !v->di_tv.vval.v_number;
23172 redraw_all_later(SOME_VALID);
23173 }
23174#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023175 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023176 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023177 else if (v->di_tv.v_type != tv->v_type)
23178 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023179 }
23180
23181 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 }
23183 else /* add a new variable */
23184 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023185 /* Can't add "v:" variable. */
23186 if (ht == &vimvarht)
23187 {
23188 EMSG2(_(e_illvar), name);
23189 return;
23190 }
23191
Bram Moolenaar92124a32005-06-17 22:03:40 +000023192 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023193 if (!valid_varname(varname))
23194 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023195
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023196 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23197 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023198 if (v == NULL)
23199 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023200 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023201 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023203 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 return;
23205 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023206 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023208
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023209 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023210 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023211 else
23212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023213 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023214 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023215 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217}
23218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023219/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023220 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 * Also give an error message.
23222 */
23223 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023224var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023225{
23226 if (flags & DI_FLAGS_RO)
23227 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023228 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023229 return TRUE;
23230 }
23231 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23232 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023233 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023234 return TRUE;
23235 }
23236 return FALSE;
23237}
23238
23239/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023240 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23241 * Also give an error message.
23242 */
23243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023244var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023245{
23246 if (flags & DI_FLAGS_FIX)
23247 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023248 EMSG2(_("E795: Cannot delete variable %s"),
23249 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023250 return TRUE;
23251 }
23252 return FALSE;
23253}
23254
23255/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023256 * Check if a funcref is assigned to a valid variable name.
23257 * Return TRUE and give an error if not.
23258 */
23259 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023260var_check_func_name(
23261 char_u *name, /* points to start of variable name */
23262 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023263{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023264 /* Allow for w: b: s: and t:. */
23265 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023266 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23267 ? name[2] : name[0]))
23268 {
23269 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23270 name);
23271 return TRUE;
23272 }
23273 /* Don't allow hiding a function. When "v" is not NULL we might be
23274 * assigning another function to the same var, the type is checked
23275 * below. */
23276 if (new_var && function_exists(name))
23277 {
23278 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23279 name);
23280 return TRUE;
23281 }
23282 return FALSE;
23283}
23284
23285/*
23286 * Check if a variable name is valid.
23287 * Return FALSE and give an error if not.
23288 */
23289 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023290valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023291{
23292 char_u *p;
23293
23294 for (p = varname; *p != NUL; ++p)
23295 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23296 && *p != AUTOLOAD_CHAR)
23297 {
23298 EMSG2(_(e_illvar), varname);
23299 return FALSE;
23300 }
23301 return TRUE;
23302}
23303
23304/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023305 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023306 * Also give an error message, using "name" or _("name") when use_gettext is
23307 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023308 */
23309 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023310tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023311{
23312 if (lock & VAR_LOCKED)
23313 {
23314 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023315 name == NULL ? (char_u *)_("Unknown")
23316 : use_gettext ? (char_u *)_(name)
23317 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023318 return TRUE;
23319 }
23320 if (lock & VAR_FIXED)
23321 {
23322 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023323 name == NULL ? (char_u *)_("Unknown")
23324 : use_gettext ? (char_u *)_(name)
23325 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023326 return TRUE;
23327 }
23328 return FALSE;
23329}
23330
23331/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023332 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023333 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023334 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023335 * It is OK for "from" and "to" to point to the same item. This is used to
23336 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023337 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023339copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023341 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023342 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023343 switch (from->v_type)
23344 {
23345 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023346 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023347 to->vval.v_number = from->vval.v_number;
23348 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023349 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023350#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023351 to->vval.v_float = from->vval.v_float;
23352 break;
23353#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023354 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023355#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023356 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023357 if (to->vval.v_job != NULL)
23358 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023359 break;
23360#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023361 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023362#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023363 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023364 if (to->vval.v_channel != NULL)
23365 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023366 break;
23367#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023368 case VAR_STRING:
23369 case VAR_FUNC:
23370 if (from->vval.v_string == NULL)
23371 to->vval.v_string = NULL;
23372 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023374 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023375 if (from->v_type == VAR_FUNC)
23376 func_ref(to->vval.v_string);
23377 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023378 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023379 case VAR_PARTIAL:
23380 if (from->vval.v_partial == NULL)
23381 to->vval.v_partial = NULL;
23382 else
23383 {
23384 to->vval.v_partial = from->vval.v_partial;
23385 ++to->vval.v_partial->pt_refcount;
23386 }
23387 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023388 case VAR_LIST:
23389 if (from->vval.v_list == NULL)
23390 to->vval.v_list = NULL;
23391 else
23392 {
23393 to->vval.v_list = from->vval.v_list;
23394 ++to->vval.v_list->lv_refcount;
23395 }
23396 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023397 case VAR_DICT:
23398 if (from->vval.v_dict == NULL)
23399 to->vval.v_dict = NULL;
23400 else
23401 {
23402 to->vval.v_dict = from->vval.v_dict;
23403 ++to->vval.v_dict->dv_refcount;
23404 }
23405 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023406 case VAR_UNKNOWN:
23407 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023408 break;
23409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410}
23411
23412/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023413 * Make a copy of an item.
23414 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023415 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23416 * reference to an already copied list/dict can be used.
23417 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023418 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023420item_copy(
23421 typval_T *from,
23422 typval_T *to,
23423 int deep,
23424 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023425{
23426 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023427 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023428
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023430 {
23431 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023432 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023433 }
23434 ++recurse;
23435
23436 switch (from->v_type)
23437 {
23438 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023439 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023440 case VAR_STRING:
23441 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023442 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023443 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023444 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023445 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023446 copy_tv(from, to);
23447 break;
23448 case VAR_LIST:
23449 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023450 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023451 if (from->vval.v_list == NULL)
23452 to->vval.v_list = NULL;
23453 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23454 {
23455 /* use the copy made earlier */
23456 to->vval.v_list = from->vval.v_list->lv_copylist;
23457 ++to->vval.v_list->lv_refcount;
23458 }
23459 else
23460 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23461 if (to->vval.v_list == NULL)
23462 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023463 break;
23464 case VAR_DICT:
23465 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023466 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023467 if (from->vval.v_dict == NULL)
23468 to->vval.v_dict = NULL;
23469 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23470 {
23471 /* use the copy made earlier */
23472 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23473 ++to->vval.v_dict->dv_refcount;
23474 }
23475 else
23476 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23477 if (to->vval.v_dict == NULL)
23478 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023479 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023480 case VAR_UNKNOWN:
23481 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023482 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023483 }
23484 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023485 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023486}
23487
23488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023489 * ":echo expr1 ..." print each argument separated with a space, add a
23490 * newline at the end.
23491 * ":echon expr1 ..." print each argument plain.
23492 */
23493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023494ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495{
23496 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023497 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023498 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 char_u *p;
23500 int needclr = TRUE;
23501 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023502 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503
23504 if (eap->skip)
23505 ++emsg_skip;
23506 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23507 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023508 /* If eval1() causes an error message the text from the command may
23509 * still need to be cleared. E.g., "echo 22,44". */
23510 need_clr_eos = needclr;
23511
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023513 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 {
23515 /*
23516 * Report the invalid expression unless the expression evaluation
23517 * has been cancelled due to an aborting error, an interrupt, or an
23518 * exception.
23519 */
23520 if (!aborting())
23521 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023522 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023523 break;
23524 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023525 need_clr_eos = FALSE;
23526
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 if (!eap->skip)
23528 {
23529 if (atstart)
23530 {
23531 atstart = FALSE;
23532 /* Call msg_start() after eval1(), evaluating the expression
23533 * may cause a message to appear. */
23534 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023535 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023536 /* Mark the saved text as finishing the line, so that what
23537 * follows is displayed on a new line when scrolling back
23538 * at the more prompt. */
23539 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023542 }
23543 else if (eap->cmdidx == CMD_echo)
23544 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023545 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023546 if (p != NULL)
23547 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023549 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023550 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023551 if (*p != TAB && needclr)
23552 {
23553 /* remove any text still there from the command */
23554 msg_clr_eos();
23555 needclr = FALSE;
23556 }
23557 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023558 }
23559 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023560 {
23561#ifdef FEAT_MBYTE
23562 if (has_mbyte)
23563 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023564 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023565
23566 (void)msg_outtrans_len_attr(p, i, echo_attr);
23567 p += i - 1;
23568 }
23569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023571 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023574 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023576 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023577 arg = skipwhite(arg);
23578 }
23579 eap->nextcmd = check_nextcmd(arg);
23580
23581 if (eap->skip)
23582 --emsg_skip;
23583 else
23584 {
23585 /* remove text that may still be there from the command */
23586 if (needclr)
23587 msg_clr_eos();
23588 if (eap->cmdidx == CMD_echo)
23589 msg_end();
23590 }
23591}
23592
23593/*
23594 * ":echohl {name}".
23595 */
23596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023597ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598{
23599 int id;
23600
23601 id = syn_name2id(eap->arg);
23602 if (id == 0)
23603 echo_attr = 0;
23604 else
23605 echo_attr = syn_id2attr(id);
23606}
23607
23608/*
23609 * ":execute expr1 ..." execute the result of an expression.
23610 * ":echomsg expr1 ..." Print a message
23611 * ":echoerr expr1 ..." Print an error
23612 * Each gets spaces around each argument and a newline at the end for
23613 * echo commands
23614 */
23615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023616ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617{
23618 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023619 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023620 int ret = OK;
23621 char_u *p;
23622 garray_T ga;
23623 int len;
23624 int save_did_emsg;
23625
23626 ga_init2(&ga, 1, 80);
23627
23628 if (eap->skip)
23629 ++emsg_skip;
23630 while (*arg != NUL && *arg != '|' && *arg != '\n')
23631 {
23632 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023633 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 {
23635 /*
23636 * Report the invalid expression unless the expression evaluation
23637 * has been cancelled due to an aborting error, an interrupt, or an
23638 * exception.
23639 */
23640 if (!aborting())
23641 EMSG2(_(e_invexpr2), p);
23642 ret = FAIL;
23643 break;
23644 }
23645
23646 if (!eap->skip)
23647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023648 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 len = (int)STRLEN(p);
23650 if (ga_grow(&ga, len + 2) == FAIL)
23651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023652 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653 ret = FAIL;
23654 break;
23655 }
23656 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 ga.ga_len += len;
23660 }
23661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023662 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 arg = skipwhite(arg);
23664 }
23665
23666 if (ret != FAIL && ga.ga_data != NULL)
23667 {
23668 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023671 out_flush();
23672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 else if (eap->cmdidx == CMD_echoerr)
23674 {
23675 /* We don't want to abort following commands, restore did_emsg. */
23676 save_did_emsg = did_emsg;
23677 EMSG((char_u *)ga.ga_data);
23678 if (!force_abort)
23679 did_emsg = save_did_emsg;
23680 }
23681 else if (eap->cmdidx == CMD_execute)
23682 do_cmdline((char_u *)ga.ga_data,
23683 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23684 }
23685
23686 ga_clear(&ga);
23687
23688 if (eap->skip)
23689 --emsg_skip;
23690
23691 eap->nextcmd = check_nextcmd(arg);
23692}
23693
23694/*
23695 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23696 * "arg" points to the "&" or '+' when called, to "option" when returning.
23697 * Returns NULL when no option name found. Otherwise pointer to the char
23698 * after the option name.
23699 */
23700 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023701find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702{
23703 char_u *p = *arg;
23704
23705 ++p;
23706 if (*p == 'g' && p[1] == ':')
23707 {
23708 *opt_flags = OPT_GLOBAL;
23709 p += 2;
23710 }
23711 else if (*p == 'l' && p[1] == ':')
23712 {
23713 *opt_flags = OPT_LOCAL;
23714 p += 2;
23715 }
23716 else
23717 *opt_flags = 0;
23718
23719 if (!ASCII_ISALPHA(*p))
23720 return NULL;
23721 *arg = p;
23722
23723 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23724 p += 4; /* termcap option */
23725 else
23726 while (ASCII_ISALPHA(*p))
23727 ++p;
23728 return p;
23729}
23730
23731/*
23732 * ":function"
23733 */
23734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023735ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736{
23737 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023738 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 int j;
23740 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023742 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023743 char_u *name = NULL;
23744 char_u *p;
23745 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023746 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 garray_T newargs;
23748 garray_T newlines;
23749 int varargs = FALSE;
23750 int mustend = FALSE;
23751 int flags = 0;
23752 ufunc_T *fp;
23753 int indent;
23754 int nesting;
23755 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023756 dictitem_T *v;
23757 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023758 static int func_nr = 0; /* number for nameless function */
23759 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023760 hashtab_T *ht;
23761 int todo;
23762 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023763 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764
23765 /*
23766 * ":function" without argument: list functions.
23767 */
23768 if (ends_excmd(*eap->arg))
23769 {
23770 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023771 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023772 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023773 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023774 {
23775 if (!HASHITEM_EMPTY(hi))
23776 {
23777 --todo;
23778 fp = HI2UF(hi);
23779 if (!isdigit(*fp->uf_name))
23780 list_func_head(fp, FALSE);
23781 }
23782 }
23783 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 eap->nextcmd = check_nextcmd(eap->arg);
23785 return;
23786 }
23787
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023788 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023789 * ":function /pat": list functions matching pattern.
23790 */
23791 if (*eap->arg == '/')
23792 {
23793 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23794 if (!eap->skip)
23795 {
23796 regmatch_T regmatch;
23797
23798 c = *p;
23799 *p = NUL;
23800 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23801 *p = c;
23802 if (regmatch.regprog != NULL)
23803 {
23804 regmatch.rm_ic = p_ic;
23805
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023806 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023807 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23808 {
23809 if (!HASHITEM_EMPTY(hi))
23810 {
23811 --todo;
23812 fp = HI2UF(hi);
23813 if (!isdigit(*fp->uf_name)
23814 && vim_regexec(&regmatch, fp->uf_name, 0))
23815 list_func_head(fp, FALSE);
23816 }
23817 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023818 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023819 }
23820 }
23821 if (*p == '/')
23822 ++p;
23823 eap->nextcmd = check_nextcmd(p);
23824 return;
23825 }
23826
23827 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023828 * Get the function name. There are these situations:
23829 * func normal function name
23830 * "name" == func, "fudi.fd_dict" == NULL
23831 * dict.func new dictionary entry
23832 * "name" == NULL, "fudi.fd_dict" set,
23833 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23834 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023835 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23837 * dict.func existing dict entry that's not a Funcref
23838 * "name" == NULL, "fudi.fd_dict" set,
23839 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023840 * s:func script-local function name
23841 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023844 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023845 paren = (vim_strchr(p, '(') != NULL);
23846 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023847 {
23848 /*
23849 * Return on an invalid expression in braces, unless the expression
23850 * evaluation has been cancelled due to an aborting error, an
23851 * interrupt, or an exception.
23852 */
23853 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023854 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023855 if (!eap->skip && fudi.fd_newkey != NULL)
23856 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023857 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023858 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 else
23861 eap->skip = TRUE;
23862 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023863
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 /* An error in a function call during evaluation of an expression in magic
23865 * braces should not cause the function not to be defined. */
23866 saved_did_emsg = did_emsg;
23867 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868
23869 /*
23870 * ":function func" with only function name: list function.
23871 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023872 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 {
23874 if (!ends_excmd(*skipwhite(p)))
23875 {
23876 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 }
23879 eap->nextcmd = check_nextcmd(p);
23880 if (eap->nextcmd != NULL)
23881 *p = NUL;
23882 if (!eap->skip && !got_int)
23883 {
23884 fp = find_func(name);
23885 if (fp != NULL)
23886 {
23887 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023888 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023890 if (FUNCLINE(fp, j) == NULL)
23891 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 msg_putchar('\n');
23893 msg_outnum((long)(j + 1));
23894 if (j < 9)
23895 msg_putchar(' ');
23896 if (j < 99)
23897 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023898 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023899 out_flush(); /* show a line at a time */
23900 ui_breakcheck();
23901 }
23902 if (!got_int)
23903 {
23904 msg_putchar('\n');
23905 msg_puts((char_u *)" endfunction");
23906 }
23907 }
23908 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023909 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023911 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912 }
23913
23914 /*
23915 * ":function name(arg1, arg2)" Define function.
23916 */
23917 p = skipwhite(p);
23918 if (*p != '(')
23919 {
23920 if (!eap->skip)
23921 {
23922 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023923 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023924 }
23925 /* attempt to continue by skipping some text */
23926 if (vim_strchr(p, '(') != NULL)
23927 p = vim_strchr(p, '(');
23928 }
23929 p = skipwhite(p + 1);
23930
23931 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23932 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23933
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023934 if (!eap->skip)
23935 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023936 /* Check the name of the function. Unless it's a dictionary function
23937 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023938 if (name != NULL)
23939 arg = name;
23940 else
23941 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023942 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023943 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23944 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023945 {
23946 if (*arg == K_SPECIAL)
23947 j = 3;
23948 else
23949 j = 0;
23950 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23951 : eval_isnamec(arg[j])))
23952 ++j;
23953 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023954 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023955 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023956 /* Disallow using the g: dict. */
23957 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23958 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023959 }
23960
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 /*
23962 * Isolate the arguments: "arg1, arg2, ...)"
23963 */
23964 while (*p != ')')
23965 {
23966 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23967 {
23968 varargs = TRUE;
23969 p += 3;
23970 mustend = TRUE;
23971 }
23972 else
23973 {
23974 arg = p;
23975 while (ASCII_ISALNUM(*p) || *p == '_')
23976 ++p;
23977 if (arg == p || isdigit(*arg)
23978 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23979 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23980 {
23981 if (!eap->skip)
23982 EMSG2(_("E125: Illegal argument: %s"), arg);
23983 break;
23984 }
23985 if (ga_grow(&newargs, 1) == FAIL)
23986 goto erret;
23987 c = *p;
23988 *p = NUL;
23989 arg = vim_strsave(arg);
23990 if (arg == NULL)
23991 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023992
23993 /* Check for duplicate argument name. */
23994 for (i = 0; i < newargs.ga_len; ++i)
23995 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23996 {
23997 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023998 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023999 goto erret;
24000 }
24001
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24003 *p = c;
24004 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 if (*p == ',')
24006 ++p;
24007 else
24008 mustend = TRUE;
24009 }
24010 p = skipwhite(p);
24011 if (mustend && *p != ')')
24012 {
24013 if (!eap->skip)
24014 EMSG2(_(e_invarg2), eap->arg);
24015 break;
24016 }
24017 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024018 if (*p != ')')
24019 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024020 ++p; /* skip the ')' */
24021
Bram Moolenaare9a41262005-01-15 22:18:47 +000024022 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 for (;;)
24024 {
24025 p = skipwhite(p);
24026 if (STRNCMP(p, "range", 5) == 0)
24027 {
24028 flags |= FC_RANGE;
24029 p += 5;
24030 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024031 else if (STRNCMP(p, "dict", 4) == 0)
24032 {
24033 flags |= FC_DICT;
24034 p += 4;
24035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 else if (STRNCMP(p, "abort", 5) == 0)
24037 {
24038 flags |= FC_ABORT;
24039 p += 5;
24040 }
24041 else
24042 break;
24043 }
24044
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024045 /* When there is a line break use what follows for the function body.
24046 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24047 if (*p == '\n')
24048 line_arg = p + 1;
24049 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024050 EMSG(_(e_trailing));
24051
24052 /*
24053 * Read the body of the function, until ":endfunction" is found.
24054 */
24055 if (KeyTyped)
24056 {
24057 /* Check if the function already exists, don't let the user type the
24058 * whole function before telling him it doesn't work! For a script we
24059 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024060 if (!eap->skip && !eap->forceit)
24061 {
24062 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24063 EMSG(_(e_funcdict));
24064 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024065 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024068 if (!eap->skip && did_emsg)
24069 goto erret;
24070
Bram Moolenaar071d4272004-06-13 20:20:40 +000024071 msg_putchar('\n'); /* don't overwrite the function name */
24072 cmdline_row = msg_row;
24073 }
24074
24075 indent = 2;
24076 nesting = 0;
24077 for (;;)
24078 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024079 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024080 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024081 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024082 saved_wait_return = FALSE;
24083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024085 sourcing_lnum_off = sourcing_lnum;
24086
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024087 if (line_arg != NULL)
24088 {
24089 /* Use eap->arg, split up in parts by line breaks. */
24090 theline = line_arg;
24091 p = vim_strchr(theline, '\n');
24092 if (p == NULL)
24093 line_arg += STRLEN(line_arg);
24094 else
24095 {
24096 *p = NUL;
24097 line_arg = p + 1;
24098 }
24099 }
24100 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 theline = getcmdline(':', 0L, indent);
24102 else
24103 theline = eap->getline(':', eap->cookie, indent);
24104 if (KeyTyped)
24105 lines_left = Rows - 1;
24106 if (theline == NULL)
24107 {
24108 EMSG(_("E126: Missing :endfunction"));
24109 goto erret;
24110 }
24111
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024112 /* Detect line continuation: sourcing_lnum increased more than one. */
24113 if (sourcing_lnum > sourcing_lnum_off + 1)
24114 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24115 else
24116 sourcing_lnum_off = 0;
24117
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 if (skip_until != NULL)
24119 {
24120 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24121 * don't check for ":endfunc". */
24122 if (STRCMP(theline, skip_until) == 0)
24123 {
24124 vim_free(skip_until);
24125 skip_until = NULL;
24126 }
24127 }
24128 else
24129 {
24130 /* skip ':' and blanks*/
24131 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24132 ;
24133
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024134 /* Check for "endfunction". */
24135 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024137 if (line_arg == NULL)
24138 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 break;
24140 }
24141
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024142 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 * at "end". */
24144 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24145 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024146 else if (STRNCMP(p, "if", 2) == 0
24147 || STRNCMP(p, "wh", 2) == 0
24148 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 || STRNCMP(p, "try", 3) == 0)
24150 indent += 2;
24151
24152 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024153 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024155 if (*p == '!')
24156 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024158 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024159 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024161 ++nesting;
24162 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024163 }
24164 }
24165
24166 /* Check for ":append" or ":insert". */
24167 p = skip_range(p, NULL);
24168 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24169 || (p[0] == 'i'
24170 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24171 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24172 skip_until = vim_strsave((char_u *)".");
24173
24174 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24175 arg = skipwhite(skiptowhite(p));
24176 if (arg[0] == '<' && arg[1] =='<'
24177 && ((p[0] == 'p' && p[1] == 'y'
24178 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24179 || (p[0] == 'p' && p[1] == 'e'
24180 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24181 || (p[0] == 't' && p[1] == 'c'
24182 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024183 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24184 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24186 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024187 || (p[0] == 'm' && p[1] == 'z'
24188 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 ))
24190 {
24191 /* ":python <<" continues until a dot, like ":append" */
24192 p = skipwhite(arg + 2);
24193 if (*p == NUL)
24194 skip_until = vim_strsave((char_u *)".");
24195 else
24196 skip_until = vim_strsave(p);
24197 }
24198 }
24199
24200 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024201 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024202 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024203 if (line_arg == NULL)
24204 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024206 }
24207
24208 /* Copy the line to newly allocated memory. get_one_sourceline()
24209 * allocates 250 bytes per line, this saves 80% on average. The cost
24210 * is an extra alloc/free. */
24211 p = vim_strsave(theline);
24212 if (p != NULL)
24213 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024214 if (line_arg == NULL)
24215 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024216 theline = p;
24217 }
24218
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024219 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24220
24221 /* Add NULL lines for continuation lines, so that the line count is
24222 * equal to the index in the growarray. */
24223 while (sourcing_lnum_off-- > 0)
24224 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024225
24226 /* Check for end of eap->arg. */
24227 if (line_arg != NULL && *line_arg == NUL)
24228 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 }
24230
24231 /* Don't define the function when skipping commands or when an error was
24232 * detected. */
24233 if (eap->skip || did_emsg)
24234 goto erret;
24235
24236 /*
24237 * If there are no errors, add the function
24238 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024239 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024240 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024241 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024242 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024243 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024244 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024245 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024246 goto erret;
24247 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024248
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024249 fp = find_func(name);
24250 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024252 if (!eap->forceit)
24253 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024254 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024255 goto erret;
24256 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024257 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024258 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024259 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024260 name);
24261 goto erret;
24262 }
24263 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024264 ga_clear_strings(&(fp->uf_args));
24265 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024266 vim_free(name);
24267 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024269 }
24270 else
24271 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024272 char numbuf[20];
24273
24274 fp = NULL;
24275 if (fudi.fd_newkey == NULL && !eap->forceit)
24276 {
24277 EMSG(_(e_funcdict));
24278 goto erret;
24279 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024280 if (fudi.fd_di == NULL)
24281 {
24282 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024283 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024284 goto erret;
24285 }
24286 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024287 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024288 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024289
24290 /* Give the function a sequential number. Can only be used with a
24291 * Funcref! */
24292 vim_free(name);
24293 sprintf(numbuf, "%d", ++func_nr);
24294 name = vim_strsave((char_u *)numbuf);
24295 if (name == NULL)
24296 goto erret;
24297 }
24298
24299 if (fp == NULL)
24300 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024301 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024302 {
24303 int slen, plen;
24304 char_u *scriptname;
24305
24306 /* Check that the autoload name matches the script name. */
24307 j = FAIL;
24308 if (sourcing_name != NULL)
24309 {
24310 scriptname = autoload_name(name);
24311 if (scriptname != NULL)
24312 {
24313 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024314 plen = (int)STRLEN(p);
24315 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024316 if (slen > plen && fnamecmp(p,
24317 sourcing_name + slen - plen) == 0)
24318 j = OK;
24319 vim_free(scriptname);
24320 }
24321 }
24322 if (j == FAIL)
24323 {
24324 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24325 goto erret;
24326 }
24327 }
24328
24329 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024330 if (fp == NULL)
24331 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024332
24333 if (fudi.fd_dict != NULL)
24334 {
24335 if (fudi.fd_di == NULL)
24336 {
24337 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024338 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024339 if (fudi.fd_di == NULL)
24340 {
24341 vim_free(fp);
24342 goto erret;
24343 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024344 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24345 {
24346 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024347 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024348 goto erret;
24349 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024350 }
24351 else
24352 /* overwrite existing dict entry */
24353 clear_tv(&fudi.fd_di->di_tv);
24354 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024355 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024357 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024358
24359 /* behave like "dict" was used */
24360 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024361 }
24362
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024364 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024365 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24366 {
24367 vim_free(fp);
24368 goto erret;
24369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024371 fp->uf_args = newargs;
24372 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024373#ifdef FEAT_PROFILE
24374 fp->uf_tml_count = NULL;
24375 fp->uf_tml_total = NULL;
24376 fp->uf_tml_self = NULL;
24377 fp->uf_profiling = FALSE;
24378 if (prof_def_func())
24379 func_do_profile(fp);
24380#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024381 fp->uf_varargs = varargs;
24382 fp->uf_flags = flags;
24383 fp->uf_calls = 0;
24384 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024385 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386
24387erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 ga_clear_strings(&newargs);
24389 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024390ret_free:
24391 vim_free(skip_until);
24392 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024393 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024394 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024395 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024396}
24397
24398/*
24399 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024400 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024401 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024402 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024403 * TFN_INT: internal function name OK
24404 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024405 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 * Advances "pp" to just after the function name (if no error).
24407 */
24408 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024409trans_function_name(
24410 char_u **pp,
24411 int skip, /* only find the end, don't evaluate */
24412 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024413 funcdict_T *fdp, /* return: info about dictionary used */
24414 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024415{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024416 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024417 char_u *start;
24418 char_u *end;
24419 int lead;
24420 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024422 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024423
24424 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024425 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024427
24428 /* Check for hard coded <SNR>: already translated function ID (from a user
24429 * command). */
24430 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24431 && (*pp)[2] == (int)KE_SNR)
24432 {
24433 *pp += 3;
24434 len = get_id_len(pp) + 3;
24435 return vim_strnsave(start, len);
24436 }
24437
24438 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24439 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024441 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024443
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024444 /* Note that TFN_ flags use the same values as GLV_ flags. */
24445 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024446 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024447 if (end == start)
24448 {
24449 if (!skip)
24450 EMSG(_("E129: Function name required"));
24451 goto theend;
24452 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024453 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024454 {
24455 /*
24456 * Report an invalid expression in braces, unless the expression
24457 * evaluation has been cancelled due to an aborting error, an
24458 * interrupt, or an exception.
24459 */
24460 if (!aborting())
24461 {
24462 if (end != NULL)
24463 EMSG2(_(e_invarg2), start);
24464 }
24465 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024466 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024467 goto theend;
24468 }
24469
24470 if (lv.ll_tv != NULL)
24471 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024472 if (fdp != NULL)
24473 {
24474 fdp->fd_dict = lv.ll_dict;
24475 fdp->fd_newkey = lv.ll_newkey;
24476 lv.ll_newkey = NULL;
24477 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024478 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024479 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24480 {
24481 name = vim_strsave(lv.ll_tv->vval.v_string);
24482 *pp = end;
24483 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024484 else if (lv.ll_tv->v_type == VAR_PARTIAL
24485 && lv.ll_tv->vval.v_partial != NULL)
24486 {
24487 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24488 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024489 if (partial != NULL)
24490 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024491 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024492 else
24493 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024494 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24495 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024496 EMSG(_(e_funcref));
24497 else
24498 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024499 name = NULL;
24500 }
24501 goto theend;
24502 }
24503
24504 if (lv.ll_name == NULL)
24505 {
24506 /* Error found, but continue after the function name. */
24507 *pp = end;
24508 goto theend;
24509 }
24510
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024511 /* Check if the name is a Funcref. If so, use the value. */
24512 if (lv.ll_exp_name != NULL)
24513 {
24514 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024515 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024516 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024517 if (name == lv.ll_exp_name)
24518 name = NULL;
24519 }
24520 else
24521 {
24522 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024523 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024524 if (name == *pp)
24525 name = NULL;
24526 }
24527 if (name != NULL)
24528 {
24529 name = vim_strsave(name);
24530 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024531 if (STRNCMP(name, "<SNR>", 5) == 0)
24532 {
24533 /* Change "<SNR>" to the byte sequence. */
24534 name[0] = K_SPECIAL;
24535 name[1] = KS_EXTRA;
24536 name[2] = (int)KE_SNR;
24537 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24538 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024539 goto theend;
24540 }
24541
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024542 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024543 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024544 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024545 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24546 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24547 {
24548 /* When there was "s:" already or the name expanded to get a
24549 * leading "s:" then remove it. */
24550 lv.ll_name += 2;
24551 len -= 2;
24552 lead = 2;
24553 }
24554 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024555 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024556 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024557 /* skip over "s:" and "g:" */
24558 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024559 lv.ll_name += 2;
24560 len = (int)(end - lv.ll_name);
24561 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024562
24563 /*
24564 * Copy the function name to allocated memory.
24565 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24566 * Accept <SNR>123_name() outside a script.
24567 */
24568 if (skip)
24569 lead = 0; /* do nothing */
24570 else if (lead > 0)
24571 {
24572 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024573 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24574 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024575 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024576 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024577 if (current_SID <= 0)
24578 {
24579 EMSG(_(e_usingsid));
24580 goto theend;
24581 }
24582 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24583 lead += (int)STRLEN(sid_buf);
24584 }
24585 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024586 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024587 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024588 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024589 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024590 goto theend;
24591 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024592 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024593 {
24594 char_u *cp = vim_strchr(lv.ll_name, ':');
24595
24596 if (cp != NULL && cp < end)
24597 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024598 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024599 goto theend;
24600 }
24601 }
24602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024603 name = alloc((unsigned)(len + lead + 1));
24604 if (name != NULL)
24605 {
24606 if (lead > 0)
24607 {
24608 name[0] = K_SPECIAL;
24609 name[1] = KS_EXTRA;
24610 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024611 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024612 STRCPY(name + 3, sid_buf);
24613 }
24614 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024615 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024616 }
24617 *pp = end;
24618
24619theend:
24620 clear_lval(&lv);
24621 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622}
24623
24624/*
24625 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24626 * Return 2 if "p" starts with "s:".
24627 * Return 0 otherwise.
24628 */
24629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024630eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024631{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024632 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24633 * the standard library function. */
24634 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24635 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 return 5;
24637 if (p[0] == 's' && p[1] == ':')
24638 return 2;
24639 return 0;
24640}
24641
24642/*
24643 * Return TRUE if "p" starts with "<SID>" or "s:".
24644 * Only works if eval_fname_script() returned non-zero for "p"!
24645 */
24646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024647eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024648{
24649 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24650}
24651
24652/*
24653 * List the head of the function: "name(arg1, arg2)".
24654 */
24655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024656list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657{
24658 int j;
24659
24660 msg_start();
24661 if (indent)
24662 MSG_PUTS(" ");
24663 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024664 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665 {
24666 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024667 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668 }
24669 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024670 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024672 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 {
24674 if (j)
24675 MSG_PUTS(", ");
24676 msg_puts(FUNCARG(fp, j));
24677 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024678 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024679 {
24680 if (j)
24681 MSG_PUTS(", ");
24682 MSG_PUTS("...");
24683 }
24684 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024685 if (fp->uf_flags & FC_ABORT)
24686 MSG_PUTS(" abort");
24687 if (fp->uf_flags & FC_RANGE)
24688 MSG_PUTS(" range");
24689 if (fp->uf_flags & FC_DICT)
24690 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024691 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024692 if (p_verbose > 0)
24693 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024694}
24695
24696/*
24697 * Find a function by name, return pointer to it in ufuncs.
24698 * Return NULL for unknown function.
24699 */
24700 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024701find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024702{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024703 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024704
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 hi = hash_find(&func_hashtab, name);
24706 if (!HASHITEM_EMPTY(hi))
24707 return HI2UF(hi);
24708 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024709}
24710
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024711#if defined(EXITFREE) || defined(PROTO)
24712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024713free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024714{
24715 hashitem_T *hi;
24716
24717 /* Need to start all over every time, because func_free() may change the
24718 * hash table. */
24719 while (func_hashtab.ht_used > 0)
24720 for (hi = func_hashtab.ht_array; ; ++hi)
24721 if (!HASHITEM_EMPTY(hi))
24722 {
24723 func_free(HI2UF(hi));
24724 break;
24725 }
24726}
24727#endif
24728
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024729 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024730translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024731{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024732 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024733 return find_internal_func(name) >= 0;
24734 return find_func(name) != NULL;
24735}
24736
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024737/*
24738 * Return TRUE if a function "name" exists.
24739 */
24740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024741function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024742{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024743 char_u *nm = name;
24744 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024745 int n = FALSE;
24746
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024747 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024748 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024749 nm = skipwhite(nm);
24750
24751 /* Only accept "funcname", "funcname ", "funcname (..." and
24752 * "funcname(...", not "funcname!...". */
24753 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024754 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024755 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024756 return n;
24757}
24758
Bram Moolenaara1544c02013-05-30 12:35:52 +020024759 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024760get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024761{
24762 char_u *nm = name;
24763 char_u *p;
24764
Bram Moolenaar65639032016-03-16 21:40:30 +010024765 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024766
24767 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024768 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024769 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024770
Bram Moolenaara1544c02013-05-30 12:35:52 +020024771 vim_free(p);
24772 return NULL;
24773}
24774
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024775/*
24776 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024777 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24778 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024779 */
24780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024781builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024782{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024783 char_u *p;
24784
24785 if (!ASCII_ISLOWER(name[0]))
24786 return FALSE;
24787 p = vim_strchr(name, AUTOLOAD_CHAR);
24788 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024789}
24790
Bram Moolenaar05159a02005-02-26 23:04:13 +000024791#if defined(FEAT_PROFILE) || defined(PROTO)
24792/*
24793 * Start profiling function "fp".
24794 */
24795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024796func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024797{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024798 int len = fp->uf_lines.ga_len;
24799
24800 if (len == 0)
24801 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024802 fp->uf_tm_count = 0;
24803 profile_zero(&fp->uf_tm_self);
24804 profile_zero(&fp->uf_tm_total);
24805 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024806 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024807 if (fp->uf_tml_total == NULL)
24808 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024809 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024810 if (fp->uf_tml_self == NULL)
24811 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024812 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024813 fp->uf_tml_idx = -1;
24814 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24815 || fp->uf_tml_self == NULL)
24816 return; /* out of memory */
24817
24818 fp->uf_profiling = TRUE;
24819}
24820
24821/*
24822 * Dump the profiling results for all functions in file "fd".
24823 */
24824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024825func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024826{
24827 hashitem_T *hi;
24828 int todo;
24829 ufunc_T *fp;
24830 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024831 ufunc_T **sorttab;
24832 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024833
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024834 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024835 if (todo == 0)
24836 return; /* nothing to dump */
24837
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024838 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024839
Bram Moolenaar05159a02005-02-26 23:04:13 +000024840 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24841 {
24842 if (!HASHITEM_EMPTY(hi))
24843 {
24844 --todo;
24845 fp = HI2UF(hi);
24846 if (fp->uf_profiling)
24847 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024848 if (sorttab != NULL)
24849 sorttab[st_len++] = fp;
24850
Bram Moolenaar05159a02005-02-26 23:04:13 +000024851 if (fp->uf_name[0] == K_SPECIAL)
24852 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24853 else
24854 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24855 if (fp->uf_tm_count == 1)
24856 fprintf(fd, "Called 1 time\n");
24857 else
24858 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24859 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24860 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24861 fprintf(fd, "\n");
24862 fprintf(fd, "count total (s) self (s)\n");
24863
24864 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24865 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024866 if (FUNCLINE(fp, i) == NULL)
24867 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024868 prof_func_line(fd, fp->uf_tml_count[i],
24869 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024870 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24871 }
24872 fprintf(fd, "\n");
24873 }
24874 }
24875 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024876
24877 if (sorttab != NULL && st_len > 0)
24878 {
24879 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24880 prof_total_cmp);
24881 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24882 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24883 prof_self_cmp);
24884 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24885 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024886
24887 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024888}
Bram Moolenaar73830342005-02-28 22:48:19 +000024889
24890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024891prof_sort_list(
24892 FILE *fd,
24893 ufunc_T **sorttab,
24894 int st_len,
24895 char *title,
24896 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024897{
24898 int i;
24899 ufunc_T *fp;
24900
24901 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24902 fprintf(fd, "count total (s) self (s) function\n");
24903 for (i = 0; i < 20 && i < st_len; ++i)
24904 {
24905 fp = sorttab[i];
24906 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24907 prefer_self);
24908 if (fp->uf_name[0] == K_SPECIAL)
24909 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24910 else
24911 fprintf(fd, " %s()\n", fp->uf_name);
24912 }
24913 fprintf(fd, "\n");
24914}
24915
24916/*
24917 * Print the count and times for one function or function line.
24918 */
24919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024920prof_func_line(
24921 FILE *fd,
24922 int count,
24923 proftime_T *total,
24924 proftime_T *self,
24925 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024926{
24927 if (count > 0)
24928 {
24929 fprintf(fd, "%5d ", count);
24930 if (prefer_self && profile_equal(total, self))
24931 fprintf(fd, " ");
24932 else
24933 fprintf(fd, "%s ", profile_msg(total));
24934 if (!prefer_self && profile_equal(total, self))
24935 fprintf(fd, " ");
24936 else
24937 fprintf(fd, "%s ", profile_msg(self));
24938 }
24939 else
24940 fprintf(fd, " ");
24941}
24942
24943/*
24944 * Compare function for total time sorting.
24945 */
24946 static int
24947#ifdef __BORLANDC__
24948_RTLENTRYF
24949#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024950prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024951{
24952 ufunc_T *p1, *p2;
24953
24954 p1 = *(ufunc_T **)s1;
24955 p2 = *(ufunc_T **)s2;
24956 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24957}
24958
24959/*
24960 * Compare function for self time sorting.
24961 */
24962 static int
24963#ifdef __BORLANDC__
24964_RTLENTRYF
24965#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024966prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024967{
24968 ufunc_T *p1, *p2;
24969
24970 p1 = *(ufunc_T **)s1;
24971 p2 = *(ufunc_T **)s2;
24972 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24973}
24974
Bram Moolenaar05159a02005-02-26 23:04:13 +000024975#endif
24976
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024977/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024978 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024979 * Return TRUE if a package was loaded.
24980 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024982script_autoload(
24983 char_u *name,
24984 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024985{
24986 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024987 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024988 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024989 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024990
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024991 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024992 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024993 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024994 return FALSE;
24995
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024996 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024997
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024998 /* Find the name in the list of previously loaded package names. Skip
24999 * "autoload/", it's always the same. */
25000 for (i = 0; i < ga_loaded.ga_len; ++i)
25001 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25002 break;
25003 if (!reload && i < ga_loaded.ga_len)
25004 ret = FALSE; /* was loaded already */
25005 else
25006 {
25007 /* Remember the name if it wasn't loaded already. */
25008 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25009 {
25010 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25011 tofree = NULL;
25012 }
25013
25014 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025015 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025016 ret = TRUE;
25017 }
25018
25019 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025020 return ret;
25021}
25022
25023/*
25024 * Return the autoload script name for a function or variable name.
25025 * Returns NULL when out of memory.
25026 */
25027 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025028autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025029{
25030 char_u *p;
25031 char_u *scriptname;
25032
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025033 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025034 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25035 if (scriptname == NULL)
25036 return FALSE;
25037 STRCPY(scriptname, "autoload/");
25038 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025039 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025040 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025041 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025042 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025043 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025044}
25045
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25047
25048/*
25049 * Function given to ExpandGeneric() to obtain the list of user defined
25050 * function names.
25051 */
25052 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025053get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025055 static long_u done;
25056 static hashitem_T *hi;
25057 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058
25059 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025060 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025061 done = 0;
25062 hi = func_hashtab.ht_array;
25063 }
25064 if (done < func_hashtab.ht_used)
25065 {
25066 if (done++ > 0)
25067 ++hi;
25068 while (HASHITEM_EMPTY(hi))
25069 ++hi;
25070 fp = HI2UF(hi);
25071
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025072 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025073 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025074
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025075 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25076 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077
25078 cat_func_name(IObuff, fp);
25079 if (xp->xp_context != EXPAND_USER_FUNC)
25080 {
25081 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025082 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083 STRCAT(IObuff, ")");
25084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025085 return IObuff;
25086 }
25087 return NULL;
25088}
25089
25090#endif /* FEAT_CMDL_COMPL */
25091
25092/*
25093 * Copy the function name of "fp" to buffer "buf".
25094 * "buf" must be able to hold the function name plus three bytes.
25095 * Takes care of script-local function names.
25096 */
25097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025098cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025099{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025100 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101 {
25102 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025103 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 }
25105 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025106 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107}
25108
25109/*
25110 * ":delfunction {name}"
25111 */
25112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025113ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025114{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025115 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025116 char_u *p;
25117 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025118 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119
25120 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025121 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025122 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025123 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025124 {
25125 if (fudi.fd_dict != NULL && !eap->skip)
25126 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025127 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025129 if (!ends_excmd(*skipwhite(p)))
25130 {
25131 vim_free(name);
25132 EMSG(_(e_trailing));
25133 return;
25134 }
25135 eap->nextcmd = check_nextcmd(p);
25136 if (eap->nextcmd != NULL)
25137 *p = NUL;
25138
25139 if (!eap->skip)
25140 fp = find_func(name);
25141 vim_free(name);
25142
25143 if (!eap->skip)
25144 {
25145 if (fp == NULL)
25146 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025147 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148 return;
25149 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025150 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151 {
25152 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25153 return;
25154 }
25155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025156 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025157 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025158 /* Delete the dict item that refers to the function, it will
25159 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025160 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025161 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025162 else
25163 func_free(fp);
25164 }
25165}
25166
25167/*
25168 * Free a function and remove it from the list of functions.
25169 */
25170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025171func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025172{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025173 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025174
25175 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025176 ga_clear_strings(&(fp->uf_args));
25177 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025178#ifdef FEAT_PROFILE
25179 vim_free(fp->uf_tml_count);
25180 vim_free(fp->uf_tml_total);
25181 vim_free(fp->uf_tml_self);
25182#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025183
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025184 /* remove the function from the function hashtable */
25185 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25186 if (HASHITEM_EMPTY(hi))
25187 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025188 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025189 hash_remove(&func_hashtab, hi);
25190
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025191 vim_free(fp);
25192}
25193
25194/*
25195 * Unreference a Function: decrement the reference count and free it when it
25196 * becomes zero. Only for numbered functions.
25197 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025199func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025200{
25201 ufunc_T *fp;
25202
25203 if (name != NULL && isdigit(*name))
25204 {
25205 fp = find_func(name);
25206 if (fp == NULL)
25207 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025208 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025209 {
25210 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025211 * when "uf_calls" becomes zero. */
25212 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025213 func_free(fp);
25214 }
25215 }
25216}
25217
25218/*
25219 * Count a reference to a Function.
25220 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025222func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025223{
25224 ufunc_T *fp;
25225
25226 if (name != NULL && isdigit(*name))
25227 {
25228 fp = find_func(name);
25229 if (fp == NULL)
25230 EMSG2(_(e_intern2), "func_ref()");
25231 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025232 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233 }
25234}
25235
25236/*
25237 * Call a user function.
25238 */
25239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025240call_user_func(
25241 ufunc_T *fp, /* pointer to function */
25242 int argcount, /* nr of args */
25243 typval_T *argvars, /* arguments */
25244 typval_T *rettv, /* return value */
25245 linenr_T firstline, /* first line of range */
25246 linenr_T lastline, /* last line of range */
25247 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248{
Bram Moolenaar33570922005-01-25 22:26:29 +000025249 char_u *save_sourcing_name;
25250 linenr_T save_sourcing_lnum;
25251 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025252 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025253 int save_did_emsg;
25254 static int depth = 0;
25255 dictitem_T *v;
25256 int fixvar_idx = 0; /* index in fixvar[] */
25257 int i;
25258 int ai;
25259 char_u numbuf[NUMBUFLEN];
25260 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025261 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025262#ifdef FEAT_PROFILE
25263 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025264 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266
25267 /* If depth of calling is getting too high, don't execute the function */
25268 if (depth >= p_mfd)
25269 {
25270 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025271 rettv->v_type = VAR_NUMBER;
25272 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273 return;
25274 }
25275 ++depth;
25276
25277 line_breakcheck(); /* check for CTRL-C hit */
25278
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025279 fc = (funccall_T *)alloc(sizeof(funccall_T));
25280 fc->caller = current_funccal;
25281 current_funccal = fc;
25282 fc->func = fp;
25283 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025284 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025285 fc->linenr = 0;
25286 fc->returned = FALSE;
25287 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025289 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25290 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025293 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025294 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25295 * each argument variable and saves a lot of time.
25296 */
25297 /*
25298 * Init l: variables.
25299 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025300 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025301 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025302 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025303 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25304 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025305 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025306 name = v->di_key;
25307 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025308 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025309 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025310 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025311 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025312 v->di_tv.vval.v_dict = selfdict;
25313 ++selfdict->dv_refcount;
25314 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025315
Bram Moolenaar33570922005-01-25 22:26:29 +000025316 /*
25317 * Init a: variables.
25318 * Set a:0 to "argcount".
25319 * Set a:000 to a list with room for the "..." arguments.
25320 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025321 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025322 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025323 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025324 /* Use "name" to avoid a warning from some compiler that checks the
25325 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025326 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025327 name = v->di_key;
25328 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025329 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025330 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025331 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025332 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025333 v->di_tv.vval.v_list = &fc->l_varlist;
25334 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25335 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25336 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025337
25338 /*
25339 * Set a:firstline to "firstline" and a:lastline to "lastline".
25340 * Set a:name to named arguments.
25341 * Set a:N to the "..." arguments.
25342 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025343 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025344 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025345 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025346 (varnumber_T)lastline);
25347 for (i = 0; i < argcount; ++i)
25348 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025349 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025350 if (ai < 0)
25351 /* named argument a:name */
25352 name = FUNCARG(fp, i);
25353 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025355 /* "..." argument a:1, a:2, etc. */
25356 sprintf((char *)numbuf, "%d", ai + 1);
25357 name = numbuf;
25358 }
25359 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25360 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025361 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025362 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25363 }
25364 else
25365 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025366 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25367 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025368 if (v == NULL)
25369 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025370 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025371 }
25372 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025373 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025374
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025375 /* Note: the values are copied directly to avoid alloc/free.
25376 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025377 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025378 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025379
25380 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25381 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025382 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25383 fc->l_listitems[ai].li_tv = argvars[i];
25384 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025385 }
25386 }
25387
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 /* Don't redraw while executing the function. */
25389 ++RedrawingDisabled;
25390 save_sourcing_name = sourcing_name;
25391 save_sourcing_lnum = sourcing_lnum;
25392 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025393 /* need space for function name + ("function " + 3) or "[number]" */
25394 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25395 + STRLEN(fp->uf_name) + 20;
25396 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 if (sourcing_name != NULL)
25398 {
25399 if (save_sourcing_name != NULL
25400 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025401 sprintf((char *)sourcing_name, "%s[%d]..",
25402 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025403 else
25404 STRCPY(sourcing_name, "function ");
25405 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25406
25407 if (p_verbose >= 12)
25408 {
25409 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025410 verbose_enter_scroll();
25411
Bram Moolenaar555b2802005-05-19 21:08:39 +000025412 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025413 if (p_verbose >= 14)
25414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025416 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025417 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025418 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419
25420 msg_puts((char_u *)"(");
25421 for (i = 0; i < argcount; ++i)
25422 {
25423 if (i > 0)
25424 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025425 if (argvars[i].v_type == VAR_NUMBER)
25426 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427 else
25428 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025429 /* Do not want errors such as E724 here. */
25430 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025431 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025432 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025433 if (s != NULL)
25434 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025435 if (vim_strsize(s) > MSG_BUF_CLEN)
25436 {
25437 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25438 s = buf;
25439 }
25440 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025441 vim_free(tofree);
25442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025443 }
25444 }
25445 msg_puts((char_u *)")");
25446 }
25447 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025448
25449 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450 --no_wait_return;
25451 }
25452 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025453#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025454 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025455 {
25456 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25457 func_do_profile(fp);
25458 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025459 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025460 {
25461 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025462 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025463 profile_zero(&fp->uf_tm_children);
25464 }
25465 script_prof_save(&wait_start);
25466 }
25467#endif
25468
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025470 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025471 save_did_emsg = did_emsg;
25472 did_emsg = FALSE;
25473
25474 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025475 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25477
25478 --RedrawingDisabled;
25479
25480 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025481 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025483 clear_tv(rettv);
25484 rettv->v_type = VAR_NUMBER;
25485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 }
25487
Bram Moolenaar05159a02005-02-26 23:04:13 +000025488#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025489 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025490 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025491 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025492 profile_end(&call_start);
25493 profile_sub_wait(&wait_start, &call_start);
25494 profile_add(&fp->uf_tm_total, &call_start);
25495 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025496 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025497 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025498 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25499 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025500 }
25501 }
25502#endif
25503
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 /* when being verbose, mention the return value */
25505 if (p_verbose >= 12)
25506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025508 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025511 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025512 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025513 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025514 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025515 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025517 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025518 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025519 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025520 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025521
Bram Moolenaar555b2802005-05-19 21:08:39 +000025522 /* The value may be very long. Skip the middle part, so that we
25523 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025524 * truncate it at the end. Don't want errors such as E724 here. */
25525 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025526 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025527 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025528 if (s != NULL)
25529 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025530 if (vim_strsize(s) > MSG_BUF_CLEN)
25531 {
25532 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25533 s = buf;
25534 }
25535 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025536 vim_free(tofree);
25537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 }
25539 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025540
25541 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 --no_wait_return;
25543 }
25544
25545 vim_free(sourcing_name);
25546 sourcing_name = save_sourcing_name;
25547 sourcing_lnum = save_sourcing_lnum;
25548 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025549#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025550 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025551 script_prof_restore(&wait_start);
25552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553
25554 if (p_verbose >= 12 && sourcing_name != NULL)
25555 {
25556 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025557 verbose_enter_scroll();
25558
Bram Moolenaar555b2802005-05-19 21:08:39 +000025559 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025561
25562 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025563 --no_wait_return;
25564 }
25565
25566 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025567 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025568 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025569
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025570 /* If the a:000 list and the l: and a: dicts are not referenced we can
25571 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025572 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25573 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25574 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25575 {
25576 free_funccal(fc, FALSE);
25577 }
25578 else
25579 {
25580 hashitem_T *hi;
25581 listitem_T *li;
25582 int todo;
25583
25584 /* "fc" is still in use. This can happen when returning "a:000" or
25585 * assigning "l:" to a global variable.
25586 * Link "fc" in the list for garbage collection later. */
25587 fc->caller = previous_funccal;
25588 previous_funccal = fc;
25589
25590 /* Make a copy of the a: variables, since we didn't do that above. */
25591 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25592 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25593 {
25594 if (!HASHITEM_EMPTY(hi))
25595 {
25596 --todo;
25597 v = HI2DI(hi);
25598 copy_tv(&v->di_tv, &v->di_tv);
25599 }
25600 }
25601
25602 /* Make a copy of the a:000 items, since we didn't do that above. */
25603 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25604 copy_tv(&li->li_tv, &li->li_tv);
25605 }
25606}
25607
25608/*
25609 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025610 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025611 */
25612 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025613can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025614{
25615 return (fc->l_varlist.lv_copyID != copyID
25616 && fc->l_vars.dv_copyID != copyID
25617 && fc->l_avars.dv_copyID != copyID);
25618}
25619
25620/*
25621 * Free "fc" and what it contains.
25622 */
25623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025624free_funccal(
25625 funccall_T *fc,
25626 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025627{
25628 listitem_T *li;
25629
25630 /* The a: variables typevals may not have been allocated, only free the
25631 * allocated variables. */
25632 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25633
25634 /* free all l: variables */
25635 vars_clear(&fc->l_vars.dv_hashtab);
25636
25637 /* Free the a:000 variables if they were allocated. */
25638 if (free_val)
25639 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25640 clear_tv(&li->li_tv);
25641
25642 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025643}
25644
25645/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025646 * Add a number variable "name" to dict "dp" with value "nr".
25647 */
25648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025649add_nr_var(
25650 dict_T *dp,
25651 dictitem_T *v,
25652 char *name,
25653 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025654{
25655 STRCPY(v->di_key, name);
25656 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25657 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25658 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025659 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025660 v->di_tv.vval.v_number = nr;
25661}
25662
25663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664 * ":return [expr]"
25665 */
25666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025667ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668{
25669 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025670 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 int returning = FALSE;
25672
25673 if (current_funccal == NULL)
25674 {
25675 EMSG(_("E133: :return not inside a function"));
25676 return;
25677 }
25678
25679 if (eap->skip)
25680 ++emsg_skip;
25681
25682 eap->nextcmd = NULL;
25683 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025684 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025685 {
25686 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025687 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025688 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025689 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025690 }
25691 /* It's safer to return also on error. */
25692 else if (!eap->skip)
25693 {
25694 /*
25695 * Return unless the expression evaluation has been cancelled due to an
25696 * aborting error, an interrupt, or an exception.
25697 */
25698 if (!aborting())
25699 returning = do_return(eap, FALSE, TRUE, NULL);
25700 }
25701
25702 /* When skipping or the return gets pending, advance to the next command
25703 * in this line (!returning). Otherwise, ignore the rest of the line.
25704 * Following lines will be ignored by get_func_line(). */
25705 if (returning)
25706 eap->nextcmd = NULL;
25707 else if (eap->nextcmd == NULL) /* no argument */
25708 eap->nextcmd = check_nextcmd(arg);
25709
25710 if (eap->skip)
25711 --emsg_skip;
25712}
25713
25714/*
25715 * Return from a function. Possibly makes the return pending. Also called
25716 * for a pending return at the ":endtry" or after returning from an extra
25717 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025718 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025719 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 * FALSE when the return gets pending.
25721 */
25722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025723do_return(
25724 exarg_T *eap,
25725 int reanimate,
25726 int is_cmd,
25727 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025728{
25729 int idx;
25730 struct condstack *cstack = eap->cstack;
25731
25732 if (reanimate)
25733 /* Undo the return. */
25734 current_funccal->returned = FALSE;
25735
25736 /*
25737 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25738 * not in its finally clause (which then is to be executed next) is found.
25739 * In this case, make the ":return" pending for execution at the ":endtry".
25740 * Otherwise, return normally.
25741 */
25742 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25743 if (idx >= 0)
25744 {
25745 cstack->cs_pending[idx] = CSTP_RETURN;
25746
25747 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025748 /* A pending return again gets pending. "rettv" points to an
25749 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025751 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 else
25753 {
25754 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025755 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025757 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025759 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760 {
25761 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025762 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025763 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025764 else
25765 EMSG(_(e_outofmem));
25766 }
25767 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025768 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769
25770 if (reanimate)
25771 {
25772 /* The pending return value could be overwritten by a ":return"
25773 * without argument in a finally clause; reset the default
25774 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025775 current_funccal->rettv->v_type = VAR_NUMBER;
25776 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 }
25778 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025779 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 }
25781 else
25782 {
25783 current_funccal->returned = TRUE;
25784
25785 /* If the return is carried out now, store the return value. For
25786 * a return immediately after reanimation, the value is already
25787 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025788 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025790 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025791 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025793 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794 }
25795 }
25796
25797 return idx < 0;
25798}
25799
25800/*
25801 * Free the variable with a pending return value.
25802 */
25803 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025804discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805{
Bram Moolenaar33570922005-01-25 22:26:29 +000025806 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807}
25808
25809/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025810 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811 * is an allocated string. Used by report_pending() for verbose messages.
25812 */
25813 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025814get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025815{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025816 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025817 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025818 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025819
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025820 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025821 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025822 if (s == NULL)
25823 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025824
25825 STRCPY(IObuff, ":return ");
25826 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25827 if (STRLEN(s) + 8 >= IOSIZE)
25828 STRCPY(IObuff + IOSIZE - 4, "...");
25829 vim_free(tofree);
25830 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025831}
25832
25833/*
25834 * Get next function line.
25835 * Called by do_cmdline() to get the next line.
25836 * Returns allocated string, or NULL for end of function.
25837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025839get_func_line(
25840 int c UNUSED,
25841 void *cookie,
25842 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843{
Bram Moolenaar33570922005-01-25 22:26:29 +000025844 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025845 ufunc_T *fp = fcp->func;
25846 char_u *retval;
25847 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848
25849 /* If breakpoints have been added/deleted need to check for it. */
25850 if (fcp->dbg_tick != debug_tick)
25851 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025852 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853 sourcing_lnum);
25854 fcp->dbg_tick = debug_tick;
25855 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025856#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025857 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025858 func_line_end(cookie);
25859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860
Bram Moolenaar05159a02005-02-26 23:04:13 +000025861 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025862 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25863 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 retval = NULL;
25865 else
25866 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025867 /* Skip NULL lines (continuation lines). */
25868 while (fcp->linenr < gap->ga_len
25869 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25870 ++fcp->linenr;
25871 if (fcp->linenr >= gap->ga_len)
25872 retval = NULL;
25873 else
25874 {
25875 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25876 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025877#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025878 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025879 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025880#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882 }
25883
25884 /* Did we encounter a breakpoint? */
25885 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25886 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025887 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025889 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890 sourcing_lnum);
25891 fcp->dbg_tick = debug_tick;
25892 }
25893
25894 return retval;
25895}
25896
Bram Moolenaar05159a02005-02-26 23:04:13 +000025897#if defined(FEAT_PROFILE) || defined(PROTO)
25898/*
25899 * Called when starting to read a function line.
25900 * "sourcing_lnum" must be correct!
25901 * When skipping lines it may not actually be executed, but we won't find out
25902 * until later and we need to store the time now.
25903 */
25904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025905func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025906{
25907 funccall_T *fcp = (funccall_T *)cookie;
25908 ufunc_T *fp = fcp->func;
25909
25910 if (fp->uf_profiling && sourcing_lnum >= 1
25911 && sourcing_lnum <= fp->uf_lines.ga_len)
25912 {
25913 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025914 /* Skip continuation lines. */
25915 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25916 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025917 fp->uf_tml_execed = FALSE;
25918 profile_start(&fp->uf_tml_start);
25919 profile_zero(&fp->uf_tml_children);
25920 profile_get_wait(&fp->uf_tml_wait);
25921 }
25922}
25923
25924/*
25925 * Called when actually executing a function line.
25926 */
25927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025928func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025929{
25930 funccall_T *fcp = (funccall_T *)cookie;
25931 ufunc_T *fp = fcp->func;
25932
25933 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25934 fp->uf_tml_execed = TRUE;
25935}
25936
25937/*
25938 * Called when done with a function line.
25939 */
25940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025941func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025942{
25943 funccall_T *fcp = (funccall_T *)cookie;
25944 ufunc_T *fp = fcp->func;
25945
25946 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25947 {
25948 if (fp->uf_tml_execed)
25949 {
25950 ++fp->uf_tml_count[fp->uf_tml_idx];
25951 profile_end(&fp->uf_tml_start);
25952 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025953 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025954 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25955 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025956 }
25957 fp->uf_tml_idx = -1;
25958 }
25959}
25960#endif
25961
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962/*
25963 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025964 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965 */
25966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025967func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968{
Bram Moolenaar33570922005-01-25 22:26:29 +000025969 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970
25971 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25972 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025973 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025974 || fcp->returned);
25975}
25976
25977/*
25978 * return TRUE if cookie indicates a function which "abort"s on errors.
25979 */
25980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025981func_has_abort(
25982 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025983{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025984 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985}
25986
25987#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25988typedef enum
25989{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025990 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25991 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25992 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993} var_flavour_T;
25994
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025995static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996
25997 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025998var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999{
26000 char_u *p = varname;
26001
26002 if (ASCII_ISUPPER(*p))
26003 {
26004 while (*(++p))
26005 if (ASCII_ISLOWER(*p))
26006 return VAR_FLAVOUR_SESSION;
26007 return VAR_FLAVOUR_VIMINFO;
26008 }
26009 else
26010 return VAR_FLAVOUR_DEFAULT;
26011}
26012#endif
26013
26014#if defined(FEAT_VIMINFO) || defined(PROTO)
26015/*
26016 * Restore global vars that start with a capital from the viminfo file
26017 */
26018 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026019read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026020{
26021 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026022 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026023 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026024 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025
26026 if (!writing && (find_viminfo_parameter('!') != NULL))
26027 {
26028 tab = vim_strchr(virp->vir_line + 1, '\t');
26029 if (tab != NULL)
26030 {
26031 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026032 switch (*tab)
26033 {
26034 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026035#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026036 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026037#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026038 case 'D': type = VAR_DICT; break;
26039 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026040 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042
26043 tab = vim_strchr(tab, '\t');
26044 if (tab != NULL)
26045 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026046 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026047 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026048 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026050#ifdef FEAT_FLOAT
26051 else if (type == VAR_FLOAT)
26052 (void)string2float(tab + 1, &tv.vval.v_float);
26053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026055 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026056 if (type == VAR_DICT || type == VAR_LIST)
26057 {
26058 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26059
26060 if (etv == NULL)
26061 /* Failed to parse back the dict or list, use it as a
26062 * string. */
26063 tv.v_type = VAR_STRING;
26064 else
26065 {
26066 vim_free(tv.vval.v_string);
26067 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026068 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026069 }
26070 }
26071
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026072 /* when in a function use global variables */
26073 save_funccal = current_funccal;
26074 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026075 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026076 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026077
26078 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026079 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026080 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26081 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026082 }
26083 }
26084 }
26085
26086 return viminfo_readline(virp);
26087}
26088
26089/*
26090 * Write global vars that start with a capital to the viminfo file
26091 */
26092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026093write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094{
Bram Moolenaar33570922005-01-25 22:26:29 +000026095 hashitem_T *hi;
26096 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026097 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026098 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026099 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026100 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026101 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102
26103 if (find_viminfo_parameter('!') == NULL)
26104 return;
26105
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026106 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026108 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026109 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026111 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026113 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026114 this_var = HI2DI(hi);
26115 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026116 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026117 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026118 {
26119 case VAR_STRING: s = "STR"; break;
26120 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026121 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026122 case VAR_DICT: s = "DIC"; break;
26123 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026124 case VAR_SPECIAL: s = "XPL"; break;
26125
26126 case VAR_UNKNOWN:
26127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026128 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026129 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026130 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026131 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026132 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026133 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026134 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026135 if (p != NULL)
26136 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026137 vim_free(tofree);
26138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 }
26140 }
26141}
26142#endif
26143
26144#if defined(FEAT_SESSION) || defined(PROTO)
26145 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026146store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147{
Bram Moolenaar33570922005-01-25 22:26:29 +000026148 hashitem_T *hi;
26149 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026150 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151 char_u *p, *t;
26152
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026153 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026154 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026156 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026158 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026159 this_var = HI2DI(hi);
26160 if ((this_var->di_tv.v_type == VAR_NUMBER
26161 || this_var->di_tv.v_type == VAR_STRING)
26162 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026163 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026164 /* Escape special characters with a backslash. Turn a LF and
26165 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026166 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026167 (char_u *)"\\\"\n\r");
26168 if (p == NULL) /* out of memory */
26169 break;
26170 for (t = p; *t != NUL; ++t)
26171 if (*t == '\n')
26172 *t = 'n';
26173 else if (*t == '\r')
26174 *t = 'r';
26175 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026176 this_var->di_key,
26177 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26178 : ' ',
26179 p,
26180 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26181 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026182 || put_eol(fd) == FAIL)
26183 {
26184 vim_free(p);
26185 return FAIL;
26186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187 vim_free(p);
26188 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026189#ifdef FEAT_FLOAT
26190 else if (this_var->di_tv.v_type == VAR_FLOAT
26191 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26192 {
26193 float_T f = this_var->di_tv.vval.v_float;
26194 int sign = ' ';
26195
26196 if (f < 0)
26197 {
26198 f = -f;
26199 sign = '-';
26200 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026201 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026202 this_var->di_key, sign, f) < 0)
26203 || put_eol(fd) == FAIL)
26204 return FAIL;
26205 }
26206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026207 }
26208 }
26209 return OK;
26210}
26211#endif
26212
Bram Moolenaar661b1822005-07-28 22:36:45 +000026213/*
26214 * Display script name where an item was last set.
26215 * Should only be invoked when 'verbose' is non-zero.
26216 */
26217 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026218last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026219{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026220 char_u *p;
26221
Bram Moolenaar661b1822005-07-28 22:36:45 +000026222 if (scriptID != 0)
26223 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026224 p = home_replace_save(NULL, get_scriptname(scriptID));
26225 if (p != NULL)
26226 {
26227 verbose_enter();
26228 MSG_PUTS(_("\n\tLast set from "));
26229 MSG_PUTS(p);
26230 vim_free(p);
26231 verbose_leave();
26232 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026233 }
26234}
26235
Bram Moolenaard812df62008-11-09 12:46:09 +000026236/*
26237 * List v:oldfiles in a nice way.
26238 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026240ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026241{
26242 list_T *l = vimvars[VV_OLDFILES].vv_list;
26243 listitem_T *li;
26244 int nr = 0;
26245
26246 if (l == NULL)
26247 msg((char_u *)_("No old files"));
26248 else
26249 {
26250 msg_start();
26251 msg_scroll = TRUE;
26252 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26253 {
26254 msg_outnum((long)++nr);
26255 MSG_PUTS(": ");
26256 msg_outtrans(get_tv_string(&li->li_tv));
26257 msg_putchar('\n');
26258 out_flush(); /* output one line at a time */
26259 ui_breakcheck();
26260 }
26261 /* Assume "got_int" was set to truncate the listing. */
26262 got_int = FALSE;
26263
26264#ifdef FEAT_BROWSE_CMD
26265 if (cmdmod.browse)
26266 {
26267 quit_more = FALSE;
26268 nr = prompt_for_number(FALSE);
26269 msg_starthere();
26270 if (nr > 0)
26271 {
26272 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26273 (long)nr);
26274
26275 if (p != NULL)
26276 {
26277 p = expand_env_save(p);
26278 eap->arg = p;
26279 eap->cmdidx = CMD_edit;
26280 cmdmod.browse = FALSE;
26281 do_exedit(eap, NULL);
26282 vim_free(p);
26283 }
26284 }
26285 }
26286#endif
26287 }
26288}
26289
Bram Moolenaar53744302015-07-17 17:38:22 +020026290/* reset v:option_new, v:option_old and v:option_type */
26291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026292reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026293{
26294 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26295 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26296 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26297}
26298
26299
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300#endif /* FEAT_EVAL */
26301
Bram Moolenaar071d4272004-06-13 20:20:40 +000026302
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026303#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026304
26305#ifdef WIN3264
26306/*
26307 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26308 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026309static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26310static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26311static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312
26313/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026314 * Get the short path (8.3) for the filename in "fnamep".
26315 * Only works for a valid file name.
26316 * When the path gets longer "fnamep" is changed and the allocated buffer
26317 * is put in "bufp".
26318 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26319 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026320 */
26321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026322get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026323{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026324 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026325 char_u *newbuf;
26326
26327 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026328 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026329 if (l > len - 1)
26330 {
26331 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026332 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333 newbuf = vim_strnsave(*fnamep, l);
26334 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026335 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026336
26337 vim_free(*bufp);
26338 *fnamep = *bufp = newbuf;
26339
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026340 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026341 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026342 }
26343
26344 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026345 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026346}
26347
26348/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026349 * Get the short path (8.3) for the filename in "fname". The converted
26350 * path is returned in "bufp".
26351 *
26352 * Some of the directories specified in "fname" may not exist. This function
26353 * will shorten the existing directories at the beginning of the path and then
26354 * append the remaining non-existing path.
26355 *
26356 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026357 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026358 * bufp - Pointer to an allocated buffer for the filename.
26359 * fnamelen - Length of the filename pointed to by fname
26360 *
26361 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362 */
26363 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026364shortpath_for_invalid_fname(
26365 char_u **fname,
26366 char_u **bufp,
26367 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026368{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026369 char_u *short_fname, *save_fname, *pbuf_unused;
26370 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026371 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026372 int old_len, len;
26373 int new_len, sfx_len;
26374 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375
26376 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026377 old_len = *fnamelen;
26378 save_fname = vim_strnsave(*fname, old_len);
26379 pbuf_unused = NULL;
26380 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026381
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026382 endp = save_fname + old_len - 1; /* Find the end of the copy */
26383 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026384
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026385 /*
26386 * Try shortening the supplied path till it succeeds by removing one
26387 * directory at a time from the tail of the path.
26388 */
26389 len = 0;
26390 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026391 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026392 /* go back one path-separator */
26393 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26394 --endp;
26395 if (endp <= save_fname)
26396 break; /* processed the complete path */
26397
26398 /*
26399 * Replace the path separator with a NUL and try to shorten the
26400 * resulting path.
26401 */
26402 ch = *endp;
26403 *endp = 0;
26404 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026405 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026406 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26407 {
26408 retval = FAIL;
26409 goto theend;
26410 }
26411 *endp = ch; /* preserve the string */
26412
26413 if (len > 0)
26414 break; /* successfully shortened the path */
26415
26416 /* failed to shorten the path. Skip the path separator */
26417 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 }
26419
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026420 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422 /*
26423 * Succeeded in shortening the path. Now concatenate the shortened
26424 * path with the remaining path at the tail.
26425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026426
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026427 /* Compute the length of the new path. */
26428 sfx_len = (int)(save_endp - endp) + 1;
26429 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026431 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026432 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026435 /* There is not enough space in the currently allocated string,
26436 * copy it to a buffer big enough. */
26437 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026438 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026439 {
26440 retval = FAIL;
26441 goto theend;
26442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026443 }
26444 else
26445 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026446 /* Transfer short_fname to the main buffer (it's big enough),
26447 * unless get_short_pathname() did its work in-place. */
26448 *fname = *bufp = save_fname;
26449 if (short_fname != save_fname)
26450 vim_strncpy(save_fname, short_fname, len);
26451 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026453
26454 /* concat the not-shortened part of the path */
26455 vim_strncpy(*fname + len, endp, sfx_len);
26456 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026458
26459theend:
26460 vim_free(pbuf_unused);
26461 vim_free(save_fname);
26462
26463 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026464}
26465
26466/*
26467 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026468 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469 */
26470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026471shortpath_for_partial(
26472 char_u **fnamep,
26473 char_u **bufp,
26474 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026475{
26476 int sepcount, len, tflen;
26477 char_u *p;
26478 char_u *pbuf, *tfname;
26479 int hasTilde;
26480
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026481 /* Count up the path separators from the RHS.. so we know which part
26482 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026484 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026485 if (vim_ispathsep(*p))
26486 ++sepcount;
26487
26488 /* Need full path first (use expand_env() to remove a "~/") */
26489 hasTilde = (**fnamep == '~');
26490 if (hasTilde)
26491 pbuf = tfname = expand_env_save(*fnamep);
26492 else
26493 pbuf = tfname = FullName_save(*fnamep, FALSE);
26494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026495 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026496
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026497 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026499
26500 if (len == 0)
26501 {
26502 /* Don't have a valid filename, so shorten the rest of the
26503 * path if we can. This CAN give us invalid 8.3 filenames, but
26504 * there's not a lot of point in guessing what it might be.
26505 */
26506 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026507 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26508 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509 }
26510
26511 /* Count the paths backward to find the beginning of the desired string. */
26512 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026513 {
26514#ifdef FEAT_MBYTE
26515 if (has_mbyte)
26516 p -= mb_head_off(tfname, p);
26517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518 if (vim_ispathsep(*p))
26519 {
26520 if (sepcount == 0 || (hasTilde && sepcount == 1))
26521 break;
26522 else
26523 sepcount --;
26524 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526 if (hasTilde)
26527 {
26528 --p;
26529 if (p >= tfname)
26530 *p = '~';
26531 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026532 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026533 }
26534 else
26535 ++p;
26536
26537 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26538 vim_free(*bufp);
26539 *fnamelen = (int)STRLEN(p);
26540 *bufp = pbuf;
26541 *fnamep = p;
26542
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026543 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026544}
26545#endif /* WIN3264 */
26546
26547/*
26548 * Adjust a filename, according to a string of modifiers.
26549 * *fnamep must be NUL terminated when called. When returning, the length is
26550 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026551 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026552 * When there is an error, *fnamep is set to NULL.
26553 */
26554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026555modify_fname(
26556 char_u *src, /* string with modifiers */
26557 int *usedlen, /* characters after src that are used */
26558 char_u **fnamep, /* file name so far */
26559 char_u **bufp, /* buffer for allocated file name or NULL */
26560 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561{
26562 int valid = 0;
26563 char_u *tail;
26564 char_u *s, *p, *pbuf;
26565 char_u dirname[MAXPATHL];
26566 int c;
26567 int has_fullname = 0;
26568#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026569 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026570 int has_shortname = 0;
26571#endif
26572
26573repeat:
26574 /* ":p" - full path/file_name */
26575 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26576 {
26577 has_fullname = 1;
26578
26579 valid |= VALID_PATH;
26580 *usedlen += 2;
26581
26582 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26583 if ((*fnamep)[0] == '~'
26584#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26585 && ((*fnamep)[1] == '/'
26586# ifdef BACKSLASH_IN_FILENAME
26587 || (*fnamep)[1] == '\\'
26588# endif
26589 || (*fnamep)[1] == NUL)
26590
26591#endif
26592 )
26593 {
26594 *fnamep = expand_env_save(*fnamep);
26595 vim_free(*bufp); /* free any allocated file name */
26596 *bufp = *fnamep;
26597 if (*fnamep == NULL)
26598 return -1;
26599 }
26600
26601 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026602 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026603 {
26604 if (vim_ispathsep(*p)
26605 && p[1] == '.'
26606 && (p[2] == NUL
26607 || vim_ispathsep(p[2])
26608 || (p[2] == '.'
26609 && (p[3] == NUL || vim_ispathsep(p[3])))))
26610 break;
26611 }
26612
26613 /* FullName_save() is slow, don't use it when not needed. */
26614 if (*p != NUL || !vim_isAbsName(*fnamep))
26615 {
26616 *fnamep = FullName_save(*fnamep, *p != NUL);
26617 vim_free(*bufp); /* free any allocated file name */
26618 *bufp = *fnamep;
26619 if (*fnamep == NULL)
26620 return -1;
26621 }
26622
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026623#ifdef WIN3264
26624# if _WIN32_WINNT >= 0x0500
26625 if (vim_strchr(*fnamep, '~') != NULL)
26626 {
26627 /* Expand 8.3 filename to full path. Needed to make sure the same
26628 * file does not have two different names.
26629 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26630 p = alloc(_MAX_PATH + 1);
26631 if (p != NULL)
26632 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026633 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026634 {
26635 vim_free(*bufp);
26636 *bufp = *fnamep = p;
26637 }
26638 else
26639 vim_free(p);
26640 }
26641 }
26642# endif
26643#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026644 /* Append a path separator to a directory. */
26645 if (mch_isdir(*fnamep))
26646 {
26647 /* Make room for one or two extra characters. */
26648 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26649 vim_free(*bufp); /* free any allocated file name */
26650 *bufp = *fnamep;
26651 if (*fnamep == NULL)
26652 return -1;
26653 add_pathsep(*fnamep);
26654 }
26655 }
26656
26657 /* ":." - path relative to the current directory */
26658 /* ":~" - path relative to the home directory */
26659 /* ":8" - shortname path - postponed till after */
26660 while (src[*usedlen] == ':'
26661 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26662 {
26663 *usedlen += 2;
26664 if (c == '8')
26665 {
26666#ifdef WIN3264
26667 has_shortname = 1; /* Postpone this. */
26668#endif
26669 continue;
26670 }
26671 pbuf = NULL;
26672 /* Need full path first (use expand_env() to remove a "~/") */
26673 if (!has_fullname)
26674 {
26675 if (c == '.' && **fnamep == '~')
26676 p = pbuf = expand_env_save(*fnamep);
26677 else
26678 p = pbuf = FullName_save(*fnamep, FALSE);
26679 }
26680 else
26681 p = *fnamep;
26682
26683 has_fullname = 0;
26684
26685 if (p != NULL)
26686 {
26687 if (c == '.')
26688 {
26689 mch_dirname(dirname, MAXPATHL);
26690 s = shorten_fname(p, dirname);
26691 if (s != NULL)
26692 {
26693 *fnamep = s;
26694 if (pbuf != NULL)
26695 {
26696 vim_free(*bufp); /* free any allocated file name */
26697 *bufp = pbuf;
26698 pbuf = NULL;
26699 }
26700 }
26701 }
26702 else
26703 {
26704 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26705 /* Only replace it when it starts with '~' */
26706 if (*dirname == '~')
26707 {
26708 s = vim_strsave(dirname);
26709 if (s != NULL)
26710 {
26711 *fnamep = s;
26712 vim_free(*bufp);
26713 *bufp = s;
26714 }
26715 }
26716 }
26717 vim_free(pbuf);
26718 }
26719 }
26720
26721 tail = gettail(*fnamep);
26722 *fnamelen = (int)STRLEN(*fnamep);
26723
26724 /* ":h" - head, remove "/file_name", can be repeated */
26725 /* Don't remove the first "/" or "c:\" */
26726 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26727 {
26728 valid |= VALID_HEAD;
26729 *usedlen += 2;
26730 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026731 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026732 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026733 *fnamelen = (int)(tail - *fnamep);
26734#ifdef VMS
26735 if (*fnamelen > 0)
26736 *fnamelen += 1; /* the path separator is part of the path */
26737#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026738 if (*fnamelen == 0)
26739 {
26740 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26741 p = vim_strsave((char_u *)".");
26742 if (p == NULL)
26743 return -1;
26744 vim_free(*bufp);
26745 *bufp = *fnamep = tail = p;
26746 *fnamelen = 1;
26747 }
26748 else
26749 {
26750 while (tail > s && !after_pathsep(s, tail))
26751 mb_ptr_back(*fnamep, tail);
26752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026753 }
26754
26755 /* ":8" - shortname */
26756 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26757 {
26758 *usedlen += 2;
26759#ifdef WIN3264
26760 has_shortname = 1;
26761#endif
26762 }
26763
26764#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026765 /*
26766 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026767 */
26768 if (has_shortname)
26769 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026770 /* Copy the string if it is shortened by :h and when it wasn't copied
26771 * yet, because we are going to change it in place. Avoids changing
26772 * the buffer name for "%:8". */
26773 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026774 {
26775 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026776 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026777 return -1;
26778 vim_free(*bufp);
26779 *bufp = *fnamep = p;
26780 }
26781
26782 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026783 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026784 if (!has_fullname && !vim_isAbsName(*fnamep))
26785 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026786 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026787 return -1;
26788 }
26789 else
26790 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026791 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026792
Bram Moolenaardc935552011-08-17 15:23:23 +020026793 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026794 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026795 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026796 return -1;
26797
26798 if (l == 0)
26799 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026800 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026802 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026803 return -1;
26804 }
26805 *fnamelen = l;
26806 }
26807 }
26808#endif /* WIN3264 */
26809
26810 /* ":t" - tail, just the basename */
26811 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26812 {
26813 *usedlen += 2;
26814 *fnamelen -= (int)(tail - *fnamep);
26815 *fnamep = tail;
26816 }
26817
26818 /* ":e" - extension, can be repeated */
26819 /* ":r" - root, without extension, can be repeated */
26820 while (src[*usedlen] == ':'
26821 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26822 {
26823 /* find a '.' in the tail:
26824 * - for second :e: before the current fname
26825 * - otherwise: The last '.'
26826 */
26827 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26828 s = *fnamep - 2;
26829 else
26830 s = *fnamep + *fnamelen - 1;
26831 for ( ; s > tail; --s)
26832 if (s[0] == '.')
26833 break;
26834 if (src[*usedlen + 1] == 'e') /* :e */
26835 {
26836 if (s > tail)
26837 {
26838 *fnamelen += (int)(*fnamep - (s + 1));
26839 *fnamep = s + 1;
26840#ifdef VMS
26841 /* cut version from the extension */
26842 s = *fnamep + *fnamelen - 1;
26843 for ( ; s > *fnamep; --s)
26844 if (s[0] == ';')
26845 break;
26846 if (s > *fnamep)
26847 *fnamelen = s - *fnamep;
26848#endif
26849 }
26850 else if (*fnamep <= tail)
26851 *fnamelen = 0;
26852 }
26853 else /* :r */
26854 {
26855 if (s > tail) /* remove one extension */
26856 *fnamelen = (int)(s - *fnamep);
26857 }
26858 *usedlen += 2;
26859 }
26860
26861 /* ":s?pat?foo?" - substitute */
26862 /* ":gs?pat?foo?" - global substitute */
26863 if (src[*usedlen] == ':'
26864 && (src[*usedlen + 1] == 's'
26865 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26866 {
26867 char_u *str;
26868 char_u *pat;
26869 char_u *sub;
26870 int sep;
26871 char_u *flags;
26872 int didit = FALSE;
26873
26874 flags = (char_u *)"";
26875 s = src + *usedlen + 2;
26876 if (src[*usedlen + 1] == 'g')
26877 {
26878 flags = (char_u *)"g";
26879 ++s;
26880 }
26881
26882 sep = *s++;
26883 if (sep)
26884 {
26885 /* find end of pattern */
26886 p = vim_strchr(s, sep);
26887 if (p != NULL)
26888 {
26889 pat = vim_strnsave(s, (int)(p - s));
26890 if (pat != NULL)
26891 {
26892 s = p + 1;
26893 /* find end of substitution */
26894 p = vim_strchr(s, sep);
26895 if (p != NULL)
26896 {
26897 sub = vim_strnsave(s, (int)(p - s));
26898 str = vim_strnsave(*fnamep, *fnamelen);
26899 if (sub != NULL && str != NULL)
26900 {
26901 *usedlen = (int)(p + 1 - src);
26902 s = do_string_sub(str, pat, sub, flags);
26903 if (s != NULL)
26904 {
26905 *fnamep = s;
26906 *fnamelen = (int)STRLEN(s);
26907 vim_free(*bufp);
26908 *bufp = s;
26909 didit = TRUE;
26910 }
26911 }
26912 vim_free(sub);
26913 vim_free(str);
26914 }
26915 vim_free(pat);
26916 }
26917 }
26918 /* after using ":s", repeat all the modifiers */
26919 if (didit)
26920 goto repeat;
26921 }
26922 }
26923
Bram Moolenaar26df0922014-02-23 23:39:13 +010026924 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26925 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026926 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026927 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026928 if (c != NUL)
26929 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026930 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026931 if (c != NUL)
26932 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026933 if (p == NULL)
26934 return -1;
26935 vim_free(*bufp);
26936 *bufp = *fnamep = p;
26937 *fnamelen = (int)STRLEN(p);
26938 *usedlen += 2;
26939 }
26940
Bram Moolenaar071d4272004-06-13 20:20:40 +000026941 return valid;
26942}
26943
26944/*
26945 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26946 * "flags" can be "g" to do a global substitute.
26947 * Returns an allocated string, NULL for error.
26948 */
26949 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026950do_string_sub(
26951 char_u *str,
26952 char_u *pat,
26953 char_u *sub,
26954 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026955{
26956 int sublen;
26957 regmatch_T regmatch;
26958 int i;
26959 int do_all;
26960 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026961 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026962 garray_T ga;
26963 char_u *ret;
26964 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026965 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026966
26967 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26968 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026969 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026970
26971 ga_init2(&ga, 1, 200);
26972
26973 do_all = (flags[0] == 'g');
26974
26975 regmatch.rm_ic = p_ic;
26976 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26977 if (regmatch.regprog != NULL)
26978 {
26979 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026980 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026981 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26982 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026983 /* Skip empty match except for first match. */
26984 if (regmatch.startp[0] == regmatch.endp[0])
26985 {
26986 if (zero_width == regmatch.startp[0])
26987 {
26988 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026989 i = MB_PTR2LEN(tail);
26990 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26991 (size_t)i);
26992 ga.ga_len += i;
26993 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026994 continue;
26995 }
26996 zero_width = regmatch.startp[0];
26997 }
26998
Bram Moolenaar071d4272004-06-13 20:20:40 +000026999 /*
27000 * Get some space for a temporary buffer to do the substitution
27001 * into. It will contain:
27002 * - The text up to where the match is.
27003 * - The substituted text.
27004 * - The text after the match.
27005 */
27006 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027007 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027008 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27009 {
27010 ga_clear(&ga);
27011 break;
27012 }
27013
27014 /* copy the text up to where the match is */
27015 i = (int)(regmatch.startp[0] - tail);
27016 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27017 /* add the substituted text */
27018 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27019 + ga.ga_len + i, TRUE, TRUE, FALSE);
27020 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027021 tail = regmatch.endp[0];
27022 if (*tail == NUL)
27023 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027024 if (!do_all)
27025 break;
27026 }
27027
27028 if (ga.ga_data != NULL)
27029 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27030
Bram Moolenaar473de612013-06-08 18:19:48 +020027031 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027032 }
27033
27034 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27035 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027036 if (p_cpo == empty_option)
27037 p_cpo = save_cpo;
27038 else
27039 /* Darn, evaluating {sub} expression changed the value. */
27040 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027041
27042 return ret;
27043}
27044
27045#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */