blob: a653b89f7227885c7cd67c3a9284ba8a6d23658e [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);
586static void f_get(typval_T *argvars, typval_T *rettv);
587static void f_getbufline(typval_T *argvars, typval_T *rettv);
588static void f_getbufvar(typval_T *argvars, typval_T *rettv);
589static void f_getchar(typval_T *argvars, typval_T *rettv);
590static void f_getcharmod(typval_T *argvars, typval_T *rettv);
591static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
592static void f_getcmdline(typval_T *argvars, typval_T *rettv);
593static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
594static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
595static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
596static void f_getcwd(typval_T *argvars, typval_T *rettv);
597static void f_getfontname(typval_T *argvars, typval_T *rettv);
598static void f_getfperm(typval_T *argvars, typval_T *rettv);
599static void f_getfsize(typval_T *argvars, typval_T *rettv);
600static void f_getftime(typval_T *argvars, typval_T *rettv);
601static void f_getftype(typval_T *argvars, typval_T *rettv);
602static void f_getline(typval_T *argvars, typval_T *rettv);
603static void f_getmatches(typval_T *argvars, typval_T *rettv);
604static void f_getpid(typval_T *argvars, typval_T *rettv);
605static void f_getcurpos(typval_T *argvars, typval_T *rettv);
606static void f_getpos(typval_T *argvars, typval_T *rettv);
607static void f_getqflist(typval_T *argvars, typval_T *rettv);
608static void f_getreg(typval_T *argvars, typval_T *rettv);
609static void f_getregtype(typval_T *argvars, typval_T *rettv);
610static void f_gettabvar(typval_T *argvars, typval_T *rettv);
611static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
612static void f_getwinposx(typval_T *argvars, typval_T *rettv);
613static void f_getwinposy(typval_T *argvars, typval_T *rettv);
614static void f_getwinvar(typval_T *argvars, typval_T *rettv);
615static void f_glob(typval_T *argvars, typval_T *rettv);
616static void f_globpath(typval_T *argvars, typval_T *rettv);
617static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
618static void f_has(typval_T *argvars, typval_T *rettv);
619static void f_has_key(typval_T *argvars, typval_T *rettv);
620static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
621static void f_hasmapto(typval_T *argvars, typval_T *rettv);
622static void f_histadd(typval_T *argvars, typval_T *rettv);
623static void f_histdel(typval_T *argvars, typval_T *rettv);
624static void f_histget(typval_T *argvars, typval_T *rettv);
625static void f_histnr(typval_T *argvars, typval_T *rettv);
626static void f_hlID(typval_T *argvars, typval_T *rettv);
627static void f_hlexists(typval_T *argvars, typval_T *rettv);
628static void f_hostname(typval_T *argvars, typval_T *rettv);
629static void f_iconv(typval_T *argvars, typval_T *rettv);
630static void f_indent(typval_T *argvars, typval_T *rettv);
631static void f_index(typval_T *argvars, typval_T *rettv);
632static void f_input(typval_T *argvars, typval_T *rettv);
633static void f_inputdialog(typval_T *argvars, typval_T *rettv);
634static void f_inputlist(typval_T *argvars, typval_T *rettv);
635static void f_inputrestore(typval_T *argvars, typval_T *rettv);
636static void f_inputsave(typval_T *argvars, typval_T *rettv);
637static void f_inputsecret(typval_T *argvars, typval_T *rettv);
638static void f_insert(typval_T *argvars, typval_T *rettv);
639static void f_invert(typval_T *argvars, typval_T *rettv);
640static void f_isdirectory(typval_T *argvars, typval_T *rettv);
641static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100642#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
643static void f_isnan(typval_T *argvars, typval_T *rettv);
644#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100646#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100647static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100648static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100649static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100650static void f_job_start(typval_T *argvars, typval_T *rettv);
651static void f_job_stop(typval_T *argvars, typval_T *rettv);
652static void f_job_status(typval_T *argvars, typval_T *rettv);
653#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100654static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100655static void f_js_decode(typval_T *argvars, typval_T *rettv);
656static void f_js_encode(typval_T *argvars, typval_T *rettv);
657static void f_json_decode(typval_T *argvars, typval_T *rettv);
658static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_keys(typval_T *argvars, typval_T *rettv);
660static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
661static void f_len(typval_T *argvars, typval_T *rettv);
662static void f_libcall(typval_T *argvars, typval_T *rettv);
663static void f_libcallnr(typval_T *argvars, typval_T *rettv);
664static void f_line(typval_T *argvars, typval_T *rettv);
665static void f_line2byte(typval_T *argvars, typval_T *rettv);
666static void f_lispindent(typval_T *argvars, typval_T *rettv);
667static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000668#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_log(typval_T *argvars, typval_T *rettv);
670static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000671#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200674#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_map(typval_T *argvars, typval_T *rettv);
676static void f_maparg(typval_T *argvars, typval_T *rettv);
677static void f_mapcheck(typval_T *argvars, typval_T *rettv);
678static void f_match(typval_T *argvars, typval_T *rettv);
679static void f_matchadd(typval_T *argvars, typval_T *rettv);
680static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
681static void f_matcharg(typval_T *argvars, typval_T *rettv);
682static void f_matchdelete(typval_T *argvars, typval_T *rettv);
683static void f_matchend(typval_T *argvars, typval_T *rettv);
684static void f_matchlist(typval_T *argvars, typval_T *rettv);
685static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200686static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_max(typval_T *argvars, typval_T *rettv);
688static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000689#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000691#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100693#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
697static void f_nr2char(typval_T *argvars, typval_T *rettv);
698static void f_or(typval_T *argvars, typval_T *rettv);
699static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100700#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100702#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000703#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
707static void f_printf(typval_T *argvars, typval_T *rettv);
708static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#endif
712#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100713static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200714#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100715static void f_range(typval_T *argvars, typval_T *rettv);
716static void f_readfile(typval_T *argvars, typval_T *rettv);
717static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100718#ifdef FEAT_FLOAT
719static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
720#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_reltimestr(typval_T *argvars, typval_T *rettv);
722static void f_remote_expr(typval_T *argvars, typval_T *rettv);
723static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
724static void f_remote_peek(typval_T *argvars, typval_T *rettv);
725static void f_remote_read(typval_T *argvars, typval_T *rettv);
726static void f_remote_send(typval_T *argvars, typval_T *rettv);
727static void f_remove(typval_T *argvars, typval_T *rettv);
728static void f_rename(typval_T *argvars, typval_T *rettv);
729static void f_repeat(typval_T *argvars, typval_T *rettv);
730static void f_resolve(typval_T *argvars, typval_T *rettv);
731static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000732#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100733static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000734#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_screenattr(typval_T *argvars, typval_T *rettv);
736static void f_screenchar(typval_T *argvars, typval_T *rettv);
737static void f_screencol(typval_T *argvars, typval_T *rettv);
738static void f_screenrow(typval_T *argvars, typval_T *rettv);
739static void f_search(typval_T *argvars, typval_T *rettv);
740static void f_searchdecl(typval_T *argvars, typval_T *rettv);
741static void f_searchpair(typval_T *argvars, typval_T *rettv);
742static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
743static void f_searchpos(typval_T *argvars, typval_T *rettv);
744static void f_server2client(typval_T *argvars, typval_T *rettv);
745static void f_serverlist(typval_T *argvars, typval_T *rettv);
746static void f_setbufvar(typval_T *argvars, typval_T *rettv);
747static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
748static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100749static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_setline(typval_T *argvars, typval_T *rettv);
751static void f_setloclist(typval_T *argvars, typval_T *rettv);
752static void f_setmatches(typval_T *argvars, typval_T *rettv);
753static void f_setpos(typval_T *argvars, typval_T *rettv);
754static void f_setqflist(typval_T *argvars, typval_T *rettv);
755static void f_setreg(typval_T *argvars, typval_T *rettv);
756static void f_settabvar(typval_T *argvars, typval_T *rettv);
757static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
758static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100759#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100761#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_shellescape(typval_T *argvars, typval_T *rettv);
763static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
764static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_sin(typval_T *argvars, typval_T *rettv);
767static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sort(typval_T *argvars, typval_T *rettv);
770static void f_soundfold(typval_T *argvars, typval_T *rettv);
771static void f_spellbadword(typval_T *argvars, typval_T *rettv);
772static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
773static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000774#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_sqrt(typval_T *argvars, typval_T *rettv);
776static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000777#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100778static void f_str2nr(typval_T *argvars, typval_T *rettv);
779static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000780#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100781static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000782#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200783static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100784static void f_stridx(typval_T *argvars, typval_T *rettv);
785static void f_string(typval_T *argvars, typval_T *rettv);
786static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200787static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100788static void f_strpart(typval_T *argvars, typval_T *rettv);
789static void f_strridx(typval_T *argvars, typval_T *rettv);
790static void f_strtrans(typval_T *argvars, typval_T *rettv);
791static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
792static void f_strwidth(typval_T *argvars, typval_T *rettv);
793static void f_submatch(typval_T *argvars, typval_T *rettv);
794static void f_substitute(typval_T *argvars, typval_T *rettv);
795static void f_synID(typval_T *argvars, typval_T *rettv);
796static void f_synIDattr(typval_T *argvars, typval_T *rettv);
797static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
798static void f_synstack(typval_T *argvars, typval_T *rettv);
799static void f_synconcealed(typval_T *argvars, typval_T *rettv);
800static void f_system(typval_T *argvars, typval_T *rettv);
801static void f_systemlist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
804static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
805static void f_taglist(typval_T *argvars, typval_T *rettv);
806static void f_tagfiles(typval_T *argvars, typval_T *rettv);
807static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200808static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
809#ifdef FEAT_JOB_CHANNEL
810static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
811#endif
812static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
813#ifdef FEAT_JOB_CHANNEL
814static void f_test_null_job(typval_T *argvars, typval_T *rettv);
815#endif
816static void f_test_null_list(typval_T *argvars, typval_T *rettv);
817static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
818static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200819#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100820static void f_tan(typval_T *argvars, typval_T *rettv);
821static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200822#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100823#ifdef FEAT_TIMERS
824static void f_timer_start(typval_T *argvars, typval_T *rettv);
825static void f_timer_stop(typval_T *argvars, typval_T *rettv);
826#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100827static void f_tolower(typval_T *argvars, typval_T *rettv);
828static void f_toupper(typval_T *argvars, typval_T *rettv);
829static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000830#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100831static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000832#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100833static void f_type(typval_T *argvars, typval_T *rettv);
834static void f_undofile(typval_T *argvars, typval_T *rettv);
835static void f_undotree(typval_T *argvars, typval_T *rettv);
836static void f_uniq(typval_T *argvars, typval_T *rettv);
837static void f_values(typval_T *argvars, typval_T *rettv);
838static void f_virtcol(typval_T *argvars, typval_T *rettv);
839static void f_visualmode(typval_T *argvars, typval_T *rettv);
840static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100841static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100842static void f_win_getid(typval_T *argvars, typval_T *rettv);
843static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
844static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
845static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static void f_winbufnr(typval_T *argvars, typval_T *rettv);
847static void f_wincol(typval_T *argvars, typval_T *rettv);
848static void f_winheight(typval_T *argvars, typval_T *rettv);
849static void f_winline(typval_T *argvars, typval_T *rettv);
850static void f_winnr(typval_T *argvars, typval_T *rettv);
851static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
852static void f_winrestview(typval_T *argvars, typval_T *rettv);
853static void f_winsaveview(typval_T *argvars, typval_T *rettv);
854static void f_winwidth(typval_T *argvars, typval_T *rettv);
855static void f_writefile(typval_T *argvars, typval_T *rettv);
856static void f_wordcount(typval_T *argvars, typval_T *rettv);
857static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000858
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
860static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
861static int get_env_len(char_u **arg);
862static int get_id_len(char_u **arg);
863static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
864static 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 +0000865#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
866#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
867 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100868static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
869static int eval_isnamec(int c);
870static int eval_isnamec1(int c);
871static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
872static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static typval_T *alloc_string_tv(char_u *string);
874static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100875#ifdef FEAT_FLOAT
876static float_T get_tv_float(typval_T *varp);
877#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100878static linenr_T get_tv_lnum(typval_T *argvars);
879static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
881static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
882static hashtab_T *find_var_ht(char_u *name, char_u **varname);
883static funccall_T *get_funccal(void);
884static void vars_clear_ext(hashtab_T *ht, int free_val);
885static void delete_var(hashtab_T *ht, hashitem_T *hi);
886static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
887static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
888static void set_var(char_u *name, typval_T *varp, int copy);
889static int var_check_ro(int flags, char_u *name, int use_gettext);
890static int var_check_fixed(int flags, char_u *name, int use_gettext);
891static int var_check_func_name(char_u *name, int new_var);
892static int valid_varname(char_u *varname);
893static int tv_check_lock(int lock, char_u *name, int use_gettext);
894static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
895static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100896static 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 +0100897static int eval_fname_script(char_u *p);
898static int eval_fname_sid(char_u *p);
899static void list_func_head(ufunc_T *fp, int indent);
900static ufunc_T *find_func(char_u *name);
901static int function_exists(char_u *name);
902static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000903#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100904static void func_do_profile(ufunc_T *fp);
905static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
906static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000907static int
908# ifdef __BORLANDC__
909 _RTLENTRYF
910# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100911 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000912static int
913# ifdef __BORLANDC__
914 _RTLENTRYF
915# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100916 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000917#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100918static int script_autoload(char_u *name, int reload);
919static char_u *autoload_name(char_u *name);
920static void cat_func_name(char_u *buf, ufunc_T *fp);
921static void func_free(ufunc_T *fp);
922static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
923static int can_free_funccal(funccall_T *fc, int copyID) ;
924static void free_funccal(funccall_T *fc, int free_val);
925static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
926static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
927static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
928static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
929static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
930static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
931static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
932static int write_list(FILE *fd, list_T *list, int binary);
933static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000934
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200935
936#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100937static int compare_func_name(const void *s1, const void *s2);
938static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200939#endif
940
Bram Moolenaar33570922005-01-25 22:26:29 +0000941/*
942 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000943 */
944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100945eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000946{
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 int i;
948 struct vimvar *p;
949
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200950 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
951 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200952 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000953 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000954 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000955
956 for (i = 0; i < VV_LEN; ++i)
957 {
958 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200959 if (STRLEN(p->vv_name) > 16)
960 {
961 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
962 getout(1);
963 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000964 STRCPY(p->vv_di.di_key, p->vv_name);
965 if (p->vv_flags & VV_RO)
966 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
967 else if (p->vv_flags & VV_RO_SBX)
968 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
969 else
970 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000971
972 /* add to v: scope dict, unless the value is not always available */
973 if (p->vv_type != VAR_UNKNOWN)
974 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000976 /* add to compat scope dict */
977 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000978 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100979 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
980
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000981 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100982 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200983 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100984 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100985
986 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
987 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
988 set_vim_var_nr(VV_NONE, VVAL_NONE);
989 set_vim_var_nr(VV_NULL, VVAL_NULL);
990
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200991 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200992
993#ifdef EBCDIC
994 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100995 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200996 */
997 sortFunctions();
998#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000999}
1000
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001#if defined(EXITFREE) || defined(PROTO)
1002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001003eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001004{
1005 int i;
1006 struct vimvar *p;
1007
1008 for (i = 0; i < VV_LEN; ++i)
1009 {
1010 p = &vimvars[i];
1011 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001012 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001013 vim_free(p->vv_str);
1014 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001015 }
1016 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1017 {
1018 list_unref(p->vv_list);
1019 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001020 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021 }
1022 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001023 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024 hash_clear(&compat_hashtab);
1025
Bram Moolenaard9fba312005-06-26 22:34:35 +00001026 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001027# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001028 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001029# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030
1031 /* global variables */
1032 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001033
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001034 /* autoloaded script names */
1035 ga_clear_strings(&ga_loaded);
1036
Bram Moolenaarcca74132013-09-25 21:00:28 +02001037 /* Script-local variables. First clear all the variables and in a second
1038 * loop free the scriptvar_T, because a variable in one script might hold
1039 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001040 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001042 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001043 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 ga_clear(&ga_scripts);
1045
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001046 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001047 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001048
1049 /* functions */
1050 free_all_functions();
1051 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001052}
1053#endif
1054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 * Return the name of the executed function.
1057 */
1058 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001061 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the next breakpoint line for a funccall cookie.
1066 */
1067 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001068func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the address holding the debug tick for a funccall cookie.
1075 */
1076 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/*
1083 * Return the nesting level for a funccall cookie.
1084 */
1085 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089}
1090
1091/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001092funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001094/* pointer to list of previously used funccal, still around because some
1095 * item in it is still being used. */
1096funccall_T *previous_funccal = NULL;
1097
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098/*
1099 * Return TRUE when a function was ended by a ":return" command.
1100 */
1101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001102current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 return current_funccal->returned;
1105}
1106
1107
1108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 * Set an internal variable to a string value. Creates the variable if it does
1110 * not already exist.
1111 */
1112 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001113set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117
1118 val = vim_strsave(value);
1119 if (val != NULL)
1120 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001122 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001124 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 }
1127 }
1128}
1129
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001130static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132static char_u *redir_endp = NULL;
1133static char_u *redir_varname = NULL;
1134
1135/*
1136 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001137 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 * Returns OK if successfully completed the setup. FAIL otherwise.
1139 */
1140 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001141var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142{
1143 int save_emsg;
1144 int err;
1145 typval_T tv;
1146
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001147 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001148 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 {
1150 EMSG(_(e_invarg));
1151 return FAIL;
1152 }
1153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001155 redir_varname = vim_strsave(name);
1156 if (redir_varname == NULL)
1157 return FAIL;
1158
1159 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1160 if (redir_lval == NULL)
1161 {
1162 var_redir_stop();
1163 return FAIL;
1164 }
1165
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001166 /* The output is stored in growarray "redir_ga" until redirection ends. */
1167 ga_init2(&redir_ga, (int)sizeof(char), 500);
1168
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001170 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001171 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1173 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001174 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175 if (redir_endp != NULL && *redir_endp != NUL)
1176 /* Trailing characters are present after the variable name */
1177 EMSG(_(e_trailing));
1178 else
1179 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001180 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 var_redir_stop();
1182 return FAIL;
1183 }
1184
1185 /* check if we can write to the variable: set it to or append an empty
1186 * string */
1187 save_emsg = did_emsg;
1188 did_emsg = FALSE;
1189 tv.v_type = VAR_STRING;
1190 tv.vval.v_string = (char_u *)"";
1191 if (append)
1192 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1193 else
1194 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001195 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001196 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001197 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 if (err)
1199 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001200 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
1202 return FAIL;
1203 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204
1205 return OK;
1206}
1207
1208/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 * Append "value[value_len]" to the variable set by var_redir_start().
1210 * The actual appending is postponed until redirection ends, because the value
1211 * appended may in fact be the string we write to, changing it may cause freed
1212 * memory to be used:
1213 * :redir => foo
1214 * :let foo
1215 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 */
1217 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001218var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001220 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221
1222 if (redir_lval == NULL)
1223 return;
1224
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001226 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001228 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001229
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001230 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001231 {
1232 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001233 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001234 }
1235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237}
1238
1239/*
1240 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 */
1243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001244var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001245{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001246 typval_T tv;
1247
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001248 if (redir_lval != NULL)
1249 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001250 /* If there was no error: assign the text to the variable. */
1251 if (redir_endp != NULL)
1252 {
1253 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1254 tv.v_type = VAR_STRING;
1255 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001256 /* Call get_lval() again, if it's inside a Dict or List it may
1257 * have changed. */
1258 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001259 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001260 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1261 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1262 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001263 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001264
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001265 /* free the collected output */
1266 vim_free(redir_ga.ga_data);
1267 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001269 vim_free(redir_lval);
1270 redir_lval = NULL;
1271 }
1272 vim_free(redir_varname);
1273 redir_varname = NULL;
1274}
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276# if defined(FEAT_MBYTE) || defined(PROTO)
1277 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001278eval_charconvert(
1279 char_u *enc_from,
1280 char_u *enc_to,
1281 char_u *fname_from,
1282 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283{
1284 int err = FALSE;
1285
1286 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1287 set_vim_var_string(VV_CC_TO, enc_to, -1);
1288 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1289 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1290 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1291 err = TRUE;
1292 set_vim_var_string(VV_CC_FROM, NULL, -1);
1293 set_vim_var_string(VV_CC_TO, NULL, -1);
1294 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1295 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1296
1297 if (err)
1298 return FAIL;
1299 return OK;
1300}
1301# endif
1302
1303# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306{
1307 int err = FALSE;
1308
1309 set_vim_var_string(VV_FNAME_IN, fname, -1);
1310 set_vim_var_string(VV_CMDARG, args, -1);
1311 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1312 err = TRUE;
1313 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1314 set_vim_var_string(VV_CMDARG, NULL, -1);
1315
1316 if (err)
1317 {
1318 mch_remove(fname);
1319 return FAIL;
1320 }
1321 return OK;
1322}
1323# endif
1324
1325# if defined(FEAT_DIFF) || defined(PROTO)
1326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001327eval_diff(
1328 char_u *origfile,
1329 char_u *newfile,
1330 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331{
1332 int err = FALSE;
1333
1334 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1335 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1336 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1337 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1338 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1339 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1340 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1341}
1342
1343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001344eval_patch(
1345 char_u *origfile,
1346 char_u *difffile,
1347 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348{
1349 int err;
1350
1351 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1352 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1353 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1354 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1356 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1357 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1358}
1359# endif
1360
1361/*
1362 * Top level evaluation function, returning a boolean.
1363 * Sets "error" to TRUE if there was an error.
1364 * Return TRUE or FALSE.
1365 */
1366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001367eval_to_bool(
1368 char_u *arg,
1369 int *error,
1370 char_u **nextcmd,
1371 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
Bram Moolenaar33570922005-01-25 22:26:29 +00001373 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 int retval = FALSE;
1375
1376 if (skip)
1377 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001378 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
1381 {
1382 *error = FALSE;
1383 if (!skip)
1384 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001385 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
1396 * Top level evaluation function, returning a string. If "skip" is TRUE,
1397 * only parsing to "nextcmd" is done, without reporting errors. Return
1398 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1399 */
1400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401eval_to_string_skip(
1402 char_u *arg,
1403 char_u **nextcmd,
1404 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaar33570922005-01-25 22:26:29 +00001406 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 char_u *retval;
1408
1409 if (skip)
1410 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001411 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 retval = NULL;
1413 else
1414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001415 retval = vim_strsave(get_tv_string(&tv));
1416 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418 if (skip)
1419 --emsg_skip;
1420
1421 return retval;
1422}
1423
1424/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001425 * Skip over an expression at "*pp".
1426 * Return FAIL for an error, OK otherwise.
1427 */
1428 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001429skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001430{
Bram Moolenaar33570922005-01-25 22:26:29 +00001431 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001432
1433 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001435}
1436
1437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001439 * When "convert" is TRUE convert a List into a sequence of lines and convert
1440 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 * Return pointer to allocated memory, or NULL for failure.
1442 */
1443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001444eval_to_string(
1445 char_u *arg,
1446 char_u **nextcmd,
1447 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448{
Bram Moolenaar33570922005-01-25 22:26:29 +00001449 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001451 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001452#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001456 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 retval = NULL;
1458 else
1459 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001460 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001461 {
1462 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001463 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001464 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001465 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001466 if (tv.vval.v_list->lv_len > 0)
1467 ga_append(&ga, NL);
1468 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 ga_append(&ga, NUL);
1470 retval = (char_u *)ga.ga_data;
1471 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001472#ifdef FEAT_FLOAT
1473 else if (convert && tv.v_type == VAR_FLOAT)
1474 {
1475 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1476 retval = vim_strsave(numbuf);
1477 }
1478#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001479 else
1480 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001481 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483
1484 return retval;
1485}
1486
1487/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001488 * Call eval_to_string() without using current local variables and using
1489 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 */
1491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492eval_to_string_safe(
1493 char_u *arg,
1494 char_u **nextcmd,
1495 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
1497 char_u *retval;
1498 void *save_funccalp;
1499
1500 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001501 if (use_sandbox)
1502 ++sandbox;
1503 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001504 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001505 if (use_sandbox)
1506 --sandbox;
1507 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 restore_funccal(save_funccalp);
1509 return retval;
1510}
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512/*
1513 * Top level evaluation function, returning a number.
1514 * Evaluates "expr" silently.
1515 * Returns -1 for an error.
1516 */
1517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001518eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar33570922005-01-25 22:26:29 +00001520 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001522 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
1524 ++emsg_off;
1525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001526 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 retval = -1;
1528 else
1529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001530 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001531 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 }
1533 --emsg_off;
1534
1535 return retval;
1536}
1537
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538/*
1539 * Prepare v: variable "idx" to be used.
1540 * Save the current typeval in "save_tv".
1541 * When not used yet add the variable to the v: hashtable.
1542 */
1543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001544prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001545{
1546 *save_tv = vimvars[idx].vv_tv;
1547 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1548 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1549}
1550
1551/*
1552 * Restore v: variable "idx" to typeval "save_tv".
1553 * When no longer defined, remove the variable from the v: hashtable.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001557{
1558 hashitem_T *hi;
1559
Bram Moolenaara40058a2005-07-11 22:42:07 +00001560 vimvars[idx].vv_tv = *save_tv;
1561 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1562 {
1563 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1564 if (HASHITEM_EMPTY(hi))
1565 EMSG2(_(e_intern2), "restore_vimvar()");
1566 else
1567 hash_remove(&vimvarht, hi);
1568 }
1569}
1570
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001571#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001572/*
1573 * Evaluate an expression to a list with suggestions.
1574 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001575 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001576 */
1577 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001579{
1580 typval_T save_val;
1581 typval_T rettv;
1582 list_T *list = NULL;
1583 char_u *p = skipwhite(expr);
1584
1585 /* Set "v:val" to the bad word. */
1586 prepare_vimvar(VV_VAL, &save_val);
1587 vimvars[VV_VAL].vv_type = VAR_STRING;
1588 vimvars[VV_VAL].vv_str = badword;
1589 if (p_verbose == 0)
1590 ++emsg_off;
1591
1592 if (eval1(&p, &rettv, TRUE) == OK)
1593 {
1594 if (rettv.v_type != VAR_LIST)
1595 clear_tv(&rettv);
1596 else
1597 list = rettv.vval.v_list;
1598 }
1599
1600 if (p_verbose == 0)
1601 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001602 restore_vimvar(VV_VAL, &save_val);
1603
1604 return list;
1605}
1606
1607/*
1608 * "list" is supposed to contain two items: a word and a number. Return the
1609 * word in "pp" and the number as the return value.
1610 * Return -1 if anything isn't right.
1611 * Used to get the good word and score from the eval_spell_expr() result.
1612 */
1613 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001615{
1616 listitem_T *li;
1617
1618 li = list->lv_first;
1619 if (li == NULL)
1620 return -1;
1621 *pp = get_tv_string(&li->li_tv);
1622
1623 li = li->li_next;
1624 if (li == NULL)
1625 return -1;
1626 return get_tv_number(&li->li_tv);
1627}
1628#endif
1629
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001630/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001631 * Top level evaluation function.
1632 * Returns an allocated typval_T with the result.
1633 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001634 */
1635 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001637{
1638 typval_T *tv;
1639
1640 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001641 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001642 {
1643 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001644 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001645 }
1646
1647 return tv;
1648}
1649
1650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001652 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001653 * Uses argv[argc] for the function arguments. Only Number and String
1654 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001655 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001658call_vim_function(
1659 char_u *func,
1660 int argc,
1661 char_u **argv,
1662 int safe, /* use the sandbox */
1663 int str_arg_only, /* all arguments are strings */
1664 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665{
Bram Moolenaar33570922005-01-25 22:26:29 +00001666 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 long n;
1668 int len;
1669 int i;
1670 int doesrange;
1671 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001672 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001674 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 for (i = 0; i < argc; i++)
1679 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001680 /* Pass a NULL or empty argument as an empty string */
1681 if (argv[i] == NULL || *argv[i] == NUL)
1682 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001683 argvars[i].v_type = VAR_STRING;
1684 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001685 continue;
1686 }
1687
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001688 if (str_arg_only)
1689 len = 0;
1690 else
1691 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001692 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (len != 0 && len == (int)STRLEN(argv[i]))
1694 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001695 argvars[i].v_type = VAR_NUMBER;
1696 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 }
1698 else
1699 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001700 argvars[i].v_type = VAR_STRING;
1701 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 }
1703 }
1704
1705 if (safe)
1706 {
1707 save_funccalp = save_funccal();
1708 ++sandbox;
1709 }
1710
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1712 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001714 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (safe)
1716 {
1717 --sandbox;
1718 restore_funccal(save_funccalp);
1719 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001720 vim_free(argvars);
1721
1722 if (ret == FAIL)
1723 clear_tv(rettv);
1724
1725 return ret;
1726}
1727
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001728/*
1729 * Call vimL function "func" and return the result as a number.
1730 * Returns -1 when calling the function fails.
1731 * Uses argv[argc] for the function arguments.
1732 */
1733 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734call_func_retnr(
1735 char_u *func,
1736 int argc,
1737 char_u **argv,
1738 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001739{
1740 typval_T rettv;
1741 long retval;
1742
1743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1745 return -1;
1746
1747 retval = get_tv_number_chk(&rettv, NULL);
1748 clear_tv(&rettv);
1749 return retval;
1750}
1751
1752#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1753 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1754
Bram Moolenaar4f688582007-07-24 12:34:30 +00001755# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001757 * Call vimL function "func" and return the result as a string.
1758 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001759 * Uses argv[argc] for the function arguments.
1760 */
1761 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762call_func_retstr(
1763 char_u *func,
1764 int argc,
1765 char_u **argv,
1766 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767{
1768 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001769 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001771 /* All arguments are passed as strings, no conversion to number. */
1772 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001773 return NULL;
1774
1775 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 return retval;
1778}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001779# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001780
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001781/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001782 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001784 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001785 */
1786 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787call_func_retlist(
1788 char_u *func,
1789 int argc,
1790 char_u **argv,
1791 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001792{
1793 typval_T rettv;
1794
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001795 /* All arguments are passed as strings, no conversion to number. */
1796 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001797 return NULL;
1798
1799 if (rettv.v_type != VAR_LIST)
1800 {
1801 clear_tv(&rettv);
1802 return NULL;
1803 }
1804
1805 return rettv.vval.v_list;
1806}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
1808
1809/*
1810 * Save the current function call pointer, and set it to NULL.
1811 * Used when executing autocommands and for ":source".
1812 */
1813 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 current_funccal = NULL;
1819 return (void *)fc;
1820}
1821
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825 funccall_T *fc = (funccall_T *)vfc;
1826
1827 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828}
1829
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830#if defined(FEAT_PROFILE) || defined(PROTO)
1831/*
1832 * Prepare profiling for entering a child or something else that is not
1833 * counted for the script/function itself.
1834 * Should always be called in pair with prof_child_exit().
1835 */
1836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001837prof_child_enter(
1838 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001839{
1840 funccall_T *fc = current_funccal;
1841
1842 if (fc != NULL && fc->func->uf_profiling)
1843 profile_start(&fc->prof_child);
1844 script_prof_save(tm);
1845}
1846
1847/*
1848 * Take care of time spent in a child.
1849 * Should always be called after prof_child_enter().
1850 */
1851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001852prof_child_exit(
1853 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001854{
1855 funccall_T *fc = current_funccal;
1856
1857 if (fc != NULL && fc->func->uf_profiling)
1858 {
1859 profile_end(&fc->prof_child);
1860 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1861 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1862 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1863 }
1864 script_prof_restore(tm);
1865}
1866#endif
1867
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869#ifdef FEAT_FOLDING
1870/*
1871 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1872 * it in "*cp". Doesn't give error messages.
1873 */
1874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876{
Bram Moolenaar33570922005-01-25 22:26:29 +00001877 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 int retval;
1879 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001880 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1881 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882
1883 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001884 if (use_sandbox)
1885 ++sandbox;
1886 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 retval = 0;
1890 else
1891 {
1892 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (tv.v_type == VAR_NUMBER)
1894 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001895 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 retval = 0;
1897 else
1898 {
1899 /* If the result is a string, check if there is a non-digit before
1900 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (!VIM_ISDIGIT(*s) && *s != '-')
1903 *cp = *s++;
1904 retval = atol((char *)s);
1905 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 }
1908 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001909 if (use_sandbox)
1910 --sandbox;
1911 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912
1913 return retval;
1914}
1915#endif
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001918 * ":let" list all variable values
1919 * ":let var1 var2" list variable values
1920 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 * ":let var += expr" assignment command.
1922 * ":let var -= expr" assignment command.
1923 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 */
1926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001927ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928{
1929 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 int var_count = 0;
1934 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001936 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaardb552d602006-03-23 22:59:57 +00001939 argend = skip_var_list(arg, &var_count, &semicolon);
1940 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001941 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001942 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1943 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001944 expr = skipwhite(argend);
1945 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1946 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001948 /*
1949 * ":let" without "=": list variables
1950 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 if (*arg == '[')
1952 EMSG(_(e_invarg));
1953 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001955 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001959 list_glob_vars(&first);
1960 list_buf_vars(&first);
1961 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001962#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001963 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001964#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001965 list_script_vars(&first);
1966 list_func_vars(&first);
1967 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 eap->nextcmd = check_nextcmd(arg);
1970 }
1971 else
1972 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 op[0] = '=';
1974 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001975 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001977 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1978 op[0] = *expr; /* +=, -= or .= */
1979 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001980 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001981 else
1982 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (eap->skip)
1985 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 if (eap->skip)
1988 {
1989 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 --emsg_skip;
1992 }
1993 else if (i != FAIL)
1994 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001997 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 }
2000}
2001
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002/*
2003 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2004 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002005 * When "nextchars" is not NULL it points to a string with characters that
2006 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2007 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002008 * Returns OK or FAIL;
2009 */
2010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002011ex_let_vars(
2012 char_u *arg_start,
2013 typval_T *tv,
2014 int copy, /* copy values from "tv", don't move */
2015 int semicolon, /* from skip_var_list() */
2016 int var_count, /* from skip_var_list() */
2017 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018{
2019 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002020 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 listitem_T *item;
2023 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024
2025 if (*arg != '[')
2026 {
2027 /*
2028 * ":let var = expr" or ":for var in list"
2029 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002030 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return FAIL;
2032 return OK;
2033 }
2034
2035 /*
2036 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2037 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002038 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 {
2040 EMSG(_(e_listreq));
2041 return FAIL;
2042 }
2043
2044 i = list_len(l);
2045 if (semicolon == 0 && var_count < i)
2046 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002047 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002048 return FAIL;
2049 }
2050 if (var_count - semicolon > i)
2051 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002052 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 return FAIL;
2054 }
2055
2056 item = l->lv_first;
2057 while (*arg != ']')
2058 {
2059 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002060 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002061 item = item->li_next;
2062 if (arg == NULL)
2063 return FAIL;
2064
2065 arg = skipwhite(arg);
2066 if (*arg == ';')
2067 {
2068 /* Put the rest of the list (may be empty) in the var after ';'.
2069 * Create a new list for this. */
2070 l = list_alloc();
2071 if (l == NULL)
2072 return FAIL;
2073 while (item != NULL)
2074 {
2075 list_append_tv(l, &item->li_tv);
2076 item = item->li_next;
2077 }
2078
2079 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002080 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081 ltv.vval.v_list = l;
2082 l->lv_refcount = 1;
2083
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002084 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2085 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 clear_tv(&ltv);
2087 if (arg == NULL)
2088 return FAIL;
2089 break;
2090 }
2091 else if (*arg != ',' && *arg != ']')
2092 {
2093 EMSG2(_(e_intern2), "ex_let_vars()");
2094 return FAIL;
2095 }
2096 }
2097
2098 return OK;
2099}
2100
2101/*
2102 * Skip over assignable variable "var" or list of variables "[var, var]".
2103 * Used for ":let varvar = expr" and ":for varvar in expr".
2104 * For "[var, var]" increment "*var_count" for each variable.
2105 * for "[var, var; var]" set "semicolon".
2106 * Return NULL for an error.
2107 */
2108 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109skip_var_list(
2110 char_u *arg,
2111 int *var_count,
2112 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113{
2114 char_u *p, *s;
2115
2116 if (*arg == '[')
2117 {
2118 /* "[var, var]": find the matching ']'. */
2119 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002120 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002121 {
2122 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2123 s = skip_var_one(p);
2124 if (s == p)
2125 {
2126 EMSG2(_(e_invarg2), p);
2127 return NULL;
2128 }
2129 ++*var_count;
2130
2131 p = skipwhite(s);
2132 if (*p == ']')
2133 break;
2134 else if (*p == ';')
2135 {
2136 if (*semicolon == 1)
2137 {
2138 EMSG(_("Double ; in list of variables"));
2139 return NULL;
2140 }
2141 *semicolon = 1;
2142 }
2143 else if (*p != ',')
2144 {
2145 EMSG2(_(e_invarg2), p);
2146 return NULL;
2147 }
2148 }
2149 return p + 1;
2150 }
2151 else
2152 return skip_var_one(arg);
2153}
2154
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002155/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002156 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002157 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002158 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002159 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002161{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002162 if (*arg == '@' && arg[1] != NUL)
2163 return arg + 2;
2164 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2165 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002166}
2167
Bram Moolenaara7043832005-01-21 11:56:39 +00002168/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002169 * List variables for hashtab "ht" with prefix "prefix".
2170 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_hashtable_vars(
2174 hashtab_T *ht,
2175 char_u *prefix,
2176 int empty,
2177 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002178{
Bram Moolenaar33570922005-01-25 22:26:29 +00002179 hashitem_T *hi;
2180 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002181 int todo;
2182
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002183 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2185 {
2186 if (!HASHITEM_EMPTY(hi))
2187 {
2188 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002189 di = HI2DI(hi);
2190 if (empty || di->di_tv.v_type != VAR_STRING
2191 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193 }
2194 }
2195}
2196
2197/*
2198 * List global variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002203 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002204}
2205
2206/*
2207 * List buffer variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002211{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 char_u numbuf[NUMBUFLEN];
2213
Bram Moolenaar429fa852013-04-15 12:27:36 +02002214 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216
2217 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2219 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002220}
2221
2222/*
2223 * List window variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002227{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002228 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002229 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002230}
2231
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232#ifdef FEAT_WINDOWS
2233/*
2234 * List tab page variables.
2235 */
2236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002237list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002239 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241}
2242#endif
2243
Bram Moolenaara7043832005-01-21 11:56:39 +00002244/*
2245 * List Vim variables.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251}
2252
2253/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002254 * List script-local variables, if there is a script.
2255 */
2256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002258{
2259 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2261 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002262}
2263
2264/*
2265 * List function variables, if there is a function.
2266 */
2267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002268list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002269{
2270 if (current_funccal != NULL)
2271 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002272 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002273}
2274
2275/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 * List variables in "arg".
2277 */
2278 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002279list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280{
2281 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 char_u *name_start;
2285 char_u *arg_subsc;
2286 char_u *tofree;
2287 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288
2289 while (!ends_excmd(*arg) && !got_int)
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002293 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2295 {
2296 emsg_severe = TRUE;
2297 EMSG(_(e_trailing));
2298 break;
2299 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 /* get_name_len() takes care of expanding curly braces */
2304 name_start = name = arg;
2305 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2306 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002307 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002308 /* This is mainly to keep test 49 working: when expanding
2309 * curly braces fails overrule the exception error message. */
2310 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 emsg_severe = TRUE;
2313 EMSG2(_(e_invarg2), arg);
2314 break;
2315 }
2316 error = TRUE;
2317 }
2318 else
2319 {
2320 if (tofree != NULL)
2321 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002322 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002323 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 else
2325 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002326 /* handle d.key, l[idx], f(expr) */
2327 arg_subsc = arg;
2328 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002329 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002330 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002331 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002334 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002335 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002336 case 'g': list_glob_vars(first); break;
2337 case 'b': list_buf_vars(first); break;
2338 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002339#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002340 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002341#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002342 case 'v': list_vim_vars(first); break;
2343 case 's': list_script_vars(first); break;
2344 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345 default:
2346 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002347 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
2349 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002350 {
2351 char_u numbuf[NUMBUFLEN];
2352 char_u *tf;
2353 int c;
2354 char_u *s;
2355
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002356 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002357 c = *arg;
2358 *arg = NUL;
2359 list_one_var_a((char_u *)"",
2360 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002361 tv.v_type,
2362 s == NULL ? (char_u *)"" : s,
2363 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002364 *arg = c;
2365 vim_free(tf);
2366 }
2367 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002368 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002371
2372 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374
2375 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002376 }
2377
2378 return arg;
2379}
2380
2381/*
2382 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2383 * Returns a pointer to the char just after the var name.
2384 * Returns NULL if there is an error.
2385 */
2386 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002387ex_let_one(
2388 char_u *arg, /* points to variable name */
2389 typval_T *tv, /* value to assign to variable */
2390 int copy, /* copy value from "tv" */
2391 char_u *endchars, /* valid chars after variable name or NULL */
2392 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393{
2394 int c1;
2395 char_u *name;
2396 char_u *p;
2397 char_u *arg_end = NULL;
2398 int len;
2399 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002400 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002401
2402 /*
2403 * ":let $VAR = expr": Set environment variable.
2404 */
2405 if (*arg == '$')
2406 {
2407 /* Find the end of the name. */
2408 ++arg;
2409 name = arg;
2410 len = get_env_len(&arg);
2411 if (len == 0)
2412 EMSG2(_(e_invarg2), name - 1);
2413 else
2414 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 if (op != NULL && (*op == '+' || *op == '-'))
2416 EMSG2(_(e_letwrong), op);
2417 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002418 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002420 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 {
2422 c1 = name[len];
2423 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002424 p = get_tv_string_chk(tv);
2425 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
2427 int mustfree = FALSE;
2428 char_u *s = vim_getenv(name, &mustfree);
2429
2430 if (s != NULL)
2431 {
2432 p = tofree = concat_str(s, p);
2433 if (mustfree)
2434 vim_free(s);
2435 }
2436 }
2437 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002438 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002439 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002440 if (STRICMP(name, "HOME") == 0)
2441 init_homedir();
2442 else if (didset_vim && STRICMP(name, "VIM") == 0)
2443 didset_vim = FALSE;
2444 else if (didset_vimruntime
2445 && STRICMP(name, "VIMRUNTIME") == 0)
2446 didset_vimruntime = FALSE;
2447 arg_end = arg;
2448 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002449 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002451 }
2452 }
2453 }
2454
2455 /*
2456 * ":let &option = expr": Set option value.
2457 * ":let &l:option = expr": Set local option value.
2458 * ":let &g:option = expr": Set global option value.
2459 */
2460 else if (*arg == '&')
2461 {
2462 /* Find the end of the name. */
2463 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002464 if (p == NULL || (endchars != NULL
2465 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 EMSG(_(e_letunexp));
2467 else
2468 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002469 long n;
2470 int opt_type;
2471 long numval;
2472 char_u *stringval = NULL;
2473 char_u *s;
2474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 c1 = *p;
2476 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477
2478 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002479 s = get_tv_string_chk(tv); /* != NULL if number or string */
2480 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 {
2482 opt_type = get_option_value(arg, &numval,
2483 &stringval, opt_flags);
2484 if ((opt_type == 1 && *op == '.')
2485 || (opt_type == 0 && *op != '.'))
2486 EMSG2(_(e_letwrong), op);
2487 else
2488 {
2489 if (opt_type == 1) /* number */
2490 {
2491 if (*op == '+')
2492 n = numval + n;
2493 else
2494 n = numval - n;
2495 }
2496 else if (opt_type == 0 && stringval != NULL) /* string */
2497 {
2498 s = concat_str(stringval, s);
2499 vim_free(stringval);
2500 stringval = s;
2501 }
2502 }
2503 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (s != NULL)
2505 {
2506 set_option_value(arg, n, s, opt_flags);
2507 arg_end = p;
2508 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002509 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let @r = expr": Set register contents.
2516 */
2517 else if (*arg == '@')
2518 {
2519 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 if (op != NULL && (*op == '+' || *op == '-'))
2521 EMSG2(_(e_letwrong), op);
2522 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002523 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 EMSG(_(e_letunexp));
2525 else
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 char_u *s;
2529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002530 p = get_tv_string_chk(tv);
2531 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002533 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 if (s != NULL)
2535 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 vim_free(s);
2538 }
2539 }
2540 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002543 arg_end = arg + 1;
2544 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002545 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002546 }
2547 }
2548
2549 /*
2550 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002554 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002555 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002557 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2561 EMSG(_(e_letunexp));
2562 else
2563 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 arg_end = p;
2566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002567 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002568 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 }
2570
2571 else
2572 EMSG2(_(e_invarg2), arg);
2573
2574 return arg_end;
2575}
2576
2577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002578 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2579 */
2580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002582{
2583 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2584 {
2585 EMSG2(_(e_readonlyvar), arg);
2586 return TRUE;
2587 }
2588 return FALSE;
2589}
2590
2591/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 * Get an lval: variable, Dict item or List item that can be assigned a value
2593 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2594 * "name.key", "name.key[expr]" etc.
2595 * Indexing only works if "name" is an existing List or Dictionary.
2596 * "name" points to the start of the name.
2597 * If "rettv" is not NULL it points to the value to be assigned.
2598 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2599 * wrong; must end in space or cmd separator.
2600 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 * flags:
2602 * GLV_QUIET: do not give error messages
2603 * GLV_NO_AUTOLOAD: do not use script autoloading
2604 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002606 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 */
2609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002610get_lval(
2611 char_u *name,
2612 typval_T *rettv,
2613 lval_T *lp,
2614 int unlet,
2615 int skip,
2616 int flags, /* GLV_ values */
2617 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002620 char_u *expr_start, *expr_end;
2621 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002622 dictitem_T *v;
2623 typval_T var1;
2624 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002626 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002627 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002629 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002630 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002632 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634
2635 if (skip)
2636 {
2637 /* When skipping just find the end of the name. */
2638 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002639 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 }
2641
2642 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002643 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (expr_start != NULL)
2645 {
2646 /* Don't expand the name when we already know there is an error. */
2647 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2648 && *p != '[' && *p != '.')
2649 {
2650 EMSG(_(e_trailing));
2651 return NULL;
2652 }
2653
2654 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2655 if (lp->ll_exp_name == NULL)
2656 {
2657 /* Report an invalid expression in braces, unless the
2658 * expression evaluation has been cancelled due to an
2659 * aborting error, an interrupt, or an exception. */
2660 if (!aborting() && !quiet)
2661 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002662 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 EMSG2(_(e_invarg2), name);
2664 return NULL;
2665 }
2666 }
2667 lp->ll_name = lp->ll_exp_name;
2668 }
2669 else
2670 lp->ll_name = name;
2671
2672 /* Without [idx] or .key we are done. */
2673 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2674 return p;
2675
2676 cc = *p;
2677 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002678 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (v == NULL && !quiet)
2680 EMSG2(_(e_undefvar), lp->ll_name);
2681 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 if (v == NULL)
2683 return NULL;
2684
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 /*
2686 * Loop until no more [idx] or .key is following.
2687 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002688 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002689 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002690 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2692 && !(lp->ll_tv->v_type == VAR_DICT
2693 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 if (!quiet)
2696 EMSG(_("E689: Can only index a List or Dictionary"));
2697 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002698 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_("E708: [:] must come last"));
2703 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002704 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002705
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 len = -1;
2707 if (*p == '.')
2708 {
2709 key = p + 1;
2710 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2711 ;
2712 if (len == 0)
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (!quiet)
2715 EMSG(_(e_emptykey));
2716 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 }
2718 p = key + len;
2719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 else
2721 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002723 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 if (*p == ':')
2725 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002726 else
2727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 empty1 = FALSE;
2729 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002731 if (get_tv_string_chk(&var1) == NULL)
2732 {
2733 /* not a number or string */
2734 clear_tv(&var1);
2735 return NULL;
2736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 }
2738
2739 /* Optionally get the second index [ :expr]. */
2740 if (*p == ':')
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002745 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002750 if (rettv != NULL && (rettv->v_type != VAR_LIST
2751 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 if (!quiet)
2754 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 if (!empty1)
2756 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 }
2759 p = skipwhite(p + 1);
2760 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 else
2763 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (!empty1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 if (get_tv_string_chk(&var2) == NULL)
2772 {
2773 /* not a number or string */
2774 if (!empty1)
2775 clear_tv(&var1);
2776 clear_tv(&var2);
2777 return NULL;
2778 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002781 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002784
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002786 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002787 if (!quiet)
2788 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (!empty1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002793 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002794 }
2795
2796 /* Skip to past ']'. */
2797 ++p;
2798 }
2799
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 {
2802 if (len == -1)
2803 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002805 key = get_tv_string_chk(&var1); /* is number or string */
2806 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002810 }
2811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002812 lp->ll_list = NULL;
2813 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002814 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002815
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002816 /* When assigning to a scope dictionary check that a function and
2817 * variable name is valid (only variable name unless it is l: or
2818 * g: dictionary). Disallow overwriting a builtin function. */
2819 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002820 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002821 int prevval;
2822 int wrong;
2823
2824 if (len != -1)
2825 {
2826 prevval = key[len];
2827 key[len] = NUL;
2828 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002829 else
2830 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002831 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2832 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002833 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002834 || !valid_varname(key);
2835 if (len != -1)
2836 key[len] = prevval;
2837 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839 }
2840
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* Can't add "v:" variable. */
2844 if (lp->ll_dict == &vimvardict)
2845 {
2846 EMSG2(_(e_illvar), name);
2847 return NULL;
2848 }
2849
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002850 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002854 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002855 if (len == -1)
2856 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 }
2859 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 if (len == -1)
2864 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 p = NULL;
2867 break;
2868 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002869 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002870 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002871 return NULL;
2872
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 if (len == -1)
2874 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 }
2877 else
2878 {
2879 /*
2880 * Get the number and item for the only or first index of the List.
2881 */
2882 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 else
2885 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002886 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 clear_tv(&var1);
2888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 lp->ll_list = lp->ll_tv->vval.v_list;
2891 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2892 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 if (lp->ll_n1 < 0)
2895 {
2896 lp->ll_n1 = 0;
2897 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2898 }
2899 }
2900 if (lp->ll_li == NULL)
2901 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 }
2908
2909 /*
2910 * May need to find the item or absolute index for the second
2911 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 * When no index given: "lp->ll_empty2" is TRUE.
2913 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002914 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002917 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002918 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002922 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002923 {
2924 if (!quiet)
2925 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002927 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002929 }
2930
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2932 if (lp->ll_n1 < 0)
2933 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2934 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002935 {
2936 if (!quiet)
2937 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002939 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 }
2941
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 }
2945
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946 return p;
2947}
2948
2949/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002950 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 */
2952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954{
2955 vim_free(lp->ll_exp_name);
2956 vim_free(lp->ll_newkey);
2957}
2958
2959/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002963 */
2964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002965set_var_lval(
2966 lval_T *lp,
2967 char_u *endp,
2968 typval_T *rettv,
2969 int copy,
2970 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971{
2972 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002973 listitem_T *ri;
2974 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975
2976 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002980 cc = *endp;
2981 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002982 if (op != NULL && *op != '=')
2983 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002984 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002985
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002986 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002987 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002988 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002989 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002990 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002991 if ((di == NULL
2992 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2993 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2994 FALSE)))
2995 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002996 set_var(lp->ll_name, &tv, FALSE);
2997 clear_tv(&tv);
2998 }
2999 }
3000 else
3001 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003004 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 else if (tv_check_lock(lp->ll_newkey == NULL
3006 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003007 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003008 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 else if (lp->ll_range)
3010 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 listitem_T *ll_li = lp->ll_li;
3012 int ll_n1 = lp->ll_n1;
3013
3014 /*
3015 * Check whether any of the list items is locked
3016 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003017 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003019 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020 return;
3021 ri = ri->li_next;
3022 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3023 break;
3024 ll_li = ll_li->li_next;
3025 ++ll_n1;
3026 }
3027
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 /*
3029 * Assign the List values to the list items.
3030 */
3031 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003033 if (op != NULL && *op != '=')
3034 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3035 else
3036 {
3037 clear_tv(&lp->ll_li->li_tv);
3038 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003040 ri = ri->li_next;
3041 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3042 break;
3043 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003044 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003046 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003047 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 ri = NULL;
3049 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003050 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 lp->ll_li = lp->ll_li->li_next;
3053 ++lp->ll_n1;
3054 }
3055 if (ri != NULL)
3056 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003057 else if (lp->ll_empty2
3058 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 : lp->ll_n1 != lp->ll_n2)
3060 EMSG(_("E711: List value has not enough items"));
3061 }
3062 else
3063 {
3064 /*
3065 * Assign to a List or Dictionary item.
3066 */
3067 if (lp->ll_newkey != NULL)
3068 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 if (op != NULL && *op != '=')
3070 {
3071 EMSG2(_(e_letwrong), op);
3072 return;
3073 }
3074
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003076 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 if (di == NULL)
3078 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003079 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3080 {
3081 vim_free(di);
3082 return;
3083 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003084 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003085 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003086 else if (op != NULL && *op != '=')
3087 {
3088 tv_op(lp->ll_tv, rettv, op);
3089 return;
3090 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003091 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003093
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 /*
3095 * Assign the value to the variable or list item.
3096 */
3097 if (copy)
3098 copy_tv(rettv, lp->ll_tv);
3099 else
3100 {
3101 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003102 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003103 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104 }
3105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106}
3107
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003108/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003109 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3110 * Returns OK or FAIL.
3111 */
3112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003114{
3115 long n;
3116 char_u numbuf[NUMBUFLEN];
3117 char_u *s;
3118
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003119 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3120 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3121 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003122 {
3123 switch (tv1->v_type)
3124 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003125 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003126 case VAR_DICT:
3127 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003128 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003129 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003130 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003131 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003132 break;
3133
3134 case VAR_LIST:
3135 if (*op != '+' || tv2->v_type != VAR_LIST)
3136 break;
3137 /* List += List */
3138 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3139 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3140 return OK;
3141
3142 case VAR_NUMBER:
3143 case VAR_STRING:
3144 if (tv2->v_type == VAR_LIST)
3145 break;
3146 if (*op == '+' || *op == '-')
3147 {
3148 /* nr += nr or nr -= nr*/
3149 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150#ifdef FEAT_FLOAT
3151 if (tv2->v_type == VAR_FLOAT)
3152 {
3153 float_T f = n;
3154
3155 if (*op == '+')
3156 f += tv2->vval.v_float;
3157 else
3158 f -= tv2->vval.v_float;
3159 clear_tv(tv1);
3160 tv1->v_type = VAR_FLOAT;
3161 tv1->vval.v_float = f;
3162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003163 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164#endif
3165 {
3166 if (*op == '+')
3167 n += get_tv_number(tv2);
3168 else
3169 n -= get_tv_number(tv2);
3170 clear_tv(tv1);
3171 tv1->v_type = VAR_NUMBER;
3172 tv1->vval.v_number = n;
3173 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003174 }
3175 else
3176 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177 if (tv2->v_type == VAR_FLOAT)
3178 break;
3179
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003180 /* str .= str */
3181 s = get_tv_string(tv1);
3182 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3183 clear_tv(tv1);
3184 tv1->v_type = VAR_STRING;
3185 tv1->vval.v_string = s;
3186 }
3187 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003188
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003190#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003191 {
3192 float_T f;
3193
3194 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3195 && tv2->v_type != VAR_NUMBER
3196 && tv2->v_type != VAR_STRING))
3197 break;
3198 if (tv2->v_type == VAR_FLOAT)
3199 f = tv2->vval.v_float;
3200 else
3201 f = get_tv_number(tv2);
3202 if (*op == '+')
3203 tv1->vval.v_float += f;
3204 else
3205 tv1->vval.v_float -= f;
3206 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003207#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003208 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003209 }
3210 }
3211
3212 EMSG2(_(e_letwrong), op);
3213 return FAIL;
3214}
3215
3216/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003217 * Add a watcher to a list.
3218 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221{
3222 lw->lw_next = l->lv_watch;
3223 l->lv_watch = lw;
3224}
3225
3226/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003227 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228 * No warning when it isn't found...
3229 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003231list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232{
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 lwp = &l->lv_watch;
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 {
3238 if (lw == lwrem)
3239 {
3240 *lwp = lw->lw_next;
3241 break;
3242 }
3243 lwp = &lw->lw_next;
3244 }
3245}
3246
3247/*
3248 * Just before removing an item from a list: advance watchers to the next
3249 * item.
3250 */
3251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
3256 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3257 if (lw->lw_item == item)
3258 lw->lw_item = item->li_next;
3259}
3260
3261/*
3262 * Evaluate the expression used in a ":for var in expr" command.
3263 * "arg" points to "var".
3264 * Set "*errp" to TRUE for an error, FALSE otherwise;
3265 * Return a pointer that holds the info. Null when there is an error.
3266 */
3267 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268eval_for_line(
3269 char_u *arg,
3270 int *errp,
3271 char_u **nextcmdp,
3272 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003276 typval_T tv;
3277 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278
3279 *errp = TRUE; /* default: there is an error */
3280
Bram Moolenaar33570922005-01-25 22:26:29 +00003281 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 if (fi == NULL)
3283 return NULL;
3284
3285 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3286 if (expr == NULL)
3287 return fi;
3288
3289 expr = skipwhite(expr);
3290 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3291 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003292 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 return fi;
3294 }
3295
3296 if (skip)
3297 ++emsg_skip;
3298 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3299 {
3300 *errp = FALSE;
3301 if (!skip)
3302 {
3303 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003304 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003307 clear_tv(&tv);
3308 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003309 else if (l == NULL)
3310 {
3311 /* a null list is like an empty list: do nothing */
3312 clear_tv(&tv);
3313 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003314 else
3315 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003316 /* No need to increment the refcount, it's already set for the
3317 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003318 fi->fi_list = l;
3319 list_add_watch(l, &fi->fi_lw);
3320 fi->fi_lw.lw_item = l->lv_first;
3321 }
3322 }
3323 }
3324 if (skip)
3325 --emsg_skip;
3326
3327 return fi;
3328}
3329
3330/*
3331 * Use the first item in a ":for" list. Advance to the next.
3332 * Assign the values to the variable (list). "arg" points to the first one.
3333 * Return TRUE when a valid item was found, FALSE when at end of list or
3334 * something wrong.
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003338{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003339 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003341 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342
3343 item = fi->fi_lw.lw_item;
3344 if (item == NULL)
3345 result = FALSE;
3346 else
3347 {
3348 fi->fi_lw.lw_item = item->li_next;
3349 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3350 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3351 }
3352 return result;
3353}
3354
3355/*
3356 * Free the structure used to store info used by ":for".
3357 */
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360{
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003363 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003364 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003366 list_unref(fi->fi_list);
3367 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 vim_free(fi);
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3372
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374set_context_for_expression(
3375 expand_T *xp,
3376 char_u *arg,
3377 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 int got_eq = FALSE;
3380 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003381 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003383 if (cmdidx == CMD_let)
3384 {
3385 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003386 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003387 {
3388 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003389 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003390 {
3391 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003393 if (vim_iswhite(*p))
3394 break;
3395 }
3396 return;
3397 }
3398 }
3399 else
3400 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3401 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 while ((xp->xp_pattern = vim_strpbrk(arg,
3403 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3404 {
3405 c = *xp->xp_pattern;
3406 if (c == '&')
3407 {
3408 c = xp->xp_pattern[1];
3409 if (c == '&')
3410 {
3411 ++xp->xp_pattern;
3412 xp->xp_context = cmdidx != CMD_let || got_eq
3413 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3414 }
3415 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003418 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3419 xp->xp_pattern += 2;
3420
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 else if (c == '$')
3424 {
3425 /* environment variable */
3426 xp->xp_context = EXPAND_ENV_VARS;
3427 }
3428 else if (c == '=')
3429 {
3430 got_eq = TRUE;
3431 xp->xp_context = EXPAND_EXPRESSION;
3432 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003433 else if (c == '#'
3434 && xp->xp_context == EXPAND_EXPRESSION)
3435 {
3436 /* Autoload function/variable contains '#'. */
3437 break;
3438 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003439 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 && xp->xp_context == EXPAND_FUNCTIONS
3441 && vim_strchr(xp->xp_pattern, '(') == NULL)
3442 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003443 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 break;
3445 }
3446 else if (cmdidx != CMD_let || got_eq)
3447 {
3448 if (c == '"') /* string */
3449 {
3450 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3451 if (c == '\\' && xp->xp_pattern[1] != NUL)
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_NOTHING;
3454 }
3455 else if (c == '\'') /* literal string */
3456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003457 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3459 /* skip */ ;
3460 xp->xp_context = EXPAND_NOTHING;
3461 }
3462 else if (c == '|')
3463 {
3464 if (xp->xp_pattern[1] == '|')
3465 {
3466 ++xp->xp_pattern;
3467 xp->xp_context = EXPAND_EXPRESSION;
3468 }
3469 else
3470 xp->xp_context = EXPAND_COMMANDS;
3471 }
3472 else
3473 xp->xp_context = EXPAND_EXPRESSION;
3474 }
3475 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003476 /* Doesn't look like something valid, expand as an expression
3477 * anyway. */
3478 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 arg = xp->xp_pattern;
3480 if (*arg != NUL)
3481 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3482 /* skip */ ;
3483 }
3484 xp->xp_pattern = arg;
3485}
3486
3487#endif /* FEAT_CMDL_COMPL */
3488
3489/*
3490 * ":1,25call func(arg1, arg2)" function call.
3491 */
3492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494{
3495 char_u *arg = eap->arg;
3496 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003498 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003500 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 linenr_T lnum;
3502 int doesrange;
3503 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003504 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003505 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003507 if (eap->skip)
3508 {
3509 /* trans_function_name() doesn't work well when skipping, use eval0()
3510 * instead to skip to any following command, e.g. for:
3511 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003512 ++emsg_skip;
3513 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3514 clear_tv(&rettv);
3515 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003516 return;
3517 }
3518
Bram Moolenaar65639032016-03-16 21:40:30 +01003519 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003520 if (fudi.fd_newkey != NULL)
3521 {
3522 /* Still need to give an error message for missing key. */
3523 EMSG2(_(e_dictkey), fudi.fd_newkey);
3524 vim_free(fudi.fd_newkey);
3525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003526 if (tofree == NULL)
3527 return;
3528
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003529 /* Increase refcount on dictionary, it could get deleted when evaluating
3530 * the arguments. */
3531 if (fudi.fd_dict != NULL)
3532 ++fudi.fd_dict->dv_refcount;
3533
Bram Moolenaar65639032016-03-16 21:40:30 +01003534 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3535 * contents. For VAR_PARTIAL get its partial, unless we already have one
3536 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003537 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003538 name = deref_func_name(tofree, &len,
3539 partial != NULL ? NULL : &partial, FALSE);
3540
Bram Moolenaar532c7802005-01-27 14:44:31 +00003541 /* Skip white space to allow ":call func ()". Not good, but required for
3542 * backward compatibility. */
3543 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545
3546 if (*startarg != '(')
3547 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003548 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 goto end;
3550 }
3551
3552 /*
3553 * When skipping, evaluate the function once, to find the end of the
3554 * arguments.
3555 * When the function takes a range, this is discovered after the first
3556 * call, and the loop is broken.
3557 */
3558 if (eap->skip)
3559 {
3560 ++emsg_skip;
3561 lnum = eap->line2; /* do it once, also with an invalid range */
3562 }
3563 else
3564 lnum = eap->line1;
3565 for ( ; lnum <= eap->line2; ++lnum)
3566 {
3567 if (!eap->skip && eap->addr_count > 0)
3568 {
3569 curwin->w_cursor.lnum = lnum;
3570 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003571#ifdef FEAT_VIRTUALEDIT
3572 curwin->w_cursor.coladd = 0;
3573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003576 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003578 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 {
3580 failed = TRUE;
3581 break;
3582 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003583
3584 /* Handle a function returning a Funcref, Dictionary or List. */
3585 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3586 {
3587 failed = TRUE;
3588 break;
3589 }
3590
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (doesrange || eap->skip)
3593 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003597 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003598 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 if (aborting())
3600 break;
3601 }
3602 if (eap->skip)
3603 --emsg_skip;
3604
3605 if (!failed)
3606 {
3607 /* Check for trailing illegal characters and a following command. */
3608 if (!ends_excmd(*arg))
3609 {
3610 emsg_severe = TRUE;
3611 EMSG(_(e_trailing));
3612 }
3613 else
3614 eap->nextcmd = check_nextcmd(arg);
3615 }
3616
3617end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003618 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003619 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620}
3621
3622/*
3623 * ":unlet[!] var1 ... " command.
3624 */
3625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003626ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 ex_unletlock(eap, eap->arg, 0);
3629}
3630
3631/*
3632 * ":lockvar" and ":unlockvar" commands
3633 */
3634 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003635ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 int deep = 2;
3639
3640 if (eap->forceit)
3641 deep = -1;
3642 else if (vim_isdigit(*arg))
3643 {
3644 deep = getdigits(&arg);
3645 arg = skipwhite(arg);
3646 }
3647
3648 ex_unletlock(eap, arg, deep);
3649}
3650
3651/*
3652 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3653 */
3654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003655ex_unletlock(
3656 exarg_T *eap,
3657 char_u *argstart,
3658 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003659{
3660 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003663 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 do
3666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003667 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003668 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003669 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003670 if (lv.ll_name == NULL)
3671 error = TRUE; /* error but continue parsing */
3672 if (name_end == NULL || (!vim_iswhite(*name_end)
3673 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (name_end != NULL)
3676 {
3677 emsg_severe = TRUE;
3678 EMSG(_(e_trailing));
3679 }
3680 if (!(eap->skip || error))
3681 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 break;
3683 }
3684
3685 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003686 {
3687 if (eap->cmdidx == CMD_unlet)
3688 {
3689 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3690 error = TRUE;
3691 }
3692 else
3693 {
3694 if (do_lock_var(&lv, name_end, deep,
3695 eap->cmdidx == CMD_lockvar) == FAIL)
3696 error = TRUE;
3697 }
3698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 if (!eap->skip)
3701 clear_lval(&lv);
3702
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 arg = skipwhite(name_end);
3704 } while (!ends_excmd(*arg));
3705
3706 eap->nextcmd = check_nextcmd(arg);
3707}
3708
Bram Moolenaar8c711452005-01-14 21:53:12 +00003709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710do_unlet_var(
3711 lval_T *lp,
3712 char_u *name_end,
3713 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003714{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 int ret = OK;
3716 int cc;
3717
3718 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 cc = *name_end;
3721 *name_end = NUL;
3722
3723 /* Normal name or expanded name. */
3724 if (check_changedtick(lp->ll_name))
3725 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003726 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003730 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003731 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003732 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003733 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003734 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735 else if (lp->ll_range)
3736 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003737 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003738 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003739 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003740
3741 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3742 {
3743 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003744 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003745 return FAIL;
3746 ll_li = li;
3747 ++ll_n1;
3748 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003749
3750 /* Delete a range of List items. */
3751 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3752 {
3753 li = lp->ll_li->li_next;
3754 listitem_remove(lp->ll_list, lp->ll_li);
3755 lp->ll_li = li;
3756 ++lp->ll_n1;
3757 }
3758 }
3759 else
3760 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003761 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 /* unlet a List item. */
3763 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003764 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003766 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003767 }
3768
3769 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003770}
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772/*
3773 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003774 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 */
3776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003777do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar33570922005-01-25 22:26:29 +00003779 hashtab_T *ht;
3780 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003782 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar33570922005-01-25 22:26:29 +00003785 ht = find_var_ht(name, &varname);
3786 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003788 if (ht == &globvarht)
3789 d = &globvardict;
3790 else if (current_funccal != NULL
3791 && ht == &current_funccal->l_vars.dv_hashtab)
3792 d = &current_funccal->l_vars;
3793 else if (ht == &compat_hashtab)
3794 d = &vimvardict;
3795 else
3796 {
3797 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3798 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3799 }
3800 if (d == NULL)
3801 {
3802 EMSG2(_(e_intern2), "do_unlet()");
3803 return FAIL;
3804 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003805 hi = hash_find(ht, varname);
3806 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003807 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003808 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003809 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003810 || var_check_ro(di->di_flags, name, FALSE)
3811 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003812 return FAIL;
3813
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003814 delete_var(ht, hi);
3815 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003818 if (forceit)
3819 return OK;
3820 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 return FAIL;
3822}
3823
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003824/*
3825 * Lock or unlock variable indicated by "lp".
3826 * "deep" is the levels to go (-1 for unlimited);
3827 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3828 */
3829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003830do_lock_var(
3831 lval_T *lp,
3832 char_u *name_end,
3833 int deep,
3834 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835{
3836 int ret = OK;
3837 int cc;
3838 dictitem_T *di;
3839
3840 if (deep == 0) /* nothing to do */
3841 return OK;
3842
3843 if (lp->ll_tv == NULL)
3844 {
3845 cc = *name_end;
3846 *name_end = NUL;
3847
3848 /* Normal name or expanded name. */
3849 if (check_changedtick(lp->ll_name))
3850 ret = FAIL;
3851 else
3852 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003853 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003854 if (di == NULL)
3855 ret = FAIL;
3856 else
3857 {
3858 if (lock)
3859 di->di_flags |= DI_FLAGS_LOCK;
3860 else
3861 di->di_flags &= ~DI_FLAGS_LOCK;
3862 item_lock(&di->di_tv, deep, lock);
3863 }
3864 }
3865 *name_end = cc;
3866 }
3867 else if (lp->ll_range)
3868 {
3869 listitem_T *li = lp->ll_li;
3870
3871 /* (un)lock a range of List items. */
3872 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3873 {
3874 item_lock(&li->li_tv, deep, lock);
3875 li = li->li_next;
3876 ++lp->ll_n1;
3877 }
3878 }
3879 else if (lp->ll_list != NULL)
3880 /* (un)lock a List item. */
3881 item_lock(&lp->ll_li->li_tv, deep, lock);
3882 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003883 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003884 item_lock(&lp->ll_di->di_tv, deep, lock);
3885
3886 return ret;
3887}
3888
3889/*
3890 * Lock or unlock an item. "deep" is nr of levels to go.
3891 */
3892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003893item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003894{
3895 static int recurse = 0;
3896 list_T *l;
3897 listitem_T *li;
3898 dict_T *d;
3899 hashitem_T *hi;
3900 int todo;
3901
3902 if (recurse >= DICT_MAXNEST)
3903 {
3904 EMSG(_("E743: variable nested too deep for (un)lock"));
3905 return;
3906 }
3907 if (deep == 0)
3908 return;
3909 ++recurse;
3910
3911 /* lock/unlock the item itself */
3912 if (lock)
3913 tv->v_lock |= VAR_LOCKED;
3914 else
3915 tv->v_lock &= ~VAR_LOCKED;
3916
3917 switch (tv->v_type)
3918 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003919 case VAR_UNKNOWN:
3920 case VAR_NUMBER:
3921 case VAR_STRING:
3922 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003923 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003924 case VAR_FLOAT:
3925 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003926 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003927 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003928 break;
3929
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003930 case VAR_LIST:
3931 if ((l = tv->vval.v_list) != NULL)
3932 {
3933 if (lock)
3934 l->lv_lock |= VAR_LOCKED;
3935 else
3936 l->lv_lock &= ~VAR_LOCKED;
3937 if (deep < 0 || deep > 1)
3938 /* recursive: lock/unlock the items the List contains */
3939 for (li = l->lv_first; li != NULL; li = li->li_next)
3940 item_lock(&li->li_tv, deep - 1, lock);
3941 }
3942 break;
3943 case VAR_DICT:
3944 if ((d = tv->vval.v_dict) != NULL)
3945 {
3946 if (lock)
3947 d->dv_lock |= VAR_LOCKED;
3948 else
3949 d->dv_lock &= ~VAR_LOCKED;
3950 if (deep < 0 || deep > 1)
3951 {
3952 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003953 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003954 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3955 {
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 --todo;
3959 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3960 }
3961 }
3962 }
3963 }
3964 }
3965 --recurse;
3966}
3967
Bram Moolenaara40058a2005-07-11 22:42:07 +00003968/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003969 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3970 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003971 */
3972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003973tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003974{
3975 return (tv->v_lock & VAR_LOCKED)
3976 || (tv->v_type == VAR_LIST
3977 && tv->vval.v_list != NULL
3978 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3979 || (tv->v_type == VAR_DICT
3980 && tv->vval.v_dict != NULL
3981 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3982}
3983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3985/*
3986 * Delete all "menutrans_" variables.
3987 */
3988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003989del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003992 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
Bram Moolenaar33570922005-01-25 22:26:29 +00003994 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003995 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003996 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003997 {
3998 if (!HASHITEM_EMPTY(hi))
3999 {
4000 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004001 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4002 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004003 }
4004 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004005 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006}
4007#endif
4008
4009#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4010
4011/*
4012 * Local string buffer for the next two functions to store a variable name
4013 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4014 * get_user_var_name().
4015 */
4016
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004017static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019static char_u *varnamebuf = NULL;
4020static int varnamebuflen = 0;
4021
4022/*
4023 * Function to concatenate a prefix and a variable name.
4024 */
4025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004026cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 int len;
4029
4030 len = (int)STRLEN(name) + 3;
4031 if (len > varnamebuflen)
4032 {
4033 vim_free(varnamebuf);
4034 len += 10; /* some additional space */
4035 varnamebuf = alloc(len);
4036 if (varnamebuf == NULL)
4037 {
4038 varnamebuflen = 0;
4039 return NULL;
4040 }
4041 varnamebuflen = len;
4042 }
4043 *varnamebuf = prefix;
4044 varnamebuf[1] = ':';
4045 STRCPY(varnamebuf + 2, name);
4046 return varnamebuf;
4047}
4048
4049/*
4050 * Function given to ExpandGeneric() to obtain the list of user defined
4051 * (global/buffer/window/built-in) variable names.
4052 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004054get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 static long_u gdone;
4057 static long_u bdone;
4058 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004059#ifdef FEAT_WINDOWS
4060 static long_u tdone;
4061#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 static int vidx;
4063 static hashitem_T *hi;
4064 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065
4066 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004067 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004068 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 tdone = 0;
4071#endif
4072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* Global variables */
4075 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004079 else
4080 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004081 while (HASHITEM_EMPTY(hi))
4082 ++hi;
4083 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4084 return cat_prefix_varname('g', hi->hi_key);
4085 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
4088 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004089 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004092 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004093 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004094 else
4095 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004096 while (HASHITEM_EMPTY(hi))
4097 ++hi;
4098 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004100 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004102 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return (char_u *)"b:changedtick";
4104 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004105
4106 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004108 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004110 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004111 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004112 else
4113 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004118
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004119#ifdef FEAT_WINDOWS
4120 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004121 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004122 if (tdone < ht->ht_used)
4123 {
4124 if (tdone++ == 0)
4125 hi = ht->ht_array;
4126 else
4127 ++hi;
4128 while (HASHITEM_EMPTY(hi))
4129 ++hi;
4130 return cat_prefix_varname('t', hi->hi_key);
4131 }
4132#endif
4133
Bram Moolenaar33570922005-01-25 22:26:29 +00004134 /* v: variables */
4135 if (vidx < VV_LEN)
4136 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
4138 vim_free(varnamebuf);
4139 varnamebuf = NULL;
4140 varnamebuflen = 0;
4141 return NULL;
4142}
4143
4144#endif /* FEAT_CMDL_COMPL */
4145
4146/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004147 * Return TRUE if "pat" matches "text".
4148 * Does not use 'cpo' and always uses 'magic'.
4149 */
4150 static int
4151pattern_match(char_u *pat, char_u *text, int ic)
4152{
4153 int matches = FALSE;
4154 char_u *save_cpo;
4155 regmatch_T regmatch;
4156
4157 /* avoid 'l' flag in 'cpoptions' */
4158 save_cpo = p_cpo;
4159 p_cpo = (char_u *)"";
4160 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4161 if (regmatch.regprog != NULL)
4162 {
4163 regmatch.rm_ic = ic;
4164 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4165 vim_regfree(regmatch.regprog);
4166 }
4167 p_cpo = save_cpo;
4168 return matches;
4169}
4170
4171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 * types for expressions.
4173 */
4174typedef enum
4175{
4176 TYPE_UNKNOWN = 0
4177 , TYPE_EQUAL /* == */
4178 , TYPE_NEQUAL /* != */
4179 , TYPE_GREATER /* > */
4180 , TYPE_GEQUAL /* >= */
4181 , TYPE_SMALLER /* < */
4182 , TYPE_SEQUAL /* <= */
4183 , TYPE_MATCH /* =~ */
4184 , TYPE_NOMATCH /* !~ */
4185} exptype_T;
4186
4187/*
4188 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4191 */
4192
4193/*
4194 * Handle zero level expression.
4195 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004197 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 * Return OK or FAIL.
4199 */
4200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004201eval0(
4202 char_u *arg,
4203 typval_T *rettv,
4204 char_u **nextcmd,
4205 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 int ret;
4208 char_u *p;
4209
4210 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004211 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 if (ret == FAIL || !ends_excmd(*p))
4213 {
4214 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 /*
4217 * Report the invalid expression unless the expression evaluation has
4218 * been cancelled due to an aborting error, an interrupt, or an
4219 * exception.
4220 */
4221 if (!aborting())
4222 EMSG2(_(e_invexpr2), arg);
4223 ret = FAIL;
4224 }
4225 if (nextcmd != NULL)
4226 *nextcmd = check_nextcmd(p);
4227
4228 return ret;
4229}
4230
4231/*
4232 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004233 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 *
4235 * "arg" must point to the first non-white of the expression.
4236 * "arg" is advanced to the next non-white after the recognized expression.
4237 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004238 * Note: "rettv.v_lock" is not set.
4239 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 * Return OK or FAIL.
4241 */
4242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004243eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244{
4245 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 /*
4249 * Get the first variable.
4250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253
4254 if ((*arg)[0] == '?')
4255 {
4256 result = FALSE;
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 int error = FALSE;
4260
4261 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 return FAIL;
4274
4275 /*
4276 * Check for the ":".
4277 */
4278 if ((*arg)[0] != ':')
4279 {
4280 EMSG(_("E109: Missing ':' after '?'"));
4281 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 return FAIL;
4284 }
4285
4286 /*
4287 * Get the third variable.
4288 */
4289 *arg = skipwhite(*arg + 1);
4290 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4291 {
4292 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 return FAIL;
4295 }
4296 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299
4300 return OK;
4301}
4302
4303/*
4304 * Handle first level expression:
4305 * expr2 || expr2 || expr2 logical OR
4306 *
4307 * "arg" must point to the first non-white of the expression.
4308 * "arg" is advanced to the next non-white after the recognized expression.
4309 *
4310 * Return OK or FAIL.
4311 */
4312 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004313eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314{
Bram Moolenaar33570922005-01-25 22:26:29 +00004315 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 long result;
4317 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
4320 /*
4321 * Get the first variable.
4322 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 return FAIL;
4325
4326 /*
4327 * Repeat until there is no following "||".
4328 */
4329 first = TRUE;
4330 result = FALSE;
4331 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4332 {
4333 if (evaluate && first)
4334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004335 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004337 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004338 if (error)
4339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 first = FALSE;
4341 }
4342
4343 /*
4344 * Get the second variable.
4345 */
4346 *arg = skipwhite(*arg + 2);
4347 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4348 return FAIL;
4349
4350 /*
4351 * Compute the result.
4352 */
4353 if (evaluate && !result)
4354 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004357 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004358 if (error)
4359 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 if (evaluate)
4362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004363 rettv->v_type = VAR_NUMBER;
4364 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 }
4367
4368 return OK;
4369}
4370
4371/*
4372 * Handle second level expression:
4373 * expr3 && expr3 && expr3 logical AND
4374 *
4375 * "arg" must point to the first non-white of the expression.
4376 * "arg" is advanced to the next non-white after the recognized expression.
4377 *
4378 * Return OK or FAIL.
4379 */
4380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004381eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382{
Bram Moolenaar33570922005-01-25 22:26:29 +00004383 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 long result;
4385 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004386 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387
4388 /*
4389 * Get the first variable.
4390 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FAIL;
4393
4394 /*
4395 * Repeat until there is no following "&&".
4396 */
4397 first = TRUE;
4398 result = TRUE;
4399 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4400 {
4401 if (evaluate && first)
4402 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004403 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004405 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004406 if (error)
4407 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 first = FALSE;
4409 }
4410
4411 /*
4412 * Get the second variable.
4413 */
4414 *arg = skipwhite(*arg + 2);
4415 if (eval4(arg, &var2, evaluate && result) == FAIL)
4416 return FAIL;
4417
4418 /*
4419 * Compute the result.
4420 */
4421 if (evaluate && result)
4422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004423 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004425 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004426 if (error)
4427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 if (evaluate)
4430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 rettv->v_type = VAR_NUMBER;
4432 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 }
4434 }
4435
4436 return OK;
4437}
4438
4439/*
4440 * Handle third level expression:
4441 * var1 == var2
4442 * var1 =~ var2
4443 * var1 != var2
4444 * var1 !~ var2
4445 * var1 > var2
4446 * var1 >= var2
4447 * var1 < var2
4448 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449 * var1 is var2
4450 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 *
4452 * "arg" must point to the first non-white of the expression.
4453 * "arg" is advanced to the next non-white after the recognized expression.
4454 *
4455 * Return OK or FAIL.
4456 */
4457 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004458eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459{
Bram Moolenaar33570922005-01-25 22:26:29 +00004460 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 char_u *p;
4462 int i;
4463 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004464 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 int len = 2;
4466 long n1, n2;
4467 char_u *s1, *s2;
4468 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470
4471 /*
4472 * Get the first variable.
4473 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476
4477 p = *arg;
4478 switch (p[0])
4479 {
4480 case '=': if (p[1] == '=')
4481 type = TYPE_EQUAL;
4482 else if (p[1] == '~')
4483 type = TYPE_MATCH;
4484 break;
4485 case '!': if (p[1] == '=')
4486 type = TYPE_NEQUAL;
4487 else if (p[1] == '~')
4488 type = TYPE_NOMATCH;
4489 break;
4490 case '>': if (p[1] != '=')
4491 {
4492 type = TYPE_GREATER;
4493 len = 1;
4494 }
4495 else
4496 type = TYPE_GEQUAL;
4497 break;
4498 case '<': if (p[1] != '=')
4499 {
4500 type = TYPE_SMALLER;
4501 len = 1;
4502 }
4503 else
4504 type = TYPE_SEQUAL;
4505 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 case 'i': if (p[1] == 's')
4507 {
4508 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4509 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004510 i = p[len];
4511 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 {
4513 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4514 type_is = TRUE;
4515 }
4516 }
4517 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
4520 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004521 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 */
4523 if (type != TYPE_UNKNOWN)
4524 {
4525 /* extra question mark appended: ignore case */
4526 if (p[len] == '?')
4527 {
4528 ic = TRUE;
4529 ++len;
4530 }
4531 /* extra '#' appended: match case */
4532 else if (p[len] == '#')
4533 {
4534 ic = FALSE;
4535 ++len;
4536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004537 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 else
4539 ic = p_ic;
4540
4541 /*
4542 * Get the second variable.
4543 */
4544 *arg = skipwhite(p + len);
4545 if (eval5(arg, &var2, evaluate) == FAIL)
4546 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004547 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 return FAIL;
4549 }
4550
4551 if (evaluate)
4552 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 if (type_is && rettv->v_type != var2.v_type)
4554 {
4555 /* For "is" a different type always means FALSE, for "notis"
4556 * it means TRUE. */
4557 n1 = (type == TYPE_NEQUAL);
4558 }
4559 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4560 {
4561 if (type_is)
4562 {
4563 n1 = (rettv->v_type == var2.v_type
4564 && rettv->vval.v_list == var2.vval.v_list);
4565 if (type == TYPE_NEQUAL)
4566 n1 = !n1;
4567 }
4568 else if (rettv->v_type != var2.v_type
4569 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4570 {
4571 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004572 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004573 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004574 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004575 clear_tv(rettv);
4576 clear_tv(&var2);
4577 return FAIL;
4578 }
4579 else
4580 {
4581 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004582 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4583 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004584 if (type == TYPE_NEQUAL)
4585 n1 = !n1;
4586 }
4587 }
4588
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4590 {
4591 if (type_is)
4592 {
4593 n1 = (rettv->v_type == var2.v_type
4594 && rettv->vval.v_dict == var2.vval.v_dict);
4595 if (type == TYPE_NEQUAL)
4596 n1 = !n1;
4597 }
4598 else if (rettv->v_type != var2.v_type
4599 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4600 {
4601 if (rettv->v_type != var2.v_type)
4602 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4603 else
4604 EMSG(_("E736: Invalid operation for Dictionary"));
4605 clear_tv(rettv);
4606 clear_tv(&var2);
4607 return FAIL;
4608 }
4609 else
4610 {
4611 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004612 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4613 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004614 if (type == TYPE_NEQUAL)
4615 n1 = !n1;
4616 }
4617 }
4618
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004619 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4620 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004621 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004622 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004624 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004625 clear_tv(rettv);
4626 clear_tv(&var2);
4627 return FAIL;
4628 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004629 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004630 if (type == TYPE_NEQUAL)
4631 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632 }
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634#ifdef FEAT_FLOAT
4635 /*
4636 * If one of the two variables is a float, compare as a float.
4637 * When using "=~" or "!~", always compare as string.
4638 */
4639 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4640 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4641 {
4642 float_T f1, f2;
4643
4644 if (rettv->v_type == VAR_FLOAT)
4645 f1 = rettv->vval.v_float;
4646 else
4647 f1 = get_tv_number(rettv);
4648 if (var2.v_type == VAR_FLOAT)
4649 f2 = var2.vval.v_float;
4650 else
4651 f2 = get_tv_number(&var2);
4652 n1 = FALSE;
4653 switch (type)
4654 {
4655 case TYPE_EQUAL: n1 = (f1 == f2); break;
4656 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4657 case TYPE_GREATER: n1 = (f1 > f2); break;
4658 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4659 case TYPE_SMALLER: n1 = (f1 < f2); break;
4660 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4661 case TYPE_UNKNOWN:
4662 case TYPE_MATCH:
4663 case TYPE_NOMATCH: break; /* avoid gcc warning */
4664 }
4665 }
4666#endif
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 /*
4669 * If one of the two variables is a number, compare as a number.
4670 * When using "=~" or "!~", always compare as string.
4671 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004672 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004675 n1 = get_tv_number(rettv);
4676 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 switch (type)
4678 {
4679 case TYPE_EQUAL: n1 = (n1 == n2); break;
4680 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4681 case TYPE_GREATER: n1 = (n1 > n2); break;
4682 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4683 case TYPE_SMALLER: n1 = (n1 < n2); break;
4684 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4685 case TYPE_UNKNOWN:
4686 case TYPE_MATCH:
4687 case TYPE_NOMATCH: break; /* avoid gcc warning */
4688 }
4689 }
4690 else
4691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004692 s1 = get_tv_string_buf(rettv, buf1);
4693 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4695 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4696 else
4697 i = 0;
4698 n1 = FALSE;
4699 switch (type)
4700 {
4701 case TYPE_EQUAL: n1 = (i == 0); break;
4702 case TYPE_NEQUAL: n1 = (i != 0); break;
4703 case TYPE_GREATER: n1 = (i > 0); break;
4704 case TYPE_GEQUAL: n1 = (i >= 0); break;
4705 case TYPE_SMALLER: n1 = (i < 0); break;
4706 case TYPE_SEQUAL: n1 = (i <= 0); break;
4707
4708 case TYPE_MATCH:
4709 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004710 n1 = pattern_match(s2, s1, ic);
4711 if (type == TYPE_NOMATCH)
4712 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714
4715 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4716 }
4717 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004718 clear_tv(rettv);
4719 clear_tv(&var2);
4720 rettv->v_type = VAR_NUMBER;
4721 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 }
4723 }
4724
4725 return OK;
4726}
4727
4728/*
4729 * Handle fourth level expression:
4730 * + number addition
4731 * - number subtraction
4732 * . string concatenation
4733 *
4734 * "arg" must point to the first non-white of the expression.
4735 * "arg" is advanced to the next non-white after the recognized expression.
4736 *
4737 * Return OK or FAIL.
4738 */
4739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004740eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
Bram Moolenaar33570922005-01-25 22:26:29 +00004742 typval_T var2;
4743 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 int op;
4745 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746#ifdef FEAT_FLOAT
4747 float_T f1 = 0, f2 = 0;
4748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 char_u *s1, *s2;
4750 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4751 char_u *p;
4752
4753 /*
4754 * Get the first variable.
4755 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004756 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 return FAIL;
4758
4759 /*
4760 * Repeat computing, until no '+', '-' or '.' is following.
4761 */
4762 for (;;)
4763 {
4764 op = **arg;
4765 if (op != '+' && op != '-' && op != '.')
4766 break;
4767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004768 if ((op != '+' || rettv->v_type != VAR_LIST)
4769#ifdef FEAT_FLOAT
4770 && (op == '.' || rettv->v_type != VAR_FLOAT)
4771#endif
4772 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004773 {
4774 /* For "list + ...", an illegal use of the first operand as
4775 * a number cannot be determined before evaluating the 2nd
4776 * operand: if this is also a list, all is ok.
4777 * For "something . ...", "something - ..." or "non-list + ...",
4778 * we know that the first operand needs to be a string or number
4779 * without evaluating the 2nd operand. So check before to avoid
4780 * side effects after an error. */
4781 if (evaluate && get_tv_string_chk(rettv) == NULL)
4782 {
4783 clear_tv(rettv);
4784 return FAIL;
4785 }
4786 }
4787
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 /*
4789 * Get the second variable.
4790 */
4791 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004794 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
4797
4798 if (evaluate)
4799 {
4800 /*
4801 * Compute the result.
4802 */
4803 if (op == '.')
4804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004805 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4806 s2 = get_tv_string_buf_chk(&var2, buf2);
4807 if (s2 == NULL) /* type error ? */
4808 {
4809 clear_tv(rettv);
4810 clear_tv(&var2);
4811 return FAIL;
4812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004813 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
4815 rettv->v_type = VAR_STRING;
4816 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004818 else if (op == '+' && rettv->v_type == VAR_LIST
4819 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004820 {
4821 /* concatenate Lists */
4822 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4823 &var3) == FAIL)
4824 {
4825 clear_tv(rettv);
4826 clear_tv(&var2);
4827 return FAIL;
4828 }
4829 clear_tv(rettv);
4830 *rettv = var3;
4831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else
4833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004834 int error = FALSE;
4835
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004836#ifdef FEAT_FLOAT
4837 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004838 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839 f1 = rettv->vval.v_float;
4840 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004841 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004842 else
4843#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845 n1 = get_tv_number_chk(rettv, &error);
4846 if (error)
4847 {
4848 /* This can only happen for "list + non-list". For
4849 * "non-list + ..." or "something - ...", we returned
4850 * before evaluating the 2nd operand. */
4851 clear_tv(rettv);
4852 return FAIL;
4853 }
4854#ifdef FEAT_FLOAT
4855 if (var2.v_type == VAR_FLOAT)
4856 f1 = n1;
4857#endif
4858 }
4859#ifdef FEAT_FLOAT
4860 if (var2.v_type == VAR_FLOAT)
4861 {
4862 f2 = var2.vval.v_float;
4863 n2 = 0;
4864 }
4865 else
4866#endif
4867 {
4868 n2 = get_tv_number_chk(&var2, &error);
4869 if (error)
4870 {
4871 clear_tv(rettv);
4872 clear_tv(&var2);
4873 return FAIL;
4874 }
4875#ifdef FEAT_FLOAT
4876 if (rettv->v_type == VAR_FLOAT)
4877 f2 = n2;
4878#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004880 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881
4882#ifdef FEAT_FLOAT
4883 /* If there is a float on either side the result is a float. */
4884 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4885 {
4886 if (op == '+')
4887 f1 = f1 + f2;
4888 else
4889 f1 = f1 - f2;
4890 rettv->v_type = VAR_FLOAT;
4891 rettv->vval.v_float = f1;
4892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894#endif
4895 {
4896 if (op == '+')
4897 n1 = n1 + n2;
4898 else
4899 n1 = n1 - n2;
4900 rettv->v_type = VAR_NUMBER;
4901 rettv->vval.v_number = n1;
4902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004904 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907 return OK;
4908}
4909
4910/*
4911 * Handle fifth level expression:
4912 * * number multiplication
4913 * / number division
4914 * % number modulo
4915 *
4916 * "arg" must point to the first non-white of the expression.
4917 * "arg" is advanced to the next non-white after the recognized expression.
4918 *
4919 * Return OK or FAIL.
4920 */
4921 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004922eval6(
4923 char_u **arg,
4924 typval_T *rettv,
4925 int evaluate,
4926 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
Bram Moolenaar33570922005-01-25 22:26:29 +00004928 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 int op;
4930 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004931#ifdef FEAT_FLOAT
4932 int use_float = FALSE;
4933 float_T f1 = 0, f2;
4934#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004935 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 /*
4938 * Get the first variable.
4939 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004940 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 return FAIL;
4942
4943 /*
4944 * Repeat computing, until no '*', '/' or '%' is following.
4945 */
4946 for (;;)
4947 {
4948 op = **arg;
4949 if (op != '*' && op != '/' && op != '%')
4950 break;
4951
4952 if (evaluate)
4953 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004954#ifdef FEAT_FLOAT
4955 if (rettv->v_type == VAR_FLOAT)
4956 {
4957 f1 = rettv->vval.v_float;
4958 use_float = TRUE;
4959 n1 = 0;
4960 }
4961 else
4962#endif
4963 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004965 if (error)
4966 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 else
4969 n1 = 0;
4970
4971 /*
4972 * Get the second variable.
4973 */
4974 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004975 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 return FAIL;
4977
4978 if (evaluate)
4979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (var2.v_type == VAR_FLOAT)
4982 {
4983 if (!use_float)
4984 {
4985 f1 = n1;
4986 use_float = TRUE;
4987 }
4988 f2 = var2.vval.v_float;
4989 n2 = 0;
4990 }
4991 else
4992#endif
4993 {
4994 n2 = get_tv_number_chk(&var2, &error);
4995 clear_tv(&var2);
4996 if (error)
4997 return FAIL;
4998#ifdef FEAT_FLOAT
4999 if (use_float)
5000 f2 = n2;
5001#endif
5002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
5004 /*
5005 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008#ifdef FEAT_FLOAT
5009 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 if (op == '*')
5012 f1 = f1 * f2;
5013 else if (op == '/')
5014 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005015# ifdef VMS
5016 /* VMS crashes on divide by zero, work around it */
5017 if (f2 == 0.0)
5018 {
5019 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005020 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005021 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005022 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005023 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005024 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005025 }
5026 else
5027 f1 = f1 / f2;
5028# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029 /* We rely on the floating point library to handle divide
5030 * by zero to result in "inf" and not a crash. */
5031 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005032# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005035 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005036 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 return FAIL;
5038 }
5039 rettv->v_type = VAR_FLOAT;
5040 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005045 if (op == '*')
5046 n1 = n1 * n2;
5047 else if (op == '/')
5048 {
5049 if (n2 == 0) /* give an error message? */
5050 {
5051 if (n1 == 0)
5052 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5053 else if (n1 < 0)
5054 n1 = -0x7fffffffL;
5055 else
5056 n1 = 0x7fffffffL;
5057 }
5058 else
5059 n1 = n1 / n2;
5060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 {
5063 if (n2 == 0) /* give an error message? */
5064 n1 = 0;
5065 else
5066 n1 = n1 % n2;
5067 }
5068 rettv->v_type = VAR_NUMBER;
5069 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 return OK;
5075}
5076
5077/*
5078 * Handle sixth level expression:
5079 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005080 * "string" string constant
5081 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 * &option-name option value
5083 * @r register contents
5084 * identifier variable value
5085 * function() function call
5086 * $VAR environment variable
5087 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005088 * [expr, expr] List
5089 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 *
5091 * Also handle:
5092 * ! in front logical NOT
5093 * - in front unary minus
5094 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005095 * trailing [] subscript in String or List
5096 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 *
5098 * "arg" must point to the first non-white of the expression.
5099 * "arg" is advanced to the next non-white after the recognized expression.
5100 *
5101 * Return OK or FAIL.
5102 */
5103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005104eval7(
5105 char_u **arg,
5106 typval_T *rettv,
5107 int evaluate,
5108 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 long n;
5111 int len;
5112 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 char_u *start_leader, *end_leader;
5114 int ret = OK;
5115 char_u *alias;
5116
5117 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122
5123 /*
5124 * Skip '!' and '-' characters. They are handled later.
5125 */
5126 start_leader = *arg;
5127 while (**arg == '!' || **arg == '-' || **arg == '+')
5128 *arg = skipwhite(*arg + 1);
5129 end_leader = *arg;
5130
5131 switch (**arg)
5132 {
5133 /*
5134 * Number constant.
5135 */
5136 case '0':
5137 case '1':
5138 case '2':
5139 case '3':
5140 case '4':
5141 case '5':
5142 case '6':
5143 case '7':
5144 case '8':
5145 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 {
5147#ifdef FEAT_FLOAT
5148 char_u *p = skipdigits(*arg + 1);
5149 int get_float = FALSE;
5150
5151 /* We accept a float when the format matches
5152 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005153 * strict to avoid backwards compatibility problems.
5154 * Don't look for a float after the "." operator, so that
5155 * ":let vers = 1.2.3" doesn't fail. */
5156 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005158 get_float = TRUE;
5159 p = skipdigits(p + 2);
5160 if (*p == 'e' || *p == 'E')
5161 {
5162 ++p;
5163 if (*p == '-' || *p == '+')
5164 ++p;
5165 if (!vim_isdigit(*p))
5166 get_float = FALSE;
5167 else
5168 p = skipdigits(p + 1);
5169 }
5170 if (ASCII_ISALPHA(*p) || *p == '.')
5171 get_float = FALSE;
5172 }
5173 if (get_float)
5174 {
5175 float_T f;
5176
5177 *arg += string2float(*arg, &f);
5178 if (evaluate)
5179 {
5180 rettv->v_type = VAR_FLOAT;
5181 rettv->vval.v_float = f;
5182 }
5183 }
5184 else
5185#endif
5186 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005187 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005188 *arg += len;
5189 if (evaluate)
5190 {
5191 rettv->v_type = VAR_NUMBER;
5192 rettv->vval.v_number = n;
5193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197
5198 /*
5199 * String constant: "string".
5200 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005201 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 break;
5203
5204 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005205 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005207 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208 break;
5209
5210 /*
5211 * List: [expr, expr]
5212 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005213 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215
5216 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005217 * Dictionary: {key: val, key: val}
5218 */
5219 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5220 break;
5221
5222 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005223 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005225 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 break;
5227
5228 /*
5229 * Environment variable: $VAR.
5230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005231 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233
5234 /*
5235 * Register contents: @r.
5236 */
5237 case '@': ++*arg;
5238 if (evaluate)
5239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005241 rettv->vval.v_string = get_reg_contents(**arg,
5242 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244 if (**arg != NUL)
5245 ++*arg;
5246 break;
5247
5248 /*
5249 * nested expression: (expression).
5250 */
5251 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (**arg == ')')
5254 ++*arg;
5255 else if (ret == OK)
5256 {
5257 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005258 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 ret = FAIL;
5260 }
5261 break;
5262
Bram Moolenaar8c711452005-01-14 21:53:12 +00005263 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 break;
5265 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266
5267 if (ret == NOTDONE)
5268 {
5269 /*
5270 * Must be a variable or function name.
5271 * Can also be a curly-braces kind of name: {expr}.
5272 */
5273 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 if (alias != NULL)
5276 s = alias;
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 ret = FAIL;
5280 else
5281 {
5282 if (**arg == '(') /* recursive! */
5283 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005284 partial_T *partial;
5285
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286 /* If "s" is the name of a variable of type VAR_FUNC
5287 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005288 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289
5290 /* Invoke the function. */
5291 ret = get_func_tv(s, len, rettv, arg,
5292 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005293 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005294
5295 /* If evaluate is FALSE rettv->v_type was not set in
5296 * get_func_tv, but it's needed in handle_subscript() to parse
5297 * what follows. So set it here. */
5298 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5299 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005300 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005301 rettv->v_type = VAR_FUNC;
5302 }
5303
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /* Stop the expression evaluation when immediately
5305 * aborting on error, or when an interrupt occurred or
5306 * an exception was thrown but not caught. */
5307 if (aborting())
5308 {
5309 if (ret == OK)
5310 clear_tv(rettv);
5311 ret = FAIL;
5312 }
5313 }
5314 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005315 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005316 else
5317 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005318 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005319 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 }
5321
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 *arg = skipwhite(*arg);
5323
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005324 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5325 * expr(expr). */
5326 if (ret == OK)
5327 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5331 */
5332 if (ret == OK && evaluate && end_leader > start_leader)
5333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005335 int val = 0;
5336#ifdef FEAT_FLOAT
5337 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 if (rettv->v_type == VAR_FLOAT)
5340 f = rettv->vval.v_float;
5341 else
5342#endif
5343 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005346 clear_tv(rettv);
5347 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else
5350 {
5351 while (end_leader > start_leader)
5352 {
5353 --end_leader;
5354 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 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005364 {
5365#ifdef FEAT_FLOAT
5366 if (rettv->v_type == VAR_FLOAT)
5367 f = -f;
5368 else
5369#endif
5370 val = -val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005373#ifdef FEAT_FLOAT
5374 if (rettv->v_type == VAR_FLOAT)
5375 {
5376 clear_tv(rettv);
5377 rettv->vval.v_float = f;
5378 }
5379 else
5380#endif
5381 {
5382 clear_tv(rettv);
5383 rettv->v_type = VAR_NUMBER;
5384 rettv->vval.v_number = val;
5385 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 }
5388
5389 return ret;
5390}
5391
5392/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005393 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5394 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5396 */
5397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005398eval_index(
5399 char_u **arg,
5400 typval_T *rettv,
5401 int evaluate,
5402 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403{
5404 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 long len = -1;
5408 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411
Bram Moolenaara03f2332016-02-06 18:09:59 +01005412 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005415 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005416 if (verbose)
5417 EMSG(_("E695: Cannot index a Funcref"));
5418 return FAIL;
5419 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005420#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005421 if (verbose)
5422 EMSG(_(e_float_as_string));
5423 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005424#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005425 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005427 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005428 if (verbose)
5429 EMSG(_("E909: Cannot index a special variable"));
5430 return FAIL;
5431 case VAR_UNKNOWN:
5432 if (evaluate)
5433 return FAIL;
5434 /* FALLTHROUGH */
5435
5436 case VAR_STRING:
5437 case VAR_NUMBER:
5438 case VAR_LIST:
5439 case VAR_DICT:
5440 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005441 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005443 init_tv(&var1);
5444 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005446 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 /*
5448 * dict.name
5449 */
5450 key = *arg + 1;
5451 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5452 ;
5453 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005455 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 }
5457 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 /*
5460 * something[idx]
5461 *
5462 * Get the (first) variable from inside the [].
5463 */
5464 *arg = skipwhite(*arg + 1);
5465 if (**arg == ':')
5466 empty1 = TRUE;
5467 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5468 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005469 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5470 {
5471 /* not a number or string */
5472 clear_tv(&var1);
5473 return FAIL;
5474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475
5476 /*
5477 * Get the second variable from inside the [:].
5478 */
5479 if (**arg == ':')
5480 {
5481 range = TRUE;
5482 *arg = skipwhite(*arg + 1);
5483 if (**arg == ']')
5484 empty2 = TRUE;
5485 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005487 if (!empty1)
5488 clear_tv(&var1);
5489 return FAIL;
5490 }
5491 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5492 {
5493 /* not a number or string */
5494 if (!empty1)
5495 clear_tv(&var1);
5496 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 return FAIL;
5498 }
5499 }
5500
5501 /* Check for the ']'. */
5502 if (**arg != ']')
5503 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 if (verbose)
5505 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005506 clear_tv(&var1);
5507 if (range)
5508 clear_tv(&var2);
5509 return FAIL;
5510 }
5511 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 }
5513
5514 if (evaluate)
5515 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005516 n1 = 0;
5517 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 n1 = get_tv_number(&var1);
5520 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 }
5522 if (range)
5523 {
5524 if (empty2)
5525 n2 = -1;
5526 else
5527 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005528 n2 = get_tv_number(&var2);
5529 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 }
5531 }
5532
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005535 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005536 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005537 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005538 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005539 case VAR_SPECIAL:
5540 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005541 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005542 break; /* not evaluating, skipping over subscript */
5543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005544 case VAR_NUMBER:
5545 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 len = (long)STRLEN(s);
5548 if (range)
5549 {
5550 /* The resulting variable is a substring. If the indexes
5551 * are out of range the result is empty. */
5552 if (n1 < 0)
5553 {
5554 n1 = len + n1;
5555 if (n1 < 0)
5556 n1 = 0;
5557 }
5558 if (n2 < 0)
5559 n2 = len + n2;
5560 else if (n2 >= len)
5561 n2 = len;
5562 if (n1 >= len || n2 < 0 || n1 > n2)
5563 s = NULL;
5564 else
5565 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5566 }
5567 else
5568 {
5569 /* The resulting variable is a string of a single
5570 * character. If the index is too big or negative the
5571 * result is empty. */
5572 if (n1 >= len || n1 < 0)
5573 s = NULL;
5574 else
5575 s = vim_strnsave(s + n1, 1);
5576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 clear_tv(rettv);
5578 rettv->v_type = VAR_STRING;
5579 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 break;
5581
5582 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 if (n1 < 0)
5585 n1 = len + n1;
5586 if (!empty1 && (n1 < 0 || n1 >= len))
5587 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005588 /* For a range we allow invalid values and return an empty
5589 * list. A list index out of range is an error. */
5590 if (!range)
5591 {
5592 if (verbose)
5593 EMSGN(_(e_listidx), n1);
5594 return FAIL;
5595 }
5596 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 }
5598 if (range)
5599 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005600 list_T *l;
5601 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005602
5603 if (n2 < 0)
5604 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005605 else if (n2 >= len)
5606 n2 = len - 1;
5607 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005608 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 l = list_alloc();
5610 if (l == NULL)
5611 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005612 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005613 n1 <= n2; ++n1)
5614 {
5615 if (list_append_tv(l, &item->li_tv) == FAIL)
5616 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 return FAIL;
5619 }
5620 item = item->li_next;
5621 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 clear_tv(rettv);
5623 rettv->v_type = VAR_LIST;
5624 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005625 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 }
5627 else
5628 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005629 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005630 clear_tv(rettv);
5631 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005634
5635 case VAR_DICT:
5636 if (range)
5637 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005638 if (verbose)
5639 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005640 if (len == -1)
5641 clear_tv(&var1);
5642 return FAIL;
5643 }
5644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005646
5647 if (len == -1)
5648 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005649 key = get_tv_string_chk(&var1);
5650 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005651 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005652 clear_tv(&var1);
5653 return FAIL;
5654 }
5655 }
5656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005657 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005658
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005659 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005660 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005661 if (len == -1)
5662 clear_tv(&var1);
5663 if (item == NULL)
5664 return FAIL;
5665
5666 copy_tv(&item->di_tv, &var1);
5667 clear_tv(rettv);
5668 *rettv = var1;
5669 }
5670 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671 }
5672 }
5673
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674 return OK;
5675}
5676
5677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 * Get an option value.
5679 * "arg" points to the '&' or '+' before the option name.
5680 * "arg" is advanced to character after the option name.
5681 * Return OK or FAIL.
5682 */
5683 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684get_option_tv(
5685 char_u **arg,
5686 typval_T *rettv, /* when NULL, only check if option exists */
5687 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688{
5689 char_u *option_end;
5690 long numval;
5691 char_u *stringval;
5692 int opt_type;
5693 int c;
5694 int working = (**arg == '+'); /* has("+option") */
5695 int ret = OK;
5696 int opt_flags;
5697
5698 /*
5699 * Isolate the option name and find its value.
5700 */
5701 option_end = find_option_end(arg, &opt_flags);
5702 if (option_end == NULL)
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 EMSG2(_("E112: Option name missing: %s"), *arg);
5706 return FAIL;
5707 }
5708
5709 if (!evaluate)
5710 {
5711 *arg = option_end;
5712 return OK;
5713 }
5714
5715 c = *option_end;
5716 *option_end = NUL;
5717 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005718 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719
5720 if (opt_type == -3) /* invalid name */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 EMSG2(_("E113: Unknown option: %s"), *arg);
5724 ret = FAIL;
5725 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (opt_type == -2) /* hidden string option */
5729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005730 rettv->v_type = VAR_STRING;
5731 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733 else if (opt_type == -1) /* hidden number option */
5734 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005735 rettv->v_type = VAR_NUMBER;
5736 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738 else if (opt_type == 1) /* number option */
5739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740 rettv->v_type = VAR_NUMBER;
5741 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 }
5743 else /* string option */
5744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005745 rettv->v_type = VAR_STRING;
5746 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748 }
5749 else if (working && (opt_type == -2 || opt_type == -1))
5750 ret = FAIL;
5751
5752 *option_end = c; /* put back for error messages */
5753 *arg = option_end;
5754
5755 return ret;
5756}
5757
5758/*
5759 * Allocate a variable for a string constant.
5760 * Return OK or FAIL.
5761 */
5762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 char_u *p;
5766 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 int extra = 0;
5768
5769 /*
5770 * Find the end of the string, skipping backslashed characters.
5771 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005772 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
5774 if (*p == '\\' && p[1] != NUL)
5775 {
5776 ++p;
5777 /* A "\<x>" form occupies at least 4 characters, and produces up
5778 * to 6 characters: reserve space for 2 extra */
5779 if (*p == '<')
5780 extra += 2;
5781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 }
5783
5784 if (*p != '"')
5785 {
5786 EMSG2(_("E114: Missing quote: %s"), *arg);
5787 return FAIL;
5788 }
5789
5790 /* If only parsing, set *arg and return here */
5791 if (!evaluate)
5792 {
5793 *arg = p + 1;
5794 return OK;
5795 }
5796
5797 /*
5798 * Copy the string into allocated memory, handling backslashed
5799 * characters.
5800 */
5801 name = alloc((unsigned)(p - *arg + extra));
5802 if (name == NULL)
5803 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005804 rettv->v_type = VAR_STRING;
5805 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806
Bram Moolenaar8c711452005-01-14 21:53:12 +00005807 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 {
5809 if (*p == '\\')
5810 {
5811 switch (*++p)
5812 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case 'b': *name++ = BS; ++p; break;
5814 case 'e': *name++ = ESC; ++p; break;
5815 case 'f': *name++ = FF; ++p; break;
5816 case 'n': *name++ = NL; ++p; break;
5817 case 'r': *name++ = CAR; ++p; break;
5818 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819
5820 case 'X': /* hex: "\x1", "\x12" */
5821 case 'x':
5822 case 'u': /* Unicode: "\u0023" */
5823 case 'U':
5824 if (vim_isxdigit(p[1]))
5825 {
5826 int n, nr;
5827 int c = toupper(*p);
5828
5829 if (c == 'X')
5830 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005831 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005833 else
5834 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 nr = 0;
5836 while (--n >= 0 && vim_isxdigit(p[1]))
5837 {
5838 ++p;
5839 nr = (nr << 4) + hex2nr(*p);
5840 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#ifdef FEAT_MBYTE
5843 /* For "\u" store the number according to
5844 * 'encoding'. */
5845 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 else
5848#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 break;
5852
5853 /* octal: "\1", "\12", "\123" */
5854 case '0':
5855 case '1':
5856 case '2':
5857 case '3':
5858 case '4':
5859 case '5':
5860 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 case '7': *name = *p++ - '0';
5862 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 *name = (*name << 3) + *p++ - '0';
5865 if (*p >= '0' && *p <= '7')
5866 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 break;
5870
5871 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (extra != 0)
5874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 break;
5877 }
5878 /* FALLTHROUGH */
5879
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 break;
5882 }
5883 }
5884 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 *arg = p + 1;
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 return OK;
5892}
5893
5894/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 * Return OK or FAIL.
5897 */
5898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005899get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 int reduce = 0;
5904
5905 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005906 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005907 */
5908 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
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 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 break;
5914 ++reduce;
5915 ++p;
5916 }
5917 }
5918
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005921 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 return FAIL;
5923 }
5924
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 if (!evaluate)
5927 {
5928 *arg = p + 1;
5929 return OK;
5930 }
5931
5932 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005933 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005934 */
5935 str = alloc((unsigned)((p - *arg) - reduce));
5936 if (str == NULL)
5937 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005938 rettv->v_type = VAR_STRING;
5939 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005943 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 break;
5947 ++p;
5948 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005950 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005952 *arg = p + 1;
5953
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 return OK;
5955}
5956
Bram Moolenaarddecc252016-04-06 22:59:37 +02005957 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005958partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005959{
5960 int i;
5961
5962 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005963 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005964 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966 func_unref(pt->pt_name);
5967 vim_free(pt->pt_name);
5968 vim_free(pt);
5969}
5970
5971/*
5972 * Unreference a closure: decrement the reference count and free it when it
5973 * becomes zero.
5974 */
5975 void
5976partial_unref(partial_T *pt)
5977{
5978 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005979 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005980}
5981
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005982/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 * Allocate a variable for a List and fill it from "*arg".
5984 * Return OK or FAIL.
5985 */
5986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005987get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988{
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l = NULL;
5990 typval_T tv;
5991 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992
5993 if (evaluate)
5994 {
5995 l = list_alloc();
5996 if (l == NULL)
5997 return FAIL;
5998 }
5999
6000 *arg = skipwhite(*arg + 1);
6001 while (**arg != ']' && **arg != NUL)
6002 {
6003 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6004 goto failret;
6005 if (evaluate)
6006 {
6007 item = listitem_alloc();
6008 if (item != NULL)
6009 {
6010 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006011 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 list_append(l, item);
6013 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006014 else
6015 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 }
6017
6018 if (**arg == ']')
6019 break;
6020 if (**arg != ',')
6021 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023 goto failret;
6024 }
6025 *arg = skipwhite(*arg + 1);
6026 }
6027
6028 if (**arg != ']')
6029 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006030 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006031failret:
6032 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006033 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034 return FAIL;
6035 }
6036
6037 *arg = skipwhite(*arg + 1);
6038 if (evaluate)
6039 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 rettv->v_type = VAR_LIST;
6041 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 ++l->lv_refcount;
6043 }
6044
6045 return OK;
6046}
6047
6048/*
6049 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006050 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006055 list_T *l;
6056
6057 l = (list_T *)alloc_clear(sizeof(list_T));
6058 if (l != NULL)
6059 {
6060 /* Prepend the list to the list of lists for garbage collection. */
6061 if (first_list != NULL)
6062 first_list->lv_used_prev = l;
6063 l->lv_used_prev = NULL;
6064 l->lv_used_next = first_list;
6065 first_list = l;
6066 }
6067 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068}
6069
6070/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006071 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006072 * Returns OK or FAIL.
6073 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006076{
6077 list_T *l = list_alloc();
6078
6079 if (l == NULL)
6080 return FAIL;
6081
6082 rettv->vval.v_list = l;
6083 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006084 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006085 ++l->lv_refcount;
6086 return OK;
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Unreference a list: decrement the reference count and free it when it
6091 * becomes zero.
6092 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006094list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006096 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006097 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006101 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006102 * Ignores the reference count.
6103 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006104 static void
6105list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006106{
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006109 for (item = l->lv_first; item != NULL; item = l->lv_first)
6110 {
6111 /* Remove the item before deleting it. */
6112 l->lv_first = item->li_next;
6113 clear_tv(&item->li_tv);
6114 vim_free(item);
6115 }
6116}
6117
6118 static void
6119list_free_list(list_T *l)
6120{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006121 /* Remove the list from the list of lists for garbage collection. */
6122 if (l->lv_used_prev == NULL)
6123 first_list = l->lv_used_next;
6124 else
6125 l->lv_used_prev->lv_used_next = l->lv_used_next;
6126 if (l->lv_used_next != NULL)
6127 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6128
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006129 vim_free(l);
6130}
6131
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006132 void
6133list_free(list_T *l)
6134{
6135 if (!in_free_unref_items)
6136 {
6137 list_free_contents(l);
6138 list_free_list(l);
6139 }
6140}
6141
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006142/*
6143 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006144 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006146 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006147listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006148{
Bram Moolenaar33570922005-01-25 22:26:29 +00006149 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006150}
6151
6152/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006153 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006156listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 vim_free(item);
6160}
6161
6162/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006163 * Remove a list item from a List and free it. Also clears the value.
6164 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006166listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006167{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006168 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006169 listitem_free(item);
6170}
6171
6172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006173 * Get the number of items in a list.
6174 */
6175 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006178 if (l == NULL)
6179 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006180 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006181}
6182
6183/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184 * Return TRUE when two lists have exactly the same values.
6185 */
6186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187list_equal(
6188 list_T *l1,
6189 list_T *l2,
6190 int ic, /* ignore case for strings */
6191 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192{
Bram Moolenaar33570922005-01-25 22:26:29 +00006193 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006195 if (l1 == NULL || l2 == NULL)
6196 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 if (l1 == l2)
6198 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006199 if (list_len(l1) != list_len(l2))
6200 return FALSE;
6201
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006202 for (item1 = l1->lv_first, item2 = l2->lv_first;
6203 item1 != NULL && item2 != NULL;
6204 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206 return FALSE;
6207 return item1 == NULL && item2 == NULL;
6208}
6209
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006210/*
6211 * Return the dictitem that an entry in a hashtable points to.
6212 */
6213 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006214dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006215{
6216 return HI2DI(hi);
6217}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006218
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006219/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006220 * Return TRUE when two dictionaries have exactly the same key/values.
6221 */
6222 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006223dict_equal(
6224 dict_T *d1,
6225 dict_T *d2,
6226 int ic, /* ignore case for strings */
6227 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228{
Bram Moolenaar33570922005-01-25 22:26:29 +00006229 hashitem_T *hi;
6230 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006231 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006232
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006233 if (d1 == NULL || d2 == NULL)
6234 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006235 if (d1 == d2)
6236 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006237 if (dict_len(d1) != dict_len(d2))
6238 return FALSE;
6239
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006240 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006242 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006243 if (!HASHITEM_EMPTY(hi))
6244 {
6245 item2 = dict_find(d2, hi->hi_key, -1);
6246 if (item2 == NULL)
6247 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006249 return FALSE;
6250 --todo;
6251 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252 }
6253 return TRUE;
6254}
6255
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006256static int tv_equal_recurse_limit;
6257
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006258/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006259 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006260 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006261 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006262 */
6263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006264tv_equal(
6265 typval_T *tv1,
6266 typval_T *tv2,
6267 int ic, /* ignore case */
6268 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006269{
6270 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006271 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006272 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006273 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006274
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006275 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6276 if ((tv1->v_type == VAR_FUNC
6277 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6278 && (tv2->v_type == VAR_FUNC
6279 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6280 {
6281 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6282 : tv1->vval.v_partial->pt_name;
6283 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6284 : tv2->vval.v_partial->pt_name;
6285 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6286 }
6287
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006288 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006289 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006290
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006291 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006292 * recursiveness to a limit. We guess they are equal then.
6293 * A fixed limit has the problem of still taking an awful long time.
6294 * Reduce the limit every time running into it. That should work fine for
6295 * deeply linked structures that are not recursively linked and catch
6296 * recursiveness quickly. */
6297 if (!recursive)
6298 tv_equal_recurse_limit = 1000;
6299 if (recursive_cnt >= tv_equal_recurse_limit)
6300 {
6301 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006302 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006303 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006304
6305 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006306 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006307 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006308 ++recursive_cnt;
6309 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6310 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006311 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006312
6313 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006314 ++recursive_cnt;
6315 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6316 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006317 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006318
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006319 case VAR_NUMBER:
6320 return tv1->vval.v_number == tv2->vval.v_number;
6321
6322 case VAR_STRING:
6323 s1 = get_tv_string_buf(tv1, buf1);
6324 s2 = get_tv_string_buf(tv2, buf2);
6325 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006326
6327 case VAR_SPECIAL:
6328 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006329
6330 case VAR_FLOAT:
6331#ifdef FEAT_FLOAT
6332 return tv1->vval.v_float == tv2->vval.v_float;
6333#endif
6334 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006336 return tv1->vval.v_job == tv2->vval.v_job;
6337#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006338 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006340 return tv1->vval.v_channel == tv2->vval.v_channel;
6341#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006342 case VAR_FUNC:
6343 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006344 case VAR_UNKNOWN:
6345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006346 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006347
Bram Moolenaara03f2332016-02-06 18:09:59 +01006348 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6349 * does not equal anything, not even itself. */
6350 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006351}
6352
6353/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 * Locate item with index "n" in list "l" and return it.
6355 * A negative index is counted from the end; -1 is the last item.
6356 * Returns NULL when "n" is out of range.
6357 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006358 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006360{
Bram Moolenaar33570922005-01-25 22:26:29 +00006361 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006362 long idx;
6363
6364 if (l == NULL)
6365 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006366
6367 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006368 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006369 n = l->lv_len + n;
6370
6371 /* Check for index out of range. */
6372 if (n < 0 || n >= l->lv_len)
6373 return NULL;
6374
6375 /* When there is a cached index may start search from there. */
6376 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006378 if (n < l->lv_idx / 2)
6379 {
6380 /* closest to the start of the list */
6381 item = l->lv_first;
6382 idx = 0;
6383 }
6384 else if (n > (l->lv_idx + l->lv_len) / 2)
6385 {
6386 /* closest to the end of the list */
6387 item = l->lv_last;
6388 idx = l->lv_len - 1;
6389 }
6390 else
6391 {
6392 /* closest to the cached index */
6393 item = l->lv_idx_item;
6394 idx = l->lv_idx;
6395 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 }
6397 else
6398 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006399 if (n < l->lv_len / 2)
6400 {
6401 /* closest to the start of the list */
6402 item = l->lv_first;
6403 idx = 0;
6404 }
6405 else
6406 {
6407 /* closest to the end of the list */
6408 item = l->lv_last;
6409 idx = l->lv_len - 1;
6410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006412
6413 while (n > idx)
6414 {
6415 /* search forward */
6416 item = item->li_next;
6417 ++idx;
6418 }
6419 while (n < idx)
6420 {
6421 /* search backward */
6422 item = item->li_prev;
6423 --idx;
6424 }
6425
6426 /* cache the used index */
6427 l->lv_idx = idx;
6428 l->lv_idx_item = item;
6429
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 return item;
6431}
6432
6433/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006434 * Get list item "l[idx]" as a number.
6435 */
6436 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006437list_find_nr(
6438 list_T *l,
6439 long idx,
6440 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006441{
6442 listitem_T *li;
6443
6444 li = list_find(l, idx);
6445 if (li == NULL)
6446 {
6447 if (errorp != NULL)
6448 *errorp = TRUE;
6449 return -1L;
6450 }
6451 return get_tv_number_chk(&li->li_tv, errorp);
6452}
6453
6454/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6456 */
6457 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006458list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006459{
6460 listitem_T *li;
6461
6462 li = list_find(l, idx - 1);
6463 if (li == NULL)
6464 {
6465 EMSGN(_(e_listidx), idx);
6466 return NULL;
6467 }
6468 return get_tv_string(&li->li_tv);
6469}
6470
6471/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006472 * Locate "item" list "l" and return its index.
6473 * Returns -1 when "item" is not in the list.
6474 */
6475 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006477{
6478 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006479 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006480
6481 if (l == NULL)
6482 return -1;
6483 idx = 0;
6484 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6485 ++idx;
6486 if (li == NULL)
6487 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006488 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006489}
6490
6491/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Append item "item" to the end of list "l".
6493 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006494 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006495list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006496{
6497 if (l->lv_last == NULL)
6498 {
6499 /* empty list */
6500 l->lv_first = item;
6501 l->lv_last = item;
6502 item->li_prev = NULL;
6503 }
6504 else
6505 {
6506 l->lv_last->li_next = item;
6507 item->li_prev = l->lv_last;
6508 l->lv_last = item;
6509 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006510 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006511 item->li_next = NULL;
6512}
6513
6514/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006515 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006516 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006519list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006521 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522
Bram Moolenaar05159a02005-02-26 23:04:13 +00006523 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006524 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006525 copy_tv(tv, &li->li_tv);
6526 list_append(l, li);
6527 return OK;
6528}
6529
6530/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006531 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006532 * Return FAIL when out of memory.
6533 */
6534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006535list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006536{
6537 listitem_T *li = listitem_alloc();
6538
6539 if (li == NULL)
6540 return FAIL;
6541 li->li_tv.v_type = VAR_DICT;
6542 li->li_tv.v_lock = 0;
6543 li->li_tv.vval.v_dict = dict;
6544 list_append(list, li);
6545 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 return OK;
6547}
6548
6549/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006550 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006551 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006552 * Returns FAIL when out of memory.
6553 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006554 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006556{
6557 listitem_T *li = listitem_alloc();
6558
6559 if (li == NULL)
6560 return FAIL;
6561 list_append(l, li);
6562 li->li_tv.v_type = VAR_STRING;
6563 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006564 if (str == NULL)
6565 li->li_tv.vval.v_string = NULL;
6566 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006567 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006568 return FAIL;
6569 return OK;
6570}
6571
6572/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006573 * Append "n" to list "l".
6574 * Returns FAIL when out of memory.
6575 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006576 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006577list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006578{
6579 listitem_T *li;
6580
6581 li = listitem_alloc();
6582 if (li == NULL)
6583 return FAIL;
6584 li->li_tv.v_type = VAR_NUMBER;
6585 li->li_tv.v_lock = 0;
6586 li->li_tv.vval.v_number = n;
6587 list_append(l, li);
6588 return OK;
6589}
6590
6591/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593 * If "item" is NULL append at the end.
6594 * Return FAIL when out of memory.
6595 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006596 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006597list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598{
Bram Moolenaar33570922005-01-25 22:26:29 +00006599 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600
6601 if (ni == NULL)
6602 return FAIL;
6603 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006604 list_insert(l, ni, item);
6605 return OK;
6606}
6607
6608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006609list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006610{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006611 if (item == NULL)
6612 /* Append new item at end of list. */
6613 list_append(l, ni);
6614 else
6615 {
6616 /* Insert new item before existing item. */
6617 ni->li_prev = item->li_prev;
6618 ni->li_next = item;
6619 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006620 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006621 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006622 ++l->lv_idx;
6623 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006624 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006625 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006626 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006627 l->lv_idx_item = NULL;
6628 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006629 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006630 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006631 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006632}
6633
6634/*
6635 * Extend "l1" with "l2".
6636 * If "bef" is NULL append at the end, otherwise insert before this item.
6637 * Returns FAIL when out of memory.
6638 */
6639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006640list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641{
Bram Moolenaar33570922005-01-25 22:26:29 +00006642 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006643 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006644
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006645 /* We also quit the loop when we have inserted the original item count of
6646 * the list, avoid a hang when we extend a list with itself. */
6647 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6649 return FAIL;
6650 return OK;
6651}
6652
6653/*
6654 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6655 * Return FAIL when out of memory.
6656 */
6657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006658list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659{
Bram Moolenaar33570922005-01-25 22:26:29 +00006660 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006662 if (l1 == NULL || l2 == NULL)
6663 return FAIL;
6664
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006666 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 if (l == NULL)
6668 return FAIL;
6669 tv->v_type = VAR_LIST;
6670 tv->vval.v_list = l;
6671
6672 /* append all items from the second list */
6673 return list_extend(l, l2, NULL);
6674}
6675
6676/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006677 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006679 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 * Returns NULL when out of memory.
6681 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006682 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006683list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684{
Bram Moolenaar33570922005-01-25 22:26:29 +00006685 list_T *copy;
6686 listitem_T *item;
6687 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006688
6689 if (orig == NULL)
6690 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691
6692 copy = list_alloc();
6693 if (copy != NULL)
6694 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 if (copyID != 0)
6696 {
6697 /* Do this before adding the items, because one of the items may
6698 * refer back to this list. */
6699 orig->lv_copyID = copyID;
6700 orig->lv_copylist = copy;
6701 }
6702 for (item = orig->lv_first; item != NULL && !got_int;
6703 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 {
6705 ni = listitem_alloc();
6706 if (ni == NULL)
6707 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006708 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006709 {
6710 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6711 {
6712 vim_free(ni);
6713 break;
6714 }
6715 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006717 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006718 list_append(copy, ni);
6719 }
6720 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006721 if (item != NULL)
6722 {
6723 list_unref(copy);
6724 copy = NULL;
6725 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006726 }
6727
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006728 return copy;
6729}
6730
6731/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006733 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006734 * This used to be called list_remove, but that conflicts with a Sun header
6735 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006736 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006739{
Bram Moolenaar33570922005-01-25 22:26:29 +00006740 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006741
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006742 /* notify watchers */
6743 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006744 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006745 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006746 list_fix_watch(l, ip);
6747 if (ip == item2)
6748 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006750
6751 if (item2->li_next == NULL)
6752 l->lv_last = item->li_prev;
6753 else
6754 item2->li_next->li_prev = item->li_prev;
6755 if (item->li_prev == NULL)
6756 l->lv_first = item2->li_next;
6757 else
6758 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006759 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006760}
6761
6762/*
6763 * Return an allocated string with the string representation of a list.
6764 * May return NULL.
6765 */
6766 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006768{
6769 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770
6771 if (tv->vval.v_list == NULL)
6772 return NULL;
6773 ga_init2(&ga, (int)sizeof(char), 80);
6774 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006775 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006776 {
6777 vim_free(ga.ga_data);
6778 return NULL;
6779 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006780 ga_append(&ga, ']');
6781 ga_append(&ga, NUL);
6782 return (char_u *)ga.ga_data;
6783}
6784
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785typedef struct join_S {
6786 char_u *s;
6787 char_u *tofree;
6788} join_T;
6789
6790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006791list_join_inner(
6792 garray_T *gap, /* to store the result in */
6793 list_T *l,
6794 char_u *sep,
6795 int echo_style,
6796 int copyID,
6797 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798{
6799 int i;
6800 join_T *p;
6801 int len;
6802 int sumlen = 0;
6803 int first = TRUE;
6804 char_u *tofree;
6805 char_u numbuf[NUMBUFLEN];
6806 listitem_T *item;
6807 char_u *s;
6808
6809 /* Stringify each item in the list. */
6810 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6811 {
6812 if (echo_style)
6813 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6814 else
6815 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6816 if (s == NULL)
6817 return FAIL;
6818
6819 len = (int)STRLEN(s);
6820 sumlen += len;
6821
Bram Moolenaarcde88542015-08-11 19:14:00 +02006822 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006823 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6824 if (tofree != NULL || s != numbuf)
6825 {
6826 p->s = s;
6827 p->tofree = tofree;
6828 }
6829 else
6830 {
6831 p->s = vim_strnsave(s, len);
6832 p->tofree = p->s;
6833 }
6834
6835 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006836 if (did_echo_string_emsg) /* recursion error, bail out */
6837 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006838 }
6839
6840 /* Allocate result buffer with its total size, avoid re-allocation and
6841 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6842 if (join_gap->ga_len >= 2)
6843 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6844 if (ga_grow(gap, sumlen + 2) == FAIL)
6845 return FAIL;
6846
6847 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6848 {
6849 if (first)
6850 first = FALSE;
6851 else
6852 ga_concat(gap, sep);
6853 p = ((join_T *)join_gap->ga_data) + i;
6854
6855 if (p->s != NULL)
6856 ga_concat(gap, p->s);
6857 line_breakcheck();
6858 }
6859
6860 return OK;
6861}
6862
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006863/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006864 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006865 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006866 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006867 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006869list_join(
6870 garray_T *gap,
6871 list_T *l,
6872 char_u *sep,
6873 int echo_style,
6874 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006875{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006876 garray_T join_ga;
6877 int retval;
6878 join_T *p;
6879 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006880
Bram Moolenaard39a7512015-04-16 22:51:22 +02006881 if (l->lv_len < 1)
6882 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6884 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6885
6886 /* Dispose each item in join_ga. */
6887 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006888 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006889 p = (join_T *)join_ga.ga_data;
6890 for (i = 0; i < join_ga.ga_len; ++i)
6891 {
6892 vim_free(p->tofree);
6893 ++p;
6894 }
6895 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006896 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006897
6898 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006899}
6900
6901/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006902 * Return the next (unique) copy ID.
6903 * Used for serializing nested structures.
6904 */
6905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006906get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006907{
6908 current_copyID += COPYID_INC;
6909 return current_copyID;
6910}
6911
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006912/* Used by get_func_tv() */
6913static garray_T funcargs = GA_EMPTY;
6914
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006915/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006916 * Garbage collection for lists and dictionaries.
6917 *
6918 * We use reference counts to be able to free most items right away when they
6919 * are no longer used. But for composite items it's possible that it becomes
6920 * unused while the reference count is > 0: When there is a recursive
6921 * reference. Example:
6922 * :let l = [1, 2, 3]
6923 * :let d = {9: l}
6924 * :let l[1] = d
6925 *
6926 * Since this is quite unusual we handle this with garbage collection: every
6927 * once in a while find out which lists and dicts are not referenced from any
6928 * variable.
6929 *
6930 * Here is a good reference text about garbage collection (refers to Python
6931 * but it applies to all reference-counting mechanisms):
6932 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006933 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006934
6935/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02006937 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006939 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006941garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006942{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 buf_T *buf;
6946 win_T *wp;
6947 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006948 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006949 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006950 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006951#ifdef FEAT_WINDOWS
6952 tabpage_T *tp;
6953#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006954
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006955 if (!testing)
6956 {
6957 /* Only do this once. */
6958 want_garbage_collect = FALSE;
6959 may_garbage_collect = FALSE;
6960 garbage_collect_at_exit = FALSE;
6961 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006962
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 /* We advance by two because we add one for items referenced through
6964 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006965 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /*
6968 * 1. Go through all accessible variables and mark all lists and dicts
6969 * with copyID.
6970 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006971
6972 /* Don't free variables in the previous_funccal list unless they are only
6973 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006974 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006975 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6976 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6978 NULL);
6979 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6980 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981 }
6982
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006983 /* script-local variables */
6984 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986
6987 /* buffer-local variables */
6988 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6990 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991
6992 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6995 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006996#ifdef FEAT_AUTOCMD
6997 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006998 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6999 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007000#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007001
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007002#ifdef FEAT_WINDOWS
7003 /* tabpage-local variables */
7004 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007005 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7006 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007007#endif
7008
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007009 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007010 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011
7012 /* function-local variables */
7013 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7014 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007015 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7016 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
7018
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007019 /* function call arguments, if v:testing is set. */
7020 for (i = 0; i < funcargs.ga_len; ++i)
7021 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7022 copyID, NULL, NULL);
7023
Bram Moolenaard812df62008-11-09 12:46:09 +00007024 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007025 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007026
Bram Moolenaar1dced572012-04-05 16:54:08 +02007027#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007029#endif
7030
Bram Moolenaardb913952012-06-29 12:54:53 +02007031#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007033#endif
7034
7035#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007037#endif
7038
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007039#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007040 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007041 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007042#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007043#ifdef FEAT_NETBEANS_INTG
7044 abort = abort || set_ref_in_nb_channel(copyID);
7045#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007046
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007048 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007049 /*
7050 * 2. Free lists and dictionaries that are not referenced.
7051 */
7052 did_free = free_unref_items(copyID);
7053
7054 /*
7055 * 3. Check if any funccal can be freed now.
7056 */
7057 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007058 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059 if (can_free_funccal(*pfc, copyID))
7060 {
7061 fc = *pfc;
7062 *pfc = fc->caller;
7063 free_funccal(fc, TRUE);
7064 did_free = TRUE;
7065 did_free_funccal = TRUE;
7066 }
7067 else
7068 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007069 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 if (did_free_funccal)
7071 /* When a funccal was freed some more items might be garbage
7072 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007073 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007074 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 else if (p_verbose > 0)
7076 {
7077 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7078 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007079
7080 return did_free;
7081}
7082
7083/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007084 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007085 */
7086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007087free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007088{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007089 dict_T *dd, *dd_next;
7090 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007091 int did_free = FALSE;
7092
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007093 /* Let all "free" functions know that we are here. This means no
7094 * dictionaries, lists, channels or jobs are to be freed, because we will
7095 * do that here. */
7096 in_free_unref_items = TRUE;
7097
7098 /*
7099 * PASS 1: free the contents of the items. We don't free the items
7100 * themselves yet, so that it is possible to decrement refcount counters
7101 */
7102
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007104 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007105 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007106 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007107 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007108 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007109 /* Free the Dictionary and ordinary items it contains, but don't
7110 * recurse into Lists and Dictionaries, they will be in the list
7111 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007112 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007114 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115
7116 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007117 * Go through the list of lists and free items without the copyID.
7118 * But don't free a list that has a watcher (used in a for loop), these
7119 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007120 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007121 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7122 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7123 && ll->lv_watch == NULL)
7124 {
7125 /* Free the List and ordinary items it contains, but don't recurse
7126 * into Lists and Dictionaries, they will be in the list of dicts
7127 * or list of lists. */
7128 list_free_contents(ll);
7129 did_free = TRUE;
7130 }
7131
7132#ifdef FEAT_JOB_CHANNEL
7133 /* Go through the list of jobs and free items without the copyID. This
7134 * must happen before doing channels, because jobs refer to channels, but
7135 * the reference from the channel to the job isn't tracked. */
7136 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7137
7138 /* Go through the list of channels and free items without the copyID. */
7139 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7140#endif
7141
7142 /*
7143 * PASS 2: free the items themselves.
7144 */
7145 for (dd = first_dict; dd != NULL; dd = dd_next)
7146 {
7147 dd_next = dd->dv_used_next;
7148 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7149 dict_free_dict(dd);
7150 }
7151
7152 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007153 {
7154 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007155 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7156 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007157 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007158 /* Free the List and ordinary items it contains, but don't recurse
7159 * into Lists and Dictionaries, they will be in the list of dicts
7160 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007161 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007162 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007163 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007164
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007165#ifdef FEAT_JOB_CHANNEL
7166 /* Go through the list of jobs and free items without the copyID. This
7167 * must happen before doing channels, because jobs refer to channels, but
7168 * the reference from the channel to the job isn't tracked. */
7169 free_unused_jobs(copyID, COPYID_MASK);
7170
7171 /* Go through the list of channels and free items without the copyID. */
7172 free_unused_channels(copyID, COPYID_MASK);
7173#endif
7174
7175 in_free_unref_items = FALSE;
7176
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177 return did_free;
7178}
7179
7180/*
7181 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 * "list_stack" is used to add lists to be marked. Can be NULL.
7183 *
7184 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007185 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007188{
7189 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007191 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007192 hashtab_T *cur_ht;
7193 ht_stack_T *ht_stack = NULL;
7194 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007196 cur_ht = ht;
7197 for (;;)
7198 {
7199 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007201 /* Mark each item in the hashtab. If the item contains a hashtab
7202 * it is added to ht_stack, if it contains a list it is added to
7203 * list_stack. */
7204 todo = (int)cur_ht->ht_used;
7205 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7206 if (!HASHITEM_EMPTY(hi))
7207 {
7208 --todo;
7209 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7210 &ht_stack, list_stack);
7211 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007212 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007213
7214 if (ht_stack == NULL)
7215 break;
7216
7217 /* take an item from the stack */
7218 cur_ht = ht_stack->ht;
7219 tempitem = ht_stack;
7220 ht_stack = ht_stack->prev;
7221 free(tempitem);
7222 }
7223
7224 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007225}
7226
7227/*
7228 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007229 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7230 *
7231 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007233 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007235{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007236 listitem_T *li;
7237 int abort = FALSE;
7238 list_T *cur_l;
7239 list_stack_T *list_stack = NULL;
7240 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007242 cur_l = l;
7243 for (;;)
7244 {
7245 if (!abort)
7246 /* Mark each item in the list. If the item contains a hashtab
7247 * it is added to ht_stack, if it contains a list it is added to
7248 * list_stack. */
7249 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7250 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7251 ht_stack, &list_stack);
7252 if (list_stack == NULL)
7253 break;
7254
7255 /* take an item from the stack */
7256 cur_l = list_stack->list;
7257 tempitem = list_stack;
7258 list_stack = list_stack->prev;
7259 free(tempitem);
7260 }
7261
7262 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263}
7264
7265/*
7266 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007267 * "list_stack" is used to add lists to be marked. Can be NULL.
7268 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7269 *
7270 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007271 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007273set_ref_in_item(
7274 typval_T *tv,
7275 int copyID,
7276 ht_stack_T **ht_stack,
7277 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007278{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007279 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007280
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007281 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007282 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007283 dict_T *dd = tv->vval.v_dict;
7284
Bram Moolenaara03f2332016-02-06 18:09:59 +01007285 if (dd != NULL && dd->dv_copyID != copyID)
7286 {
7287 /* Didn't see this dict yet. */
7288 dd->dv_copyID = copyID;
7289 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007290 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007291 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7292 }
7293 else
7294 {
7295 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7296 if (newitem == NULL)
7297 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007298 else
7299 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007300 newitem->ht = &dd->dv_hashtab;
7301 newitem->prev = *ht_stack;
7302 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007303 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007304 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007305 }
7306 }
7307 else if (tv->v_type == VAR_LIST)
7308 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007309 list_T *ll = tv->vval.v_list;
7310
Bram Moolenaara03f2332016-02-06 18:09:59 +01007311 if (ll != NULL && ll->lv_copyID != copyID)
7312 {
7313 /* Didn't see this list yet. */
7314 ll->lv_copyID = copyID;
7315 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007316 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007317 abort = set_ref_in_list(ll, copyID, ht_stack);
7318 }
7319 else
7320 {
7321 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007322 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007323 if (newitem == NULL)
7324 abort = TRUE;
7325 else
7326 {
7327 newitem->list = ll;
7328 newitem->prev = *list_stack;
7329 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007330 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007331 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007332 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007333 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007334 else if (tv->v_type == VAR_PARTIAL)
7335 {
7336 partial_T *pt = tv->vval.v_partial;
7337 int i;
7338
7339 /* A partial does not have a copyID, because it cannot contain itself.
7340 */
7341 if (pt != NULL)
7342 {
7343 if (pt->pt_dict != NULL)
7344 {
7345 typval_T dtv;
7346
7347 dtv.v_type = VAR_DICT;
7348 dtv.vval.v_dict = pt->pt_dict;
7349 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7350 }
7351
7352 for (i = 0; i < pt->pt_argc; ++i)
7353 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7354 ht_stack, list_stack);
7355 }
7356 }
7357#ifdef FEAT_JOB_CHANNEL
7358 else if (tv->v_type == VAR_JOB)
7359 {
7360 job_T *job = tv->vval.v_job;
7361 typval_T dtv;
7362
7363 if (job != NULL && job->jv_copyID != copyID)
7364 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007365 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007366 if (job->jv_channel != NULL)
7367 {
7368 dtv.v_type = VAR_CHANNEL;
7369 dtv.vval.v_channel = job->jv_channel;
7370 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7371 }
7372 if (job->jv_exit_partial != NULL)
7373 {
7374 dtv.v_type = VAR_PARTIAL;
7375 dtv.vval.v_partial = job->jv_exit_partial;
7376 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7377 }
7378 }
7379 }
7380 else if (tv->v_type == VAR_CHANNEL)
7381 {
7382 channel_T *ch =tv->vval.v_channel;
7383 int part;
7384 typval_T dtv;
7385 jsonq_T *jq;
7386 cbq_T *cq;
7387
7388 if (ch != NULL && ch->ch_copyID != copyID)
7389 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007390 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007391 for (part = PART_SOCK; part <= PART_IN; ++part)
7392 {
7393 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7394 jq = jq->jq_next)
7395 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7396 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7397 cq = cq->cq_next)
7398 if (cq->cq_partial != NULL)
7399 {
7400 dtv.v_type = VAR_PARTIAL;
7401 dtv.vval.v_partial = cq->cq_partial;
7402 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7403 }
7404 if (ch->ch_part[part].ch_partial != NULL)
7405 {
7406 dtv.v_type = VAR_PARTIAL;
7407 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7408 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7409 }
7410 }
7411 if (ch->ch_partial != NULL)
7412 {
7413 dtv.v_type = VAR_PARTIAL;
7414 dtv.vval.v_partial = ch->ch_partial;
7415 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7416 }
7417 if (ch->ch_close_partial != NULL)
7418 {
7419 dtv.v_type = VAR_PARTIAL;
7420 dtv.vval.v_partial = ch->ch_close_partial;
7421 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7422 }
7423 }
7424 }
7425#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007426 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007427}
7428
7429/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 * Allocate an empty header for a dictionary.
7431 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007432 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007433dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434{
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007436
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007438 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007439 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007440 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007441 if (first_dict != NULL)
7442 first_dict->dv_used_prev = d;
7443 d->dv_used_next = first_dict;
7444 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007445 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007446
Bram Moolenaar33570922005-01-25 22:26:29 +00007447 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007448 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007449 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007450 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007451 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007452 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007453 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454}
7455
7456/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007457 * Allocate an empty dict for a return value.
7458 * Returns OK or FAIL.
7459 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007461rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007462{
7463 dict_T *d = dict_alloc();
7464
7465 if (d == NULL)
7466 return FAIL;
7467
7468 rettv->vval.v_dict = d;
7469 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007470 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007471 ++d->dv_refcount;
7472 return OK;
7473}
7474
7475
7476/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007477 * Unreference a Dictionary: decrement the reference count and free it when it
7478 * becomes zero.
7479 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007483 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007484 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485}
7486
7487/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007488 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 * Ignores the reference count.
7490 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007491 static void
7492dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007496 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007498 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007499 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007500 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007501 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503 if (!HASHITEM_EMPTY(hi))
7504 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007505 /* Remove the item before deleting it, just in case there is
7506 * something recursive causing trouble. */
7507 di = HI2DI(hi);
7508 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007509 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007510 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 --todo;
7512 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007515}
7516
7517 static void
7518dict_free_dict(dict_T *d)
7519{
7520 /* Remove the dict from the list of dicts for garbage collection. */
7521 if (d->dv_used_prev == NULL)
7522 first_dict = d->dv_used_next;
7523 else
7524 d->dv_used_prev->dv_used_next = d->dv_used_next;
7525 if (d->dv_used_next != NULL)
7526 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 vim_free(d);
7528}
7529
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007530 void
7531dict_free(dict_T *d)
7532{
7533 if (!in_free_unref_items)
7534 {
7535 dict_free_contents(d);
7536 dict_free_dict(d);
7537 }
7538}
7539
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540/*
7541 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007542 * The "key" is copied to the new item.
7543 * Note that the value of the item "di_tv" still needs to be initialized!
7544 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007546 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007547dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007548{
Bram Moolenaar33570922005-01-25 22:26:29 +00007549 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007551 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007552 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007555 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007556 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007558}
7559
7560/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007561 * Make a copy of a Dictionary item.
7562 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007564dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565{
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007567
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007568 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7569 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007570 if (di != NULL)
7571 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007573 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007574 copy_tv(&org->di_tv, &di->di_tv);
7575 }
7576 return di;
7577}
7578
7579/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 * Remove item "item" from Dictionary "dict" and free it.
7581 */
7582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007583dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584{
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 if (HASHITEM_EMPTY(hi))
7589 EMSG2(_(e_intern2), "dictitem_remove()");
7590 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 dictitem_free(item);
7593}
7594
7595/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596 * Free a dict item. Also clears the value.
7597 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007599dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007602 if (item->di_flags & DI_FLAGS_ALLOC)
7603 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604}
7605
7606/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007607 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7608 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007610 * Returns NULL when out of memory.
7611 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007612 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007613dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007614{
Bram Moolenaar33570922005-01-25 22:26:29 +00007615 dict_T *copy;
7616 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007617 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007618 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619
7620 if (orig == NULL)
7621 return NULL;
7622
7623 copy = dict_alloc();
7624 if (copy != NULL)
7625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007626 if (copyID != 0)
7627 {
7628 orig->dv_copyID = copyID;
7629 orig->dv_copydict = copy;
7630 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007631 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007632 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007634 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007635 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 --todo;
7637
7638 di = dictitem_alloc(hi->hi_key);
7639 if (di == NULL)
7640 break;
7641 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007642 {
7643 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7644 copyID) == FAIL)
7645 {
7646 vim_free(di);
7647 break;
7648 }
7649 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007650 else
7651 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7652 if (dict_add(copy, di) == FAIL)
7653 {
7654 dictitem_free(di);
7655 break;
7656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007658 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659
Bram Moolenaare9a41262005-01-15 22:18:47 +00007660 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007661 if (todo > 0)
7662 {
7663 dict_unref(copy);
7664 copy = NULL;
7665 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007666 }
7667
7668 return copy;
7669}
7670
7671/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007672 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007673 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007674 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007675 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007676dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007677{
Bram Moolenaar33570922005-01-25 22:26:29 +00007678 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679}
7680
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007682 * Add a number or string entry to dictionary "d".
7683 * When "str" is NULL use number "nr", otherwise use "str".
7684 * Returns FAIL when out of memory and when key already exists.
7685 */
7686 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007687dict_add_nr_str(
7688 dict_T *d,
7689 char *key,
7690 long nr,
7691 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007692{
7693 dictitem_T *item;
7694
7695 item = dictitem_alloc((char_u *)key);
7696 if (item == NULL)
7697 return FAIL;
7698 item->di_tv.v_lock = 0;
7699 if (str == NULL)
7700 {
7701 item->di_tv.v_type = VAR_NUMBER;
7702 item->di_tv.vval.v_number = nr;
7703 }
7704 else
7705 {
7706 item->di_tv.v_type = VAR_STRING;
7707 item->di_tv.vval.v_string = vim_strsave(str);
7708 }
7709 if (dict_add(d, item) == FAIL)
7710 {
7711 dictitem_free(item);
7712 return FAIL;
7713 }
7714 return OK;
7715}
7716
7717/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007718 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007719 * Returns FAIL when out of memory and when key already exists.
7720 */
7721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007722dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007723{
7724 dictitem_T *item;
7725
7726 item = dictitem_alloc((char_u *)key);
7727 if (item == NULL)
7728 return FAIL;
7729 item->di_tv.v_lock = 0;
7730 item->di_tv.v_type = VAR_LIST;
7731 item->di_tv.vval.v_list = list;
7732 if (dict_add(d, item) == FAIL)
7733 {
7734 dictitem_free(item);
7735 return FAIL;
7736 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007737 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007738 return OK;
7739}
7740
7741/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007742 * Get the number of items in a Dictionary.
7743 */
7744 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007745dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007746{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007747 if (d == NULL)
7748 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007749 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007750}
7751
7752/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 * Find item "key[len]" in Dictionary "d".
7754 * If "len" is negative use strlen(key).
7755 * Returns NULL when not found.
7756 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007757 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007758dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760#define AKEYLEN 200
7761 char_u buf[AKEYLEN];
7762 char_u *akey;
7763 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007765
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766 if (len < 0)
7767 akey = key;
7768 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 tofree = akey = vim_strnsave(key, len);
7771 if (akey == NULL)
7772 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007773 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 else
7775 {
7776 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007777 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 akey = buf;
7779 }
7780
Bram Moolenaar33570922005-01-25 22:26:29 +00007781 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 vim_free(tofree);
7783 if (HASHITEM_EMPTY(hi))
7784 return NULL;
7785 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786}
7787
7788/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007789 * Get a string item from a dictionary.
7790 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007791 * Returns NULL if the entry doesn't exist or out of memory.
7792 */
7793 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007794get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007795{
7796 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007797 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007798
7799 di = dict_find(d, key, -1);
7800 if (di == NULL)
7801 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007802 s = get_tv_string(&di->di_tv);
7803 if (save && s != NULL)
7804 s = vim_strsave(s);
7805 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007806}
7807
7808/*
7809 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007810 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007811 */
7812 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007813get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007814{
7815 dictitem_T *di;
7816
7817 di = dict_find(d, key, -1);
7818 if (di == NULL)
7819 return 0;
7820 return get_tv_number(&di->di_tv);
7821}
7822
7823/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007824 * Return an allocated string with the string representation of a Dictionary.
7825 * May return NULL.
7826 */
7827 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007829{
7830 garray_T ga;
7831 int first = TRUE;
7832 char_u *tofree;
7833 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007835 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007837 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007838
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007839 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840 return NULL;
7841 ga_init2(&ga, (int)sizeof(char), 80);
7842 ga_append(&ga, '{');
7843
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007844 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007845 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007846 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007847 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007849 --todo;
7850
7851 if (first)
7852 first = FALSE;
7853 else
7854 ga_concat(&ga, (char_u *)", ");
7855
7856 tofree = string_quote(hi->hi_key, FALSE);
7857 if (tofree != NULL)
7858 {
7859 ga_concat(&ga, tofree);
7860 vim_free(tofree);
7861 }
7862 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007863 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007864 if (s != NULL)
7865 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007866 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007867 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007868 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007869 line_breakcheck();
7870
Bram Moolenaar8c711452005-01-14 21:53:12 +00007871 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007873 if (todo > 0)
7874 {
7875 vim_free(ga.ga_data);
7876 return NULL;
7877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878
7879 ga_append(&ga, '}');
7880 ga_append(&ga, NUL);
7881 return (char_u *)ga.ga_data;
7882}
7883
7884/*
7885 * Allocate a variable for a Dictionary and fill it from "*arg".
7886 * Return OK or FAIL. Returns NOTDONE for {expr}.
7887 */
7888 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007889get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007890{
Bram Moolenaar33570922005-01-25 22:26:29 +00007891 dict_T *d = NULL;
7892 typval_T tvkey;
7893 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007894 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007895 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007896 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007897 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007898
7899 /*
7900 * First check if it's not a curly-braces thing: {expr}.
7901 * Must do this without evaluating, otherwise a function may be called
7902 * twice. Unfortunately this means we need to call eval1() twice for the
7903 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007904 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007906 if (*start != '}')
7907 {
7908 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7909 return FAIL;
7910 if (*start == '}')
7911 return NOTDONE;
7912 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007913
7914 if (evaluate)
7915 {
7916 d = dict_alloc();
7917 if (d == NULL)
7918 return FAIL;
7919 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007920 tvkey.v_type = VAR_UNKNOWN;
7921 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007922
7923 *arg = skipwhite(*arg + 1);
7924 while (**arg != '}' && **arg != NUL)
7925 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007926 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 goto failret;
7928 if (**arg != ':')
7929 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007930 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007931 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 goto failret;
7933 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007934 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007935 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007936 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007937 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007938 {
7939 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007940 clear_tv(&tvkey);
7941 goto failret;
7942 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007944
7945 *arg = skipwhite(*arg + 1);
7946 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7947 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007948 if (evaluate)
7949 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007950 goto failret;
7951 }
7952 if (evaluate)
7953 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007954 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007955 if (item != NULL)
7956 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007957 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007958 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007959 clear_tv(&tv);
7960 goto failret;
7961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007962 item = dictitem_alloc(key);
7963 clear_tv(&tvkey);
7964 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007965 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007966 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007967 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007968 if (dict_add(d, item) == FAIL)
7969 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007970 }
7971 }
7972
7973 if (**arg == '}')
7974 break;
7975 if (**arg != ',')
7976 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007977 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 goto failret;
7979 }
7980 *arg = skipwhite(*arg + 1);
7981 }
7982
7983 if (**arg != '}')
7984 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007985 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007986failret:
7987 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007988 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007989 return FAIL;
7990 }
7991
7992 *arg = skipwhite(*arg + 1);
7993 if (evaluate)
7994 {
7995 rettv->v_type = VAR_DICT;
7996 rettv->vval.v_dict = d;
7997 ++d->dv_refcount;
7998 }
7999
8000 return OK;
8001}
8002
Bram Moolenaar17a13432016-01-24 14:22:10 +01008003 static char *
8004get_var_special_name(int nr)
8005{
8006 switch (nr)
8007 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008008 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008009 case VVAL_TRUE: return "v:true";
8010 case VVAL_NONE: return "v:none";
8011 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008012 }
8013 EMSG2(_(e_intern2), "get_var_special_name()");
8014 return "42";
8015}
8016
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008018 * Return a string with the string representation of a variable.
8019 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008020 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008022 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008023 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 */
8025 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008026echo_string(
8027 typval_T *tv,
8028 char_u **tofree,
8029 char_u *numbuf,
8030 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008032 static int recurse = 0;
8033 char_u *r = NULL;
8034
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008037 if (!did_echo_string_emsg)
8038 {
8039 /* Only give this message once for a recursive call to avoid
8040 * flooding the user with errors. And stop iterating over lists
8041 * and dicts. */
8042 did_echo_string_emsg = TRUE;
8043 EMSG(_("E724: variable nested too deep for displaying"));
8044 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008045 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008046 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008047 }
8048 ++recurse;
8049
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008050 switch (tv->v_type)
8051 {
8052 case VAR_FUNC:
8053 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008054 r = tv->vval.v_string;
8055 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008056
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008057 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008058 {
8059 partial_T *pt = tv->vval.v_partial;
8060 char_u *fname = string_quote(pt == NULL ? NULL
8061 : pt->pt_name, FALSE);
8062 garray_T ga;
8063 int i;
8064 char_u *tf;
8065
8066 ga_init2(&ga, 1, 100);
8067 ga_concat(&ga, (char_u *)"function(");
8068 if (fname != NULL)
8069 {
8070 ga_concat(&ga, fname);
8071 vim_free(fname);
8072 }
8073 if (pt != NULL && pt->pt_argc > 0)
8074 {
8075 ga_concat(&ga, (char_u *)", [");
8076 for (i = 0; i < pt->pt_argc; ++i)
8077 {
8078 if (i > 0)
8079 ga_concat(&ga, (char_u *)", ");
8080 ga_concat(&ga,
8081 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8082 vim_free(tf);
8083 }
8084 ga_concat(&ga, (char_u *)"]");
8085 }
8086 if (pt != NULL && pt->pt_dict != NULL)
8087 {
8088 typval_T dtv;
8089
8090 ga_concat(&ga, (char_u *)", ");
8091 dtv.v_type = VAR_DICT;
8092 dtv.vval.v_dict = pt->pt_dict;
8093 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8094 vim_free(tf);
8095 }
8096 ga_concat(&ga, (char_u *)")");
8097
8098 *tofree = ga.ga_data;
8099 r = *tofree;
8100 break;
8101 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008102
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008103 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008104 if (tv->vval.v_list == NULL)
8105 {
8106 *tofree = NULL;
8107 r = NULL;
8108 }
8109 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8110 {
8111 *tofree = NULL;
8112 r = (char_u *)"[...]";
8113 }
8114 else
8115 {
8116 tv->vval.v_list->lv_copyID = copyID;
8117 *tofree = list2string(tv, copyID);
8118 r = *tofree;
8119 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008120 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008121
Bram Moolenaar8c711452005-01-14 21:53:12 +00008122 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008123 if (tv->vval.v_dict == NULL)
8124 {
8125 *tofree = NULL;
8126 r = NULL;
8127 }
8128 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8129 {
8130 *tofree = NULL;
8131 r = (char_u *)"{...}";
8132 }
8133 else
8134 {
8135 tv->vval.v_dict->dv_copyID = copyID;
8136 *tofree = dict2string(tv, copyID);
8137 r = *tofree;
8138 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008139 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008140
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141 case VAR_STRING:
8142 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008143 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008144 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008145 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008146 *tofree = NULL;
8147 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008148 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008149
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008150 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008151#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008152 *tofree = NULL;
8153 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8154 r = numbuf;
8155 break;
8156#endif
8157
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008158 case VAR_SPECIAL:
8159 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008160 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008161 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008162 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008163
Bram Moolenaar8502c702014-06-17 12:51:16 +02008164 if (--recurse == 0)
8165 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008166 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008167}
8168
8169/*
8170 * Return a string with the string representation of a variable.
8171 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8172 * "numbuf" is used for a number.
8173 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008174 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008175 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008176 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008177tv2string(
8178 typval_T *tv,
8179 char_u **tofree,
8180 char_u *numbuf,
8181 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008182{
8183 switch (tv->v_type)
8184 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008185 case VAR_FUNC:
8186 *tofree = string_quote(tv->vval.v_string, TRUE);
8187 return *tofree;
8188 case VAR_STRING:
8189 *tofree = string_quote(tv->vval.v_string, FALSE);
8190 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008191 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008192#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008193 *tofree = NULL;
8194 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8195 return numbuf;
8196#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008197 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008198 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008199 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008200 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008201 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008202 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008203 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008204 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008205 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008206 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008207 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008208}
8209
8210/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008211 * Return string "str" in ' quotes, doubling ' characters.
8212 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008213 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008214 */
8215 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008216string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217{
Bram Moolenaar33570922005-01-25 22:26:29 +00008218 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008219 char_u *p, *r, *s;
8220
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 len = (function ? 13 : 3);
8222 if (str != NULL)
8223 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008224 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008225 for (p = str; *p != NUL; mb_ptr_adv(p))
8226 if (*p == '\'')
8227 ++len;
8228 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 s = r = alloc(len);
8230 if (r != NULL)
8231 {
8232 if (function)
8233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008234 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 r += 10;
8236 }
8237 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008238 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008239 if (str != NULL)
8240 for (p = str; *p != NUL; )
8241 {
8242 if (*p == '\'')
8243 *r++ = '\'';
8244 MB_COPY_CHAR(p, r);
8245 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008246 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008247 if (function)
8248 *r++ = ')';
8249 *r++ = NUL;
8250 }
8251 return s;
8252}
8253
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008254#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008255/*
8256 * Convert the string "text" to a floating point number.
8257 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8258 * this always uses a decimal point.
8259 * Returns the length of the text that was consumed.
8260 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008262string2float(
8263 char_u *text,
8264 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265{
8266 char *s = (char *)text;
8267 float_T f;
8268
8269 f = strtod(s, &s);
8270 *value = f;
8271 return (int)((char_u *)s - text);
8272}
8273#endif
8274
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008275/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * Get the value of an environment variable.
8277 * "arg" is pointing to the '$'. It is advanced to after the name.
8278 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008279 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 */
8281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008282get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
8284 char_u *string = NULL;
8285 int len;
8286 int cc;
8287 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008288 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
8290 ++*arg;
8291 name = *arg;
8292 len = get_env_len(arg);
8293 if (evaluate)
8294 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008295 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008296 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008297
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008298 cc = name[len];
8299 name[len] = NUL;
8300 /* first try vim_getenv(), fast for normal environment vars */
8301 string = vim_getenv(name, &mustfree);
8302 if (string != NULL && *string != NUL)
8303 {
8304 if (!mustfree)
8305 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008307 else
8308 {
8309 if (mustfree)
8310 vim_free(string);
8311
8312 /* next try expanding things like $VIM and ${HOME} */
8313 string = expand_env_save(name - 1);
8314 if (string != NULL && *string == '$')
8315 {
8316 vim_free(string);
8317 string = NULL;
8318 }
8319 }
8320 name[len] = cc;
8321
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008322 rettv->v_type = VAR_STRING;
8323 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325
8326 return OK;
8327}
8328
8329/*
8330 * Array with names and number of arguments of all internal functions
8331 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8332 */
8333static struct fst
8334{
8335 char *f_name; /* function name */
8336 char f_min_argc; /* minimal number of arguments */
8337 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008338 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008339 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340} functions[] =
8341{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008342#ifdef FEAT_FLOAT
8343 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008344 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008345#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008346 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008347 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008348 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 {"append", 2, 2, f_append},
8350 {"argc", 0, 0, f_argc},
8351 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008352 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008353 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008354#ifdef FEAT_FLOAT
8355 {"asin", 1, 1, f_asin}, /* WJMc */
8356#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008357 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008358 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008359 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008360 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008361 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008362 {"assert_notequal", 2, 3, f_assert_notequal},
8363 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008364 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008365#ifdef FEAT_FLOAT
8366 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008367 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008370 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"bufexists", 1, 1, f_bufexists},
8372 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8373 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8374 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8375 {"buflisted", 1, 1, f_buflisted},
8376 {"bufloaded", 1, 1, f_bufloaded},
8377 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008378 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 {"bufwinnr", 1, 1, f_bufwinnr},
8380 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008381 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008382 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008383 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008384#ifdef FEAT_FLOAT
8385 {"ceil", 1, 1, f_ceil},
8386#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008387#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008388 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008389 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8390 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008391 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008392 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008393 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008394 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008395 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008396 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008397 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008398 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008399 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8400 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008401 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008402 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008403#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008404 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008405 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008407 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008409#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008410 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008411 {"complete_add", 1, 1, f_complete_add},
8412 {"complete_check", 0, 0, f_complete_check},
8413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008415 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008416#ifdef FEAT_FLOAT
8417 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008418 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008419#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008420 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008422 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008423 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008424 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008426 {"diff_filler", 1, 1, f_diff_filler},
8427 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008428 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008429 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008431 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 {"eventhandler", 0, 0, f_eventhandler},
8433 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008434 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008436#ifdef FEAT_FLOAT
8437 {"exp", 1, 1, f_exp},
8438#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008439 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008440 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008441 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8443 {"filereadable", 1, 1, f_filereadable},
8444 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008445 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008446 {"finddir", 1, 3, f_finddir},
8447 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008448#ifdef FEAT_FLOAT
8449 {"float2nr", 1, 1, f_float2nr},
8450 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008451 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008452#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008453 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {"fnamemodify", 2, 2, f_fnamemodify},
8455 {"foldclosed", 1, 1, f_foldclosed},
8456 {"foldclosedend", 1, 1, f_foldclosedend},
8457 {"foldlevel", 1, 1, f_foldlevel},
8458 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008459 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008461 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008462 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008463 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008464 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008465 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 {"getchar", 0, 1, f_getchar},
8467 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008468 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {"getcmdline", 0, 0, f_getcmdline},
8470 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008471 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008472 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008473 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008474 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008475 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008476 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 {"getfsize", 1, 1, f_getfsize},
8478 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008479 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008480 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008481 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008482 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008483 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008484 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008485 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008486 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008488 {"gettabvar", 2, 3, f_gettabvar},
8489 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"getwinposx", 0, 0, f_getwinposx},
8491 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008492 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008493 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008494 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008495 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008497 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008498 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008499 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8501 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8502 {"histadd", 2, 2, f_histadd},
8503 {"histdel", 1, 2, f_histdel},
8504 {"histget", 1, 2, f_histget},
8505 {"histnr", 1, 1, f_histnr},
8506 {"hlID", 1, 1, f_hlID},
8507 {"hlexists", 1, 1, f_hlexists},
8508 {"hostname", 0, 0, f_hostname},
8509 {"iconv", 3, 3, f_iconv},
8510 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008511 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008512 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008514 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"inputrestore", 0, 0, f_inputrestore},
8516 {"inputsave", 0, 0, f_inputsave},
8517 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008519 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008521 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008522#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8523 {"isnan", 1, 1, f_isnan},
8524#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008525 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008526#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008527 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008528 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008529 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008530 {"job_start", 1, 2, f_job_start},
8531 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008532 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008533#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008534 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008535 {"js_decode", 1, 1, f_js_decode},
8536 {"js_encode", 1, 1, f_js_encode},
8537 {"json_decode", 1, 1, f_json_decode},
8538 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008539 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008541 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"libcall", 3, 3, f_libcall},
8543 {"libcallnr", 3, 3, f_libcallnr},
8544 {"line", 1, 1, f_line},
8545 {"line2byte", 1, 1, f_line2byte},
8546 {"lispindent", 1, 1, f_lispindent},
8547 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008548#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008549 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008550 {"log10", 1, 1, f_log10},
8551#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008552#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008553 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008554#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008555 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008556 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008557 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008558 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008559 {"matchadd", 2, 5, f_matchadd},
8560 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008561 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008562 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008563 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008564 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008565 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008566 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008567 {"max", 1, 1, f_max},
8568 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008569#ifdef vim_mkdir
8570 {"mkdir", 1, 3, f_mkdir},
8571#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008572 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008573#ifdef FEAT_MZSCHEME
8574 {"mzeval", 1, 1, f_mzeval},
8575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008577 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008578 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008579 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008580#ifdef FEAT_PERL
8581 {"perleval", 1, 1, f_perleval},
8582#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008583#ifdef FEAT_FLOAT
8584 {"pow", 2, 2, f_pow},
8585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008587 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008588 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008589#ifdef FEAT_PYTHON3
8590 {"py3eval", 1, 1, f_py3eval},
8591#endif
8592#ifdef FEAT_PYTHON
8593 {"pyeval", 1, 1, f_pyeval},
8594#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008595 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008596 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008597 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008598#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008599 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008600#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008601 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {"remote_expr", 2, 3, f_remote_expr},
8603 {"remote_foreground", 1, 1, f_remote_foreground},
8604 {"remote_peek", 1, 2, f_remote_peek},
8605 {"remote_read", 1, 1, f_remote_read},
8606 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008607 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008609 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008611 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008612#ifdef FEAT_FLOAT
8613 {"round", 1, 1, f_round},
8614#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008615 {"screenattr", 2, 2, f_screenattr},
8616 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008617 {"screencol", 0, 0, f_screencol},
8618 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008619 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008620 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008621 {"searchpair", 3, 7, f_searchpair},
8622 {"searchpairpos", 3, 7, f_searchpairpos},
8623 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 {"server2client", 2, 2, f_server2client},
8625 {"serverlist", 0, 0, f_serverlist},
8626 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008627 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008629 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008631 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008632 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008633 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008634 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008636 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008637 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008639#ifdef FEAT_CRYPT
8640 {"sha256", 1, 1, f_sha256},
8641#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008642 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008643 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008645#ifdef FEAT_FLOAT
8646 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008647 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008648#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008649 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008650 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008651 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008652 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008653 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008654#ifdef FEAT_FLOAT
8655 {"sqrt", 1, 1, f_sqrt},
8656 {"str2float", 1, 1, f_str2float},
8657#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008658 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008659 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008660 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008661 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662#ifdef HAVE_STRFTIME
8663 {"strftime", 1, 2, f_strftime},
8664#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008665 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {"strlen", 1, 1, f_strlen},
8669 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008670 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008672 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008673 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {"substitute", 4, 4, f_substitute},
8675 {"synID", 3, 3, f_synID},
8676 {"synIDattr", 2, 3, f_synIDattr},
8677 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008678 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008679 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008680 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008681 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008682 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008683 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008684 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008685 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008686 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008687#ifdef FEAT_FLOAT
8688 {"tan", 1, 1, f_tan},
8689 {"tanh", 1, 1, f_tanh},
8690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {"tempname", 0, 0, f_tempname},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008692 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8693#ifdef FEAT_JOB_CHANNEL
8694 {"test_null_channel", 0, 0, f_test_null_channel},
8695#endif
8696 {"test_null_dict", 0, 0, f_test_null_dict},
8697#ifdef FEAT_JOB_CHANNEL
8698 {"test_null_job", 0, 0, f_test_null_job},
8699#endif
8700 {"test_null_list", 0, 0, f_test_null_list},
8701 {"test_null_partial", 0, 0, f_test_null_partial},
8702 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008703#ifdef FEAT_TIMERS
8704 {"timer_start", 2, 3, f_timer_start},
8705 {"timer_stop", 1, 1, f_timer_stop},
8706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {"tolower", 1, 1, f_tolower},
8708 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008709 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008710#ifdef FEAT_FLOAT
8711 {"trunc", 1, 1, f_trunc},
8712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008714 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008715 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008716 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008717 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {"virtcol", 1, 1, f_virtcol},
8719 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008720 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008721 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008722 {"win_getid", 0, 2, f_win_getid},
8723 {"win_gotoid", 1, 1, f_win_gotoid},
8724 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8725 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 {"winbufnr", 1, 1, f_winbufnr},
8727 {"wincol", 0, 0, f_wincol},
8728 {"winheight", 1, 1, f_winheight},
8729 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008730 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008732 {"winrestview", 1, 1, f_winrestview},
8733 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008735 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008736 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008737 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738};
8739
8740#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8741
8742/*
8743 * Function given to ExpandGeneric() to obtain the list of internal
8744 * or user defined function names.
8745 */
8746 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008747get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
8749 static int intidx = -1;
8750 char_u *name;
8751
8752 if (idx == 0)
8753 intidx = -1;
8754 if (intidx < 0)
8755 {
8756 name = get_user_func_name(xp, idx);
8757 if (name != NULL)
8758 return name;
8759 }
8760 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8761 {
8762 STRCPY(IObuff, functions[intidx].f_name);
8763 STRCAT(IObuff, "(");
8764 if (functions[intidx].f_max_argc == 0)
8765 STRCAT(IObuff, ")");
8766 return IObuff;
8767 }
8768
8769 return NULL;
8770}
8771
8772/*
8773 * Function given to ExpandGeneric() to obtain the list of internal or
8774 * user defined variable or function names.
8775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008777get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778{
8779 static int intidx = -1;
8780 char_u *name;
8781
8782 if (idx == 0)
8783 intidx = -1;
8784 if (intidx < 0)
8785 {
8786 name = get_function_name(xp, idx);
8787 if (name != NULL)
8788 return name;
8789 }
8790 return get_user_var_name(xp, ++intidx);
8791}
8792
8793#endif /* FEAT_CMDL_COMPL */
8794
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008795#if defined(EBCDIC) || defined(PROTO)
8796/*
8797 * Compare struct fst by function name.
8798 */
8799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008800compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008801{
8802 struct fst *p1 = (struct fst *)s1;
8803 struct fst *p2 = (struct fst *)s2;
8804
8805 return STRCMP(p1->f_name, p2->f_name);
8806}
8807
8808/*
8809 * Sort the function table by function name.
8810 * The sorting of the table above is ASCII dependant.
8811 * On machines using EBCDIC we have to sort it.
8812 */
8813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008814sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008815{
8816 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8817
8818 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8819}
8820#endif
8821
8822
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823/*
8824 * Find internal function in table above.
8825 * Return index, or -1 if not found
8826 */
8827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008828find_internal_func(
8829 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830{
8831 int first = 0;
8832 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8833 int cmp;
8834 int x;
8835
8836 /*
8837 * Find the function name in the table. Binary search.
8838 */
8839 while (first <= last)
8840 {
8841 x = first + ((unsigned)(last - first) >> 1);
8842 cmp = STRCMP(name, functions[x].f_name);
8843 if (cmp < 0)
8844 last = x - 1;
8845 else if (cmp > 0)
8846 first = x + 1;
8847 else
8848 return x;
8849 }
8850 return -1;
8851}
8852
8853/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8855 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008856 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8857 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008858 */
8859 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008860deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008861{
Bram Moolenaar33570922005-01-25 22:26:29 +00008862 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008863 int cc;
8864
Bram Moolenaar65639032016-03-16 21:40:30 +01008865 if (partialp != NULL)
8866 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008867
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008868 cc = name[*lenp];
8869 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008870 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008872 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008873 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008874 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008875 {
8876 *lenp = 0;
8877 return (char_u *)""; /* just in case */
8878 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008879 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008880 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008881 }
8882
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008883 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8884 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008885 partial_T *pt = v->di_tv.vval.v_partial;
8886
8887 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008888 {
8889 *lenp = 0;
8890 return (char_u *)""; /* just in case */
8891 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008892 if (partialp != NULL)
8893 *partialp = pt;
8894 *lenp = (int)STRLEN(pt->pt_name);
8895 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008896 }
8897
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008898 return name;
8899}
8900
8901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 * Allocate a variable for the result of a function.
8903 * Return OK or FAIL.
8904 */
8905 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008906get_func_tv(
8907 char_u *name, /* name of the function */
8908 int len, /* length of "name" */
8909 typval_T *rettv,
8910 char_u **arg, /* argument, pointing to the '(' */
8911 linenr_T firstline, /* first line of range */
8912 linenr_T lastline, /* last line of range */
8913 int *doesrange, /* return: function handled range */
8914 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008915 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008916 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918 char_u *argp;
8919 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008920 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 int argcount = 0; /* number of arguments found */
8922
8923 /*
8924 * Get the arguments.
8925 */
8926 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008927 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 {
8929 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8930 if (*argp == ')' || *argp == ',' || *argp == NUL)
8931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8933 {
8934 ret = FAIL;
8935 break;
8936 }
8937 ++argcount;
8938 if (*argp != ',')
8939 break;
8940 }
8941 if (*argp == ')')
8942 ++argp;
8943 else
8944 ret = FAIL;
8945
8946 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008947 {
8948 int i = 0;
8949
8950 if (get_vim_var_nr(VV_TESTING))
8951 {
8952 /* Prepare for calling garbagecollect_for_testing(), need to know
8953 * what variables are used on the call stack. */
8954 if (funcargs.ga_itemsize == 0)
8955 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8956 for (i = 0; i < argcount; ++i)
8957 if (ga_grow(&funcargs, 1) == OK)
8958 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8959 &argvars[i];
8960 }
8961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008962 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008963 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008964
8965 funcargs.ga_len -= i;
8966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008968 {
8969 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008970 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008971 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008972 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974
8975 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008976 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
8978 *arg = skipwhite(argp);
8979 return ret;
8980}
8981
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008982#define ERROR_UNKNOWN 0
8983#define ERROR_TOOMANY 1
8984#define ERROR_TOOFEW 2
8985#define ERROR_SCRIPT 3
8986#define ERROR_DICT 4
8987#define ERROR_NONE 5
8988#define ERROR_OTHER 6
8989#define FLEN_FIXED 40
8990
8991/*
8992 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8993 * Change <SNR>123_name() to K_SNR 123_name().
8994 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8995 * (slow).
8996 */
8997 static char_u *
8998fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8999{
9000 int llen;
9001 char_u *fname;
9002 int i;
9003
9004 llen = eval_fname_script(name);
9005 if (llen > 0)
9006 {
9007 fname_buf[0] = K_SPECIAL;
9008 fname_buf[1] = KS_EXTRA;
9009 fname_buf[2] = (int)KE_SNR;
9010 i = 3;
9011 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9012 {
9013 if (current_SID <= 0)
9014 *error = ERROR_SCRIPT;
9015 else
9016 {
9017 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9018 i = (int)STRLEN(fname_buf);
9019 }
9020 }
9021 if (i + STRLEN(name + llen) < FLEN_FIXED)
9022 {
9023 STRCPY(fname_buf + i, name + llen);
9024 fname = fname_buf;
9025 }
9026 else
9027 {
9028 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9029 if (fname == NULL)
9030 *error = ERROR_OTHER;
9031 else
9032 {
9033 *tofree = fname;
9034 mch_memmove(fname, fname_buf, (size_t)i);
9035 STRCPY(fname + i, name + llen);
9036 }
9037 }
9038 }
9039 else
9040 fname = name;
9041 return fname;
9042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044/*
9045 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009046 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009047 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009049 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050call_func(
9051 char_u *funcname, /* name of the function */
9052 int len, /* length of "name" */
9053 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009054 int argcount_in, /* number of "argvars" */
9055 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009056 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009057 linenr_T firstline, /* first line of range */
9058 linenr_T lastline, /* last line of range */
9059 int *doesrange, /* return: function handled range */
9060 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009061 partial_T *partial, /* optional, can be NULL */
9062 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063{
9064 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 int error = ERROR_NONE;
9066 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009069 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009071 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009072 int argcount = argcount_in;
9073 typval_T *argvars = argvars_in;
9074 dict_T *selfdict = selfdict_in;
9075 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9076 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009077
9078 /* Make a copy of the name, if it comes from a funcref variable it could
9079 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009080 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009081 if (name == NULL)
9082 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009084 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085
9086 *doesrange = FALSE;
9087
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009088 if (partial != NULL)
9089 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009090 /* When the function has a partial with a dict and there is a dict
9091 * argument, use the dict argument. That is backwards compatible.
9092 * When the dict was bound explicitly use the one from the partial. */
9093 if (partial->pt_dict != NULL
9094 && (selfdict_in == NULL || !partial->pt_auto))
9095 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009096 if (error == ERROR_NONE && partial->pt_argc > 0)
9097 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009098 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9099 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9100 for (i = 0; i < argcount_in; ++i)
9101 argv[i + argv_clear] = argvars_in[i];
9102 argvars = argv;
9103 argcount = partial->pt_argc + argcount_in;
9104 }
9105 }
9106
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107
9108 /* execute the function if no errors detected and executing */
9109 if (evaluate && error == ERROR_NONE)
9110 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009111 char_u *rfname = fname;
9112
9113 /* Ignore "g:" before a function name. */
9114 if (fname[0] == 'g' && fname[1] == ':')
9115 rfname = fname + 2;
9116
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009117 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9118 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 error = ERROR_UNKNOWN;
9120
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009121 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 {
9123 /*
9124 * User defined function.
9125 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009126 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009127
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009129 /* Trigger FuncUndefined event, may load the function. */
9130 if (fp == NULL
9131 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009132 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009133 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009135 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009136 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 }
9138#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009139 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009140 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009141 {
9142 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009143 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009144 }
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 if (fp != NULL)
9147 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009148 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009150 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009152 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009154 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009155 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 else
9157 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009158 int did_save_redo = FALSE;
9159
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 /*
9161 * Call the user function.
9162 * Save and restore search patterns, script variables and
9163 * redo buffer.
9164 */
9165 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009166#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009167 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009168#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009169 {
9170 saveRedobuff();
9171 did_save_redo = TRUE;
9172 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009173 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009175 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009176 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9177 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9178 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009179 /* Function was unreferenced while being used, free it
9180 * now. */
9181 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009182 if (did_save_redo)
9183 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 restore_search_patterns();
9185 error = ERROR_NONE;
9186 }
9187 }
9188 }
9189 else
9190 {
9191 /*
9192 * Find the function name in the table, call its implementation.
9193 */
9194 i = find_internal_func(fname);
9195 if (i >= 0)
9196 {
9197 if (argcount < functions[i].f_min_argc)
9198 error = ERROR_TOOFEW;
9199 else if (argcount > functions[i].f_max_argc)
9200 error = ERROR_TOOMANY;
9201 else
9202 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009203 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 error = ERROR_NONE;
9206 }
9207 }
9208 }
9209 /*
9210 * The function call (or "FuncUndefined" autocommand sequence) might
9211 * have been aborted by an error, an interrupt, or an explicitly thrown
9212 * exception that has not been caught so far. This situation can be
9213 * tested for by calling aborting(). For an error in an internal
9214 * function or for the "E132" error in call_user_func(), however, the
9215 * throw point at which the "force_abort" flag (temporarily reset by
9216 * emsg()) is normally updated has not been reached yet. We need to
9217 * update that flag first to make aborting() reliable.
9218 */
9219 update_force_abort();
9220 }
9221 if (error == ERROR_NONE)
9222 ret = OK;
9223
9224 /*
9225 * Report an error unless the argument evaluation or function call has been
9226 * cancelled due to an aborting error, an interrupt, or an exception.
9227 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009228 if (!aborting())
9229 {
9230 switch (error)
9231 {
9232 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009233 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009234 break;
9235 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009237 break;
9238 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009239 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009240 name);
9241 break;
9242 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009243 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009244 name);
9245 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009246 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009247 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009248 name);
9249 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009250 }
9251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009253 while (argv_clear > 0)
9254 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009255 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009256 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257
9258 return ret;
9259}
9260
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009261/*
9262 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009263 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009264 */
9265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009266emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009267{
9268 char_u *p;
9269
9270 if (*name == K_SPECIAL)
9271 p = concat_str((char_u *)"<SNR>", name + 3);
9272 else
9273 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009274 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009275 if (p != name)
9276 vim_free(p);
9277}
9278
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009279/*
9280 * Return TRUE for a non-zero Number and a non-empty String.
9281 */
9282 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009283non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009284{
9285 return ((argvars[0].v_type == VAR_NUMBER
9286 && argvars[0].vval.v_number != 0)
9287 || (argvars[0].v_type == VAR_STRING
9288 && argvars[0].vval.v_string != NULL
9289 && *argvars[0].vval.v_string != NUL));
9290}
9291
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292/*********************************************
9293 * Implementation of the built-in functions
9294 */
9295
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009296#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009297static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009298
9299/*
9300 * Get the float value of "argvars[0]" into "f".
9301 * Returns FAIL when the argument is not a Number or Float.
9302 */
9303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009305{
9306 if (argvars[0].v_type == VAR_FLOAT)
9307 {
9308 *f = argvars[0].vval.v_float;
9309 return OK;
9310 }
9311 if (argvars[0].v_type == VAR_NUMBER)
9312 {
9313 *f = (float_T)argvars[0].vval.v_number;
9314 return OK;
9315 }
9316 EMSG(_("E808: Number or Float required"));
9317 return FAIL;
9318}
9319
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009320/*
9321 * "abs(expr)" function
9322 */
9323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009324f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009325{
9326 if (argvars[0].v_type == VAR_FLOAT)
9327 {
9328 rettv->v_type = VAR_FLOAT;
9329 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9330 }
9331 else
9332 {
9333 varnumber_T n;
9334 int error = FALSE;
9335
9336 n = get_tv_number_chk(&argvars[0], &error);
9337 if (error)
9338 rettv->vval.v_number = -1;
9339 else if (n > 0)
9340 rettv->vval.v_number = n;
9341 else
9342 rettv->vval.v_number = -n;
9343 }
9344}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009345
9346/*
9347 * "acos()" function
9348 */
9349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009350f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009351{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009352 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009353
9354 rettv->v_type = VAR_FLOAT;
9355 if (get_float_arg(argvars, &f) == OK)
9356 rettv->vval.v_float = acos(f);
9357 else
9358 rettv->vval.v_float = 0.0;
9359}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009360#endif
9361
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009363 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 */
9365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009366f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367{
Bram Moolenaar33570922005-01-25 22:26:29 +00009368 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009371 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009373 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009374 && !tv_check_lock(l->lv_lock,
9375 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009376 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009377 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009378 }
9379 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009380 EMSG(_(e_listreq));
9381}
9382
9383/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009384 * "alloc_fail(id, countdown, repeat)" function
9385 */
9386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009387f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009388{
9389 if (argvars[0].v_type != VAR_NUMBER
9390 || argvars[0].vval.v_number <= 0
9391 || argvars[1].v_type != VAR_NUMBER
9392 || argvars[1].vval.v_number < 0
9393 || argvars[2].v_type != VAR_NUMBER)
9394 EMSG(_(e_invarg));
9395 else
9396 {
9397 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009398 if (alloc_fail_id >= aid_last)
9399 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009400 alloc_fail_countdown = argvars[1].vval.v_number;
9401 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009402 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009403 }
9404}
9405
9406/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009407 * "and(expr, expr)" function
9408 */
9409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009410f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009411{
9412 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9413 & get_tv_number_chk(&argvars[1], NULL);
9414}
9415
9416/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009417 * "append(lnum, string/list)" function
9418 */
9419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009420f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009421{
9422 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009423 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009424 list_T *l = NULL;
9425 listitem_T *li = NULL;
9426 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 long added = 0;
9428
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009429 /* When coming here from Insert mode, sync undo, so that this can be
9430 * undone separately from what was previously inserted. */
9431 if (u_sync_once == 2)
9432 {
9433 u_sync_once = 1; /* notify that u_sync() was called */
9434 u_sync(TRUE);
9435 }
9436
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 lnum = get_tv_lnum(argvars);
9438 if (lnum >= 0
9439 && lnum <= curbuf->b_ml.ml_line_count
9440 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009441 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009442 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009443 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009444 l = argvars[1].vval.v_list;
9445 if (l == NULL)
9446 return;
9447 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009448 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009449 for (;;)
9450 {
9451 if (l == NULL)
9452 tv = &argvars[1]; /* append a string */
9453 else if (li == NULL)
9454 break; /* end of list */
9455 else
9456 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009457 line = get_tv_string_chk(tv);
9458 if (line == NULL) /* type error */
9459 {
9460 rettv->vval.v_number = 1; /* Failed */
9461 break;
9462 }
9463 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464 ++added;
9465 if (l == NULL)
9466 break;
9467 li = li->li_next;
9468 }
9469
9470 appended_lines_mark(lnum, added);
9471 if (curwin->w_cursor.lnum > lnum)
9472 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009474 else
9475 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476}
9477
9478/*
9479 * "argc()" function
9480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009482f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009484 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009485}
9486
9487/*
9488 * "argidx()" function
9489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009491f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009492{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009493 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494}
9495
9496/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009497 * "arglistid()" function
9498 */
9499 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009500f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009501{
9502 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009503
9504 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009505 wp = find_tabwin(&argvars[0], &argvars[1]);
9506 if (wp != NULL)
9507 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009508}
9509
9510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 * "argv(nr)" function
9512 */
9513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 int idx;
9517
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009518 if (argvars[0].v_type != VAR_UNKNOWN)
9519 {
9520 idx = get_tv_number_chk(&argvars[0], NULL);
9521 if (idx >= 0 && idx < ARGCOUNT)
9522 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9523 else
9524 rettv->vval.v_string = NULL;
9525 rettv->v_type = VAR_STRING;
9526 }
9527 else if (rettv_list_alloc(rettv) == OK)
9528 for (idx = 0; idx < ARGCOUNT; ++idx)
9529 list_append_string(rettv->vval.v_list,
9530 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009533typedef enum
9534{
9535 ASSERT_EQUAL,
9536 ASSERT_NOTEQUAL,
9537 ASSERT_MATCH,
9538 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009539 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009540} assert_type_T;
9541
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009542static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009543static 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 +01009544static void assert_error(garray_T *gap);
9545static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009546
9547/*
9548 * Prepare "gap" for an assert error and add the sourcing position.
9549 */
9550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009551prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009552{
9553 char buf[NUMBUFLEN];
9554
9555 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009556 if (sourcing_name != NULL)
9557 {
9558 ga_concat(gap, sourcing_name);
9559 if (sourcing_lnum > 0)
9560 ga_concat(gap, (char_u *)" ");
9561 }
9562 if (sourcing_lnum > 0)
9563 {
9564 sprintf(buf, "line %ld", (long)sourcing_lnum);
9565 ga_concat(gap, (char_u *)buf);
9566 }
9567 if (sourcing_name != NULL || sourcing_lnum > 0)
9568 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009569}
9570
9571/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009572 * Append "str" to "gap", escaping unprintable characters.
9573 * Changes NL to \n, CR to \r, etc.
9574 */
9575 static void
9576ga_concat_esc(garray_T *gap, char_u *str)
9577{
9578 char_u *p;
9579 char_u buf[NUMBUFLEN];
9580
Bram Moolenaarf1551962016-03-15 12:55:58 +01009581 if (str == NULL)
9582 {
9583 ga_concat(gap, (char_u *)"NULL");
9584 return;
9585 }
9586
Bram Moolenaar23689172016-02-15 22:37:37 +01009587 for (p = str; *p != NUL; ++p)
9588 switch (*p)
9589 {
9590 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9591 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9592 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9593 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9594 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9595 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9596 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9597 default:
9598 if (*p < ' ')
9599 {
9600 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9601 ga_concat(gap, buf);
9602 }
9603 else
9604 ga_append(gap, *p);
9605 break;
9606 }
9607}
9608
9609/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009610 * Fill "gap" with information about an assert error.
9611 */
9612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009613fill_assert_error(
9614 garray_T *gap,
9615 typval_T *opt_msg_tv,
9616 char_u *exp_str,
9617 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009618 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009619 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009620{
9621 char_u numbuf[NUMBUFLEN];
9622 char_u *tofree;
9623
9624 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9625 {
9626 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9627 vim_free(tofree);
9628 }
9629 else
9630 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009631 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009632 ga_concat(gap, (char_u *)"Pattern ");
9633 else
9634 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009635 if (exp_str == NULL)
9636 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009637 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009638 vim_free(tofree);
9639 }
9640 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009641 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009642 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009643 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009644 else if (atype == ASSERT_NOTMATCH)
9645 ga_concat(gap, (char_u *)" does match ");
9646 else if (atype == ASSERT_NOTEQUAL)
9647 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009648 else
9649 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009650 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009651 vim_free(tofree);
9652 }
9653}
Bram Moolenaar43345542015-11-29 17:35:35 +01009654
9655/*
9656 * Add an assert error to v:errors.
9657 */
9658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009660{
9661 struct vimvar *vp = &vimvars[VV_ERRORS];
9662
9663 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9664 /* Make sure v:errors is a list. */
9665 set_vim_var_list(VV_ERRORS, list_alloc());
9666 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9667}
9668
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669 static void
9670assert_equal_common(typval_T *argvars, assert_type_T atype)
9671{
9672 garray_T ga;
9673
9674 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9675 != (atype == ASSERT_EQUAL))
9676 {
9677 prepare_assert_error(&ga);
9678 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9679 atype);
9680 assert_error(&ga);
9681 ga_clear(&ga);
9682 }
9683}
9684
Bram Moolenaar43345542015-11-29 17:35:35 +01009685/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009686 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009687 */
9688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009689f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009690{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009691 assert_equal_common(argvars, ASSERT_EQUAL);
9692}
Bram Moolenaar43345542015-11-29 17:35:35 +01009693
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009694/*
9695 * "assert_notequal(expected, actual[, msg])" function
9696 */
9697 static void
9698f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9699{
9700 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009701}
9702
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009703/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009704 * "assert_exception(string[, msg])" function
9705 */
9706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009707f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009708{
9709 garray_T ga;
9710 char *error;
9711
9712 error = (char *)get_tv_string_chk(&argvars[0]);
9713 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9714 {
9715 prepare_assert_error(&ga);
9716 ga_concat(&ga, (char_u *)"v:exception is not set");
9717 assert_error(&ga);
9718 ga_clear(&ga);
9719 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009720 else if (error != NULL
9721 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009722 {
9723 prepare_assert_error(&ga);
9724 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009725 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009726 assert_error(&ga);
9727 ga_clear(&ga);
9728 }
9729}
9730
9731/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009732 * "assert_fails(cmd [, error])" function
9733 */
9734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009735f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009736{
9737 char_u *cmd = get_tv_string_chk(&argvars[0]);
9738 garray_T ga;
9739
9740 called_emsg = FALSE;
9741 suppress_errthrow = TRUE;
9742 emsg_silent = TRUE;
9743 do_cmdline_cmd(cmd);
9744 if (!called_emsg)
9745 {
9746 prepare_assert_error(&ga);
9747 ga_concat(&ga, (char_u *)"command did not fail: ");
9748 ga_concat(&ga, cmd);
9749 assert_error(&ga);
9750 ga_clear(&ga);
9751 }
9752 else if (argvars[1].v_type != VAR_UNKNOWN)
9753 {
9754 char_u buf[NUMBUFLEN];
9755 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9756
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009757 if (error == NULL
9758 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009759 {
9760 prepare_assert_error(&ga);
9761 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009762 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009763 assert_error(&ga);
9764 ga_clear(&ga);
9765 }
9766 }
9767
9768 called_emsg = FALSE;
9769 suppress_errthrow = FALSE;
9770 emsg_silent = FALSE;
9771 emsg_on_display = FALSE;
9772 set_vim_var_string(VV_ERRMSG, NULL, 0);
9773}
9774
9775/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 * Common for assert_true() and assert_false().
9777 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009780{
9781 int error = FALSE;
9782 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009783
Bram Moolenaar37127922016-02-06 20:29:28 +01009784 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009785 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009786 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009787 if (argvars[0].v_type != VAR_NUMBER
9788 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9789 || error)
9790 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009791 prepare_assert_error(&ga);
9792 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009793 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009794 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009795 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009796 ga_clear(&ga);
9797 }
9798}
9799
9800/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009801 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009802 */
9803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009805{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009806 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009807}
9808
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009809 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009810assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009811{
9812 garray_T ga;
9813 char_u buf1[NUMBUFLEN];
9814 char_u buf2[NUMBUFLEN];
9815 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9816 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9817
Bram Moolenaar72188e92016-03-28 22:48:29 +02009818 if (pat == NULL || text == NULL)
9819 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009820 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009821 {
9822 prepare_assert_error(&ga);
9823 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009824 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009825 assert_error(&ga);
9826 ga_clear(&ga);
9827 }
9828}
9829
9830/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009831 * "assert_match(pattern, actual[, msg])" function
9832 */
9833 static void
9834f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9835{
9836 assert_match_common(argvars, ASSERT_MATCH);
9837}
9838
9839/*
9840 * "assert_notmatch(pattern, actual[, msg])" function
9841 */
9842 static void
9843f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9844{
9845 assert_match_common(argvars, ASSERT_NOTMATCH);
9846}
9847
9848/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009849 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009850 */
9851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009853{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009854 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009855}
9856
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009857#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009858/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009859 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009860 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009862f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009863{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009864 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009865
9866 rettv->v_type = VAR_FLOAT;
9867 if (get_float_arg(argvars, &f) == OK)
9868 rettv->vval.v_float = asin(f);
9869 else
9870 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009871}
9872
9873/*
9874 * "atan()" function
9875 */
9876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009877f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009878{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009879 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009880
9881 rettv->v_type = VAR_FLOAT;
9882 if (get_float_arg(argvars, &f) == OK)
9883 rettv->vval.v_float = atan(f);
9884 else
9885 rettv->vval.v_float = 0.0;
9886}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009887
9888/*
9889 * "atan2()" function
9890 */
9891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009892f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009893{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009894 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009895
9896 rettv->v_type = VAR_FLOAT;
9897 if (get_float_arg(argvars, &fx) == OK
9898 && get_float_arg(&argvars[1], &fy) == OK)
9899 rettv->vval.v_float = atan2(fx, fy);
9900 else
9901 rettv->vval.v_float = 0.0;
9902}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009903#endif
9904
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905/*
9906 * "browse(save, title, initdir, default)" function
9907 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009909f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910{
9911#ifdef FEAT_BROWSE
9912 int save;
9913 char_u *title;
9914 char_u *initdir;
9915 char_u *defname;
9916 char_u buf[NUMBUFLEN];
9917 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009918 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009920 save = get_tv_number_chk(&argvars[0], &error);
9921 title = get_tv_string_chk(&argvars[1]);
9922 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9923 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (error || title == NULL || initdir == NULL || defname == NULL)
9926 rettv->vval.v_string = NULL;
9927 else
9928 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009929 do_browse(save ? BROWSE_SAVE : 0,
9930 title, defname, NULL, initdir, NULL, curbuf);
9931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009935}
9936
9937/*
9938 * "browsedir(title, initdir)" function
9939 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009941f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009942{
9943#ifdef FEAT_BROWSE
9944 char_u *title;
9945 char_u *initdir;
9946 char_u buf[NUMBUFLEN];
9947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009948 title = get_tv_string_chk(&argvars[0]);
9949 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009950
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009951 if (title == NULL || initdir == NULL)
9952 rettv->vval.v_string = NULL;
9953 else
9954 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009955 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960}
9961
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009962static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009963
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964/*
9965 * Find a buffer by number or exact name.
9966 */
9967 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009968find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969{
9970 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009972 if (avar->v_type == VAR_NUMBER)
9973 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009974 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009977 if (buf == NULL)
9978 {
9979 /* No full path name match, try a match with a URL or a "nofile"
9980 * buffer, these don't use the full path. */
9981 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9982 if (buf->b_fname != NULL
9983 && (path_with_url(buf->b_fname)
9984#ifdef FEAT_QUICKFIX
9985 || bt_nofile(buf)
9986#endif
9987 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009988 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009989 break;
9990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 }
9992 return buf;
9993}
9994
9995/*
9996 * "bufexists(expr)" function
9997 */
9998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009999f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002}
10003
10004/*
10005 * "buflisted(expr)" function
10006 */
10007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010008f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010 buf_T *buf;
10011
10012 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014}
10015
10016/*
10017 * "bufloaded(expr)" function
10018 */
10019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010020f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
10022 buf_T *buf;
10023
10024 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010025 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026}
10027
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010028 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010029buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 int save_magic;
10032 char_u *save_cpo;
10033 buf_T *buf;
10034
Bram Moolenaar071d4272004-06-13 20:20:40 +000010035 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10036 save_magic = p_magic;
10037 p_magic = TRUE;
10038 save_cpo = p_cpo;
10039 p_cpo = (char_u *)"";
10040
10041 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010042 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 p_magic = save_magic;
10045 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010046 return buf;
10047}
10048
10049/*
10050 * Get buffer by number or pattern.
10051 */
10052 static buf_T *
10053get_buf_tv(typval_T *tv, int curtab_only)
10054{
10055 char_u *name = tv->vval.v_string;
10056 buf_T *buf;
10057
10058 if (tv->v_type == VAR_NUMBER)
10059 return buflist_findnr((int)tv->vval.v_number);
10060 if (tv->v_type != VAR_STRING)
10061 return NULL;
10062 if (name == NULL || *name == NUL)
10063 return curbuf;
10064 if (name[0] == '$' && name[1] == NUL)
10065 return lastbuf;
10066
10067 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
10069 /* If not found, try expanding the name, like done for bufexists(). */
10070 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010071 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072
10073 return buf;
10074}
10075
10076/*
10077 * "bufname(expr)" function
10078 */
10079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010080f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 buf_T *buf;
10083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010084 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010086 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010087 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010089 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010091 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 --emsg_off;
10093}
10094
10095/*
10096 * "bufnr(expr)" function
10097 */
10098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010099f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100{
10101 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010102 int error = FALSE;
10103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010108 --emsg_off;
10109
10110 /* If the buffer isn't found and the second argument is not zero create a
10111 * new buffer. */
10112 if (buf == NULL
10113 && argvars[1].v_type != VAR_UNKNOWN
10114 && get_tv_number_chk(&argvars[1], &error) != 0
10115 && !error
10116 && (name = get_tv_string_chk(&argvars[0])) != NULL
10117 && !error)
10118 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10119
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124}
10125
10126/*
10127 * "bufwinnr(nr)" function
10128 */
10129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010130f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131{
10132#ifdef FEAT_WINDOWS
10133 win_T *wp;
10134 int winnr = 0;
10135#endif
10136 buf_T *buf;
10137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010138 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010139 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010140 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141#ifdef FEAT_WINDOWS
10142 for (wp = firstwin; wp; wp = wp->w_next)
10143 {
10144 ++winnr;
10145 if (wp->w_buffer == buf)
10146 break;
10147 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010148 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010149#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151#endif
10152 --emsg_off;
10153}
10154
10155/*
10156 * "byte2line(byte)" function
10157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010159f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160{
10161#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163#else
10164 long boff = 0;
10165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010166 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010170 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 (linenr_T)0, &boff);
10172#endif
10173}
10174
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010176byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010177{
10178#ifdef FEAT_MBYTE
10179 char_u *t;
10180#endif
10181 char_u *str;
10182 long idx;
10183
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010184 str = get_tv_string_chk(&argvars[0]);
10185 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010186 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010187 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010188 return;
10189
10190#ifdef FEAT_MBYTE
10191 t = str;
10192 for ( ; idx > 0; idx--)
10193 {
10194 if (*t == NUL) /* EOL reached */
10195 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010196 if (enc_utf8 && comp)
10197 t += utf_ptr2len(t);
10198 else
10199 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010200 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010201 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010202#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010203 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010204 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010205#endif
10206}
10207
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010208/*
10209 * "byteidx()" function
10210 */
10211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010213{
10214 byteidx(argvars, rettv, FALSE);
10215}
10216
10217/*
10218 * "byteidxcomp()" function
10219 */
10220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010221f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010222{
10223 byteidx(argvars, rettv, TRUE);
10224}
10225
Bram Moolenaardb913952012-06-29 12:54:53 +020010226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010227func_call(
10228 char_u *name,
10229 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010230 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010231 dict_T *selfdict,
10232 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010233{
10234 listitem_T *item;
10235 typval_T argv[MAX_FUNC_ARGS + 1];
10236 int argc = 0;
10237 int dummy;
10238 int r = 0;
10239
10240 for (item = args->vval.v_list->lv_first; item != NULL;
10241 item = item->li_next)
10242 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010243 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010244 {
10245 EMSG(_("E699: Too many arguments"));
10246 break;
10247 }
10248 /* Make a copy of each argument. This is needed to be able to set
10249 * v_lock to VAR_FIXED in the copy without changing the original list.
10250 */
10251 copy_tv(&item->li_tv, &argv[argc++]);
10252 }
10253
10254 if (item == NULL)
10255 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10256 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010257 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010258
10259 /* Free the arguments. */
10260 while (argc > 0)
10261 clear_tv(&argv[--argc]);
10262
10263 return r;
10264}
10265
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010266/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010267 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268 */
10269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010270f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010271{
10272 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010273 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010274 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010275
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010276 if (argvars[1].v_type != VAR_LIST)
10277 {
10278 EMSG(_(e_listreq));
10279 return;
10280 }
10281 if (argvars[1].vval.v_list == NULL)
10282 return;
10283
10284 if (argvars[0].v_type == VAR_FUNC)
10285 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010286 else if (argvars[0].v_type == VAR_PARTIAL)
10287 {
10288 partial = argvars[0].vval.v_partial;
10289 func = partial->pt_name;
10290 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010291 else
10292 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010293 if (*func == NUL)
10294 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010295
Bram Moolenaare9a41262005-01-15 22:18:47 +000010296 if (argvars[2].v_type != VAR_UNKNOWN)
10297 {
10298 if (argvars[2].v_type != VAR_DICT)
10299 {
10300 EMSG(_(e_dictreq));
10301 return;
10302 }
10303 selfdict = argvars[2].vval.v_dict;
10304 }
10305
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010306 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010307}
10308
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010309#ifdef FEAT_FLOAT
10310/*
10311 * "ceil({float})" function
10312 */
10313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010314f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010315{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010316 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010317
10318 rettv->v_type = VAR_FLOAT;
10319 if (get_float_arg(argvars, &f) == OK)
10320 rettv->vval.v_float = ceil(f);
10321 else
10322 rettv->vval.v_float = 0.0;
10323}
10324#endif
10325
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010327/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010328 * "ch_close()" function
10329 */
10330 static void
10331f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10332{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010333 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010334
10335 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010336 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010337 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010338 channel_clear(channel);
10339 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010340}
10341
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010342/*
10343 * "ch_getbufnr()" function
10344 */
10345 static void
10346f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10347{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010348 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010349
10350 rettv->vval.v_number = -1;
10351 if (channel != NULL)
10352 {
10353 char_u *what = get_tv_string(&argvars[1]);
10354 int part;
10355
10356 if (STRCMP(what, "err") == 0)
10357 part = PART_ERR;
10358 else if (STRCMP(what, "out") == 0)
10359 part = PART_OUT;
10360 else if (STRCMP(what, "in") == 0)
10361 part = PART_IN;
10362 else
10363 part = PART_SOCK;
10364 if (channel->ch_part[part].ch_buffer != NULL)
10365 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10366 }
10367}
10368
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010369/*
10370 * "ch_getjob()" function
10371 */
10372 static void
10373f_ch_getjob(typval_T *argvars, typval_T *rettv)
10374{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010375 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010376
10377 if (channel != NULL)
10378 {
10379 rettv->v_type = VAR_JOB;
10380 rettv->vval.v_job = channel->ch_job;
10381 if (channel->ch_job != NULL)
10382 ++channel->ch_job->jv_refcount;
10383 }
10384}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010385
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010386/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010387 * "ch_info()" function
10388 */
10389 static void
10390f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10391{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010392 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010393
10394 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10395 channel_info(channel, rettv->vval.v_dict);
10396}
10397
10398/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010399 * "ch_log()" function
10400 */
10401 static void
10402f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10403{
10404 char_u *msg = get_tv_string(&argvars[0]);
10405 channel_T *channel = NULL;
10406
10407 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010408 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010409
10410 ch_log(channel, (char *)msg);
10411}
10412
10413/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010414 * "ch_logfile()" function
10415 */
10416 static void
10417f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10418{
10419 char_u *fname;
10420 char_u *opt = (char_u *)"";
10421 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010422
10423 fname = get_tv_string(&argvars[0]);
10424 if (argvars[1].v_type == VAR_STRING)
10425 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010426 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010427}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010428
10429/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010430 * "ch_open()" function
10431 */
10432 static void
10433f_ch_open(typval_T *argvars, typval_T *rettv)
10434{
Bram Moolenaar77073442016-02-13 23:23:53 +010010435 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010436 if (check_restricted() || check_secure())
10437 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010438 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010439}
10440
10441/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010442 * "ch_read()" function
10443 */
10444 static void
10445f_ch_read(typval_T *argvars, typval_T *rettv)
10446{
10447 common_channel_read(argvars, rettv, FALSE);
10448}
10449
10450/*
10451 * "ch_readraw()" function
10452 */
10453 static void
10454f_ch_readraw(typval_T *argvars, typval_T *rettv)
10455{
10456 common_channel_read(argvars, rettv, TRUE);
10457}
10458
10459/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010460 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010461 */
10462 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010463f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10464{
10465 ch_expr_common(argvars, rettv, TRUE);
10466}
10467
10468/*
10469 * "ch_sendexpr()" function
10470 */
10471 static void
10472f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10473{
10474 ch_expr_common(argvars, rettv, FALSE);
10475}
10476
10477/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010478 * "ch_evalraw()" function
10479 */
10480 static void
10481f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10482{
10483 ch_raw_common(argvars, rettv, TRUE);
10484}
10485
10486/*
10487 * "ch_sendraw()" function
10488 */
10489 static void
10490f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10491{
10492 ch_raw_common(argvars, rettv, FALSE);
10493}
10494
10495/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010496 * "ch_setoptions()" function
10497 */
10498 static void
10499f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10500{
10501 channel_T *channel;
10502 jobopt_T opt;
10503
Bram Moolenaar437905c2016-04-26 19:01:05 +020010504 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010505 if (channel == NULL)
10506 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010507 clear_job_options(&opt);
10508 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010509 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10510 channel_set_options(channel, &opt);
10511 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010512}
10513
10514/*
10515 * "ch_status()" function
10516 */
10517 static void
10518f_ch_status(typval_T *argvars, typval_T *rettv)
10519{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010520 channel_T *channel;
10521
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010522 /* return an empty string by default */
10523 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010524 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010525
Bram Moolenaar437905c2016-04-26 19:01:05 +020010526 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010527 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010528}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010529#endif
10530
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010531/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010532 * "changenr()" function
10533 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010535f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010536{
10537 rettv->vval.v_number = curbuf->b_u_seq_cur;
10538}
10539
10540/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 * "char2nr(string)" function
10542 */
10543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010544f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545{
10546#ifdef FEAT_MBYTE
10547 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010548 {
10549 int utf8 = 0;
10550
10551 if (argvars[1].v_type != VAR_UNKNOWN)
10552 utf8 = get_tv_number_chk(&argvars[1], NULL);
10553
10554 if (utf8)
10555 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10556 else
10557 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 else
10560#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010561 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562}
10563
10564/*
10565 * "cindent(lnum)" function
10566 */
10567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010568f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570#ifdef FEAT_CINDENT
10571 pos_T pos;
10572 linenr_T lnum;
10573
10574 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10577 {
10578 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580 curwin->w_cursor = pos;
10581 }
10582 else
10583#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010584 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585}
10586
10587/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010588 * "clearmatches()" function
10589 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010591f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010592{
10593#ifdef FEAT_SEARCH_EXTRA
10594 clear_matches(curwin);
10595#endif
10596}
10597
10598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 * "col(string)" function
10600 */
10601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010602f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 colnr_T col = 0;
10605 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010606 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010608 fp = var2fpos(&argvars[0], FALSE, &fnum);
10609 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610 {
10611 if (fp->col == MAXCOL)
10612 {
10613 /* '> can be MAXCOL, get the length of the line then */
10614 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010615 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 else
10617 col = MAXCOL;
10618 }
10619 else
10620 {
10621 col = fp->col + 1;
10622#ifdef FEAT_VIRTUALEDIT
10623 /* col(".") when the cursor is on the NUL at the end of the line
10624 * because of "coladd" can be seen as an extra column. */
10625 if (virtual_active() && fp == &curwin->w_cursor)
10626 {
10627 char_u *p = ml_get_cursor();
10628
10629 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10630 curwin->w_virtcol - curwin->w_cursor.coladd))
10631 {
10632# ifdef FEAT_MBYTE
10633 int l;
10634
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010635 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 col += l;
10637# else
10638 if (*p != NUL && p[1] == NUL)
10639 ++col;
10640# endif
10641 }
10642 }
10643#endif
10644 }
10645 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647}
10648
Bram Moolenaar572cb562005-08-05 21:35:02 +000010649#if defined(FEAT_INS_EXPAND)
10650/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010651 * "complete()" function
10652 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010654f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010655{
10656 int startcol;
10657
10658 if ((State & INSERT) == 0)
10659 {
10660 EMSG(_("E785: complete() can only be used in Insert mode"));
10661 return;
10662 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010663
10664 /* Check for undo allowed here, because if something was already inserted
10665 * the line was already saved for undo and this check isn't done. */
10666 if (!undo_allowed())
10667 return;
10668
Bram Moolenaarade00832006-03-10 21:46:58 +000010669 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10670 {
10671 EMSG(_(e_invarg));
10672 return;
10673 }
10674
10675 startcol = get_tv_number_chk(&argvars[0], NULL);
10676 if (startcol <= 0)
10677 return;
10678
10679 set_completion(startcol - 1, argvars[1].vval.v_list);
10680}
10681
10682/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010683 * "complete_add()" function
10684 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010686f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010687{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010688 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010689}
10690
10691/*
10692 * "complete_check()" function
10693 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010695f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010696{
10697 int saved = RedrawingDisabled;
10698
10699 RedrawingDisabled = 0;
10700 ins_compl_check_keys(0);
10701 rettv->vval.v_number = compl_interrupted;
10702 RedrawingDisabled = saved;
10703}
10704#endif
10705
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706/*
10707 * "confirm(message, buttons[, default [, type]])" function
10708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010710f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010711{
10712#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10713 char_u *message;
10714 char_u *buttons = NULL;
10715 char_u buf[NUMBUFLEN];
10716 char_u buf2[NUMBUFLEN];
10717 int def = 1;
10718 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010719 char_u *typestr;
10720 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010722 message = get_tv_string_chk(&argvars[0]);
10723 if (message == NULL)
10724 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010725 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010727 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10728 if (buttons == NULL)
10729 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010730 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010732 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010733 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010735 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10736 if (typestr == NULL)
10737 error = TRUE;
10738 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010740 switch (TOUPPER_ASC(*typestr))
10741 {
10742 case 'E': type = VIM_ERROR; break;
10743 case 'Q': type = VIM_QUESTION; break;
10744 case 'I': type = VIM_INFO; break;
10745 case 'W': type = VIM_WARNING; break;
10746 case 'G': type = VIM_GENERIC; break;
10747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 }
10749 }
10750 }
10751 }
10752
10753 if (buttons == NULL || *buttons == NUL)
10754 buttons = (char_u *)_("&Ok");
10755
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010756 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010757 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010758 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759#endif
10760}
10761
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010762/*
10763 * "copy()" function
10764 */
10765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010766f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010767{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010768 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010769}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010771#ifdef FEAT_FLOAT
10772/*
10773 * "cos()" function
10774 */
10775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010776f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010777{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010778 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010779
10780 rettv->v_type = VAR_FLOAT;
10781 if (get_float_arg(argvars, &f) == OK)
10782 rettv->vval.v_float = cos(f);
10783 else
10784 rettv->vval.v_float = 0.0;
10785}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010786
10787/*
10788 * "cosh()" function
10789 */
10790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010791f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010792{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010793 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010794
10795 rettv->v_type = VAR_FLOAT;
10796 if (get_float_arg(argvars, &f) == OK)
10797 rettv->vval.v_float = cosh(f);
10798 else
10799 rettv->vval.v_float = 0.0;
10800}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010801#endif
10802
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010804 * "count()" function
10805 */
10806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010807f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010808{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010809 long n = 0;
10810 int ic = FALSE;
10811
Bram Moolenaare9a41262005-01-15 22:18:47 +000010812 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010813 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010814 listitem_T *li;
10815 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010816 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010817
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 if ((l = argvars[0].vval.v_list) != NULL)
10819 {
10820 li = l->lv_first;
10821 if (argvars[2].v_type != VAR_UNKNOWN)
10822 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 int error = FALSE;
10824
10825 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010826 if (argvars[3].v_type != VAR_UNKNOWN)
10827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 idx = get_tv_number_chk(&argvars[3], &error);
10829 if (!error)
10830 {
10831 li = list_find(l, idx);
10832 if (li == NULL)
10833 EMSGN(_(e_listidx), idx);
10834 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010835 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 if (error)
10837 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010838 }
10839
10840 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010841 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010842 ++n;
10843 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 else if (argvars[0].v_type == VAR_DICT)
10846 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 int todo;
10848 dict_T *d;
10849 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010850
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010851 if ((d = argvars[0].vval.v_dict) != NULL)
10852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010853 int error = FALSE;
10854
Bram Moolenaare9a41262005-01-15 22:18:47 +000010855 if (argvars[2].v_type != VAR_UNKNOWN)
10856 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010857 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010858 if (argvars[3].v_type != VAR_UNKNOWN)
10859 EMSG(_(e_invarg));
10860 }
10861
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010862 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010863 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010864 {
10865 if (!HASHITEM_EMPTY(hi))
10866 {
10867 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010868 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010869 ++n;
10870 }
10871 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010872 }
10873 }
10874 else
10875 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010876 rettv->vval.v_number = n;
10877}
10878
10879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10881 *
10882 * Checks the existence of a cscope connection.
10883 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010885f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886{
10887#ifdef FEAT_CSCOPE
10888 int num = 0;
10889 char_u *dbpath = NULL;
10890 char_u *prepend = NULL;
10891 char_u buf[NUMBUFLEN];
10892
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010893 if (argvars[0].v_type != VAR_UNKNOWN
10894 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 num = (int)get_tv_number(&argvars[0]);
10897 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010898 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010899 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 }
10901
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010902 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903#endif
10904}
10905
10906/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010907 * "cursor(lnum, col)" function, or
10908 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010910 * Moves the cursor to the specified line and column.
10911 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010914f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915{
10916 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010917#ifdef FEAT_VIRTUALEDIT
10918 long coladd = 0;
10919#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010920 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010922 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010923 if (argvars[1].v_type == VAR_UNKNOWN)
10924 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010925 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010926 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010927
Bram Moolenaar493c1782014-05-28 14:34:46 +020010928 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010929 {
10930 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010931 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010932 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010933 line = pos.lnum;
10934 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010935#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010936 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010937#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010938 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010939 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010940 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010941 set_curswant = FALSE;
10942 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010943 }
10944 else
10945 {
10946 line = get_tv_lnum(argvars);
10947 col = get_tv_number_chk(&argvars[1], NULL);
10948#ifdef FEAT_VIRTUALEDIT
10949 if (argvars[2].v_type != VAR_UNKNOWN)
10950 coladd = get_tv_number_chk(&argvars[2], NULL);
10951#endif
10952 }
10953 if (line < 0 || col < 0
10954#ifdef FEAT_VIRTUALEDIT
10955 || coladd < 0
10956#endif
10957 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (line > 0)
10960 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961 if (col > 0)
10962 curwin->w_cursor.col = col - 1;
10963#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010964 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965#endif
10966
10967 /* Make sure the cursor is in a valid position. */
10968 check_cursor();
10969#ifdef FEAT_MBYTE
10970 /* Correct cursor for multi-byte character. */
10971 if (has_mbyte)
10972 mb_adjust_cursor();
10973#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010974
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010975 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010976 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
10979/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010980 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 */
10982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010983f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010985 int noref = 0;
10986
10987 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010988 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010989 if (noref < 0 || noref > 1)
10990 EMSG(_(e_invarg));
10991 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010992 {
10993 current_copyID += COPYID_INC;
10994 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996}
10997
10998/*
10999 * "delete()" function
11000 */
11001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011002f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011004 char_u nbuf[NUMBUFLEN];
11005 char_u *name;
11006 char_u *flags;
11007
11008 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011010 return;
11011
11012 name = get_tv_string(&argvars[0]);
11013 if (name == NULL || *name == NUL)
11014 {
11015 EMSG(_(e_invarg));
11016 return;
11017 }
11018
11019 if (argvars[1].v_type != VAR_UNKNOWN)
11020 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011022 flags = (char_u *)"";
11023
11024 if (*flags == NUL)
11025 /* delete a file */
11026 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11027 else if (STRCMP(flags, "d") == 0)
11028 /* delete an empty directory */
11029 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11030 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011031 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011032 rettv->vval.v_number = delete_recursive(name);
11033 else
11034 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011035}
11036
11037/*
11038 * "did_filetype()" function
11039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011041f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
11043#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011045#endif
11046}
11047
11048/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011049 * "diff_filler()" function
11050 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011052f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011053{
11054#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011056#endif
11057}
11058
11059/*
11060 * "diff_hlID()" function
11061 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011063f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011064{
11065#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011066 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011067 static linenr_T prev_lnum = 0;
11068 static int changedtick = 0;
11069 static int fnum = 0;
11070 static int change_start = 0;
11071 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011072 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011073 int filler_lines;
11074 int col;
11075
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011076 if (lnum < 0) /* ignore type error in {lnum} arg */
11077 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011078 if (lnum != prev_lnum
11079 || changedtick != curbuf->b_changedtick
11080 || fnum != curbuf->b_fnum)
11081 {
11082 /* New line, buffer, change: need to get the values. */
11083 filler_lines = diff_check(curwin, lnum);
11084 if (filler_lines < 0)
11085 {
11086 if (filler_lines == -1)
11087 {
11088 change_start = MAXCOL;
11089 change_end = -1;
11090 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11091 hlID = HLF_ADD; /* added line */
11092 else
11093 hlID = HLF_CHD; /* changed line */
11094 }
11095 else
11096 hlID = HLF_ADD; /* added line */
11097 }
11098 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011099 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011100 prev_lnum = lnum;
11101 changedtick = curbuf->b_changedtick;
11102 fnum = curbuf->b_fnum;
11103 }
11104
11105 if (hlID == HLF_CHD || hlID == HLF_TXD)
11106 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011107 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011108 if (col >= change_start && col <= change_end)
11109 hlID = HLF_TXD; /* changed text */
11110 else
11111 hlID = HLF_CHD; /* changed line */
11112 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011113 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011114#endif
11115}
11116
11117/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011118 * "disable_char_avail_for_testing({expr})" function
11119 */
11120 static void
11121f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11122{
11123 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11124}
11125
11126/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011127 * "empty({expr})" function
11128 */
11129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011130f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011131{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011132 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011133
11134 switch (argvars[0].v_type)
11135 {
11136 case VAR_STRING:
11137 case VAR_FUNC:
11138 n = argvars[0].vval.v_string == NULL
11139 || *argvars[0].vval.v_string == NUL;
11140 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011141 case VAR_PARTIAL:
11142 n = FALSE;
11143 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011144 case VAR_NUMBER:
11145 n = argvars[0].vval.v_number == 0;
11146 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011147 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011148#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011149 n = argvars[0].vval.v_float == 0.0;
11150 break;
11151#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011152 case VAR_LIST:
11153 n = argvars[0].vval.v_list == NULL
11154 || argvars[0].vval.v_list->lv_first == NULL;
11155 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011156 case VAR_DICT:
11157 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011158 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011159 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011160 case VAR_SPECIAL:
11161 n = argvars[0].vval.v_number != VVAL_TRUE;
11162 break;
11163
Bram Moolenaar835dc632016-02-07 14:27:38 +010011164 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011165#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011166 n = argvars[0].vval.v_job == NULL
11167 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11168 break;
11169#endif
11170 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011171#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011172 n = argvars[0].vval.v_channel == NULL
11173 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011174 break;
11175#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011176 case VAR_UNKNOWN:
11177 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11178 n = TRUE;
11179 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011180 }
11181
11182 rettv->vval.v_number = n;
11183}
11184
11185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011186 * "escape({string}, {chars})" function
11187 */
11188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011189f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190{
11191 char_u buf[NUMBUFLEN];
11192
Bram Moolenaar758711c2005-02-02 23:11:38 +000011193 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11194 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011195 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011196}
11197
11198/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011199 * "eval()" function
11200 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011202f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011203{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011204 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011205
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011206 s = get_tv_string_chk(&argvars[0]);
11207 if (s != NULL)
11208 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011209
Bram Moolenaar615b9972015-01-14 17:15:05 +010011210 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011211 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11212 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011213 if (p != NULL && !aborting())
11214 EMSG2(_(e_invexpr2), p);
11215 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011217 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011218 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011219 else if (*s != NUL)
11220 EMSG(_(e_trailing));
11221}
11222
11223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224 * "eventhandler()" function
11225 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011227f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230}
11231
11232/*
11233 * "executable()" function
11234 */
11235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011236f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011237{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011238 char_u *name = get_tv_string(&argvars[0]);
11239
11240 /* Check in $PATH and also check directly if there is a directory name. */
11241 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11242 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011243}
11244
11245/*
11246 * "exepath()" function
11247 */
11248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011249f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011250{
11251 char_u *p = NULL;
11252
Bram Moolenaarb5971142015-03-21 17:32:19 +010011253 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011254 rettv->v_type = VAR_STRING;
11255 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256}
11257
11258/*
11259 * "exists()" function
11260 */
11261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011262f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263{
11264 char_u *p;
11265 char_u *name;
11266 int n = FALSE;
11267 int len = 0;
11268
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011269 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270 if (*p == '$') /* environment variable */
11271 {
11272 /* first try "normal" environment variables (fast) */
11273 if (mch_getenv(p + 1) != NULL)
11274 n = TRUE;
11275 else
11276 {
11277 /* try expanding things like $VIM and ${HOME} */
11278 p = expand_env_save(p);
11279 if (p != NULL && *p != '$')
11280 n = TRUE;
11281 vim_free(p);
11282 }
11283 }
11284 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011286 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011287 if (*skipwhite(p) != NUL)
11288 n = FALSE; /* trailing garbage */
11289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290 else if (*p == '*') /* internal or user defined function */
11291 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011292 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293 }
11294 else if (*p == ':')
11295 {
11296 n = cmd_exists(p + 1);
11297 }
11298 else if (*p == '#')
11299 {
11300#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011301 if (p[1] == '#')
11302 n = autocmd_supported(p + 2);
11303 else
11304 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305#endif
11306 }
11307 else /* internal variable */
11308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011309 char_u *tofree;
11310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011312 /* get_name_len() takes care of expanding curly braces */
11313 name = p;
11314 len = get_name_len(&p, &tofree, TRUE, FALSE);
11315 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011317 if (tofree != NULL)
11318 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011319 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011320 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011322 /* handle d.key, l[idx], f(expr) */
11323 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11324 if (n)
11325 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011326 }
11327 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011328 if (*p != NUL)
11329 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011331 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 }
11333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335}
11336
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011337#ifdef FEAT_FLOAT
11338/*
11339 * "exp()" function
11340 */
11341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011342f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011343{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011344 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011345
11346 rettv->v_type = VAR_FLOAT;
11347 if (get_float_arg(argvars, &f) == OK)
11348 rettv->vval.v_float = exp(f);
11349 else
11350 rettv->vval.v_float = 0.0;
11351}
11352#endif
11353
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354/*
11355 * "expand()" function
11356 */
11357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011358f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359{
11360 char_u *s;
11361 int len;
11362 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011363 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011364 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011366 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011369 if (argvars[1].v_type != VAR_UNKNOWN
11370 && argvars[2].v_type != VAR_UNKNOWN
11371 && get_tv_number_chk(&argvars[2], &error)
11372 && !error)
11373 {
11374 rettv->v_type = VAR_LIST;
11375 rettv->vval.v_list = NULL;
11376 }
11377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011378 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 if (*s == '%' || *s == '#' || *s == '<')
11380 {
11381 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011382 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011384 if (rettv->v_type == VAR_LIST)
11385 {
11386 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11387 list_append_string(rettv->vval.v_list, result, -1);
11388 else
11389 vim_free(result);
11390 }
11391 else
11392 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 }
11394 else
11395 {
11396 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011397 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011398 if (argvars[1].v_type != VAR_UNKNOWN
11399 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011400 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 if (!error)
11402 {
11403 ExpandInit(&xpc);
11404 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011405 if (p_wic)
11406 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011407 if (rettv->v_type == VAR_STRING)
11408 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11409 options, WILD_ALL);
11410 else if (rettv_list_alloc(rettv) != FAIL)
11411 {
11412 int i;
11413
11414 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11415 for (i = 0; i < xpc.xp_numfiles; i++)
11416 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11417 ExpandCleanup(&xpc);
11418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011419 }
11420 else
11421 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 }
11423}
11424
11425/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011426 * Go over all entries in "d2" and add them to "d1".
11427 * When "action" is "error" then a duplicate key is an error.
11428 * When "action" is "force" then a duplicate key is overwritten.
11429 * Otherwise duplicate keys are ignored ("action" is "keep").
11430 */
11431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011432dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011433{
11434 dictitem_T *di1;
11435 hashitem_T *hi2;
11436 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011437 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011438
11439 todo = (int)d2->dv_hashtab.ht_used;
11440 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11441 {
11442 if (!HASHITEM_EMPTY(hi2))
11443 {
11444 --todo;
11445 di1 = dict_find(d1, hi2->hi_key, -1);
11446 if (d1->dv_scope != 0)
11447 {
11448 /* Disallow replacing a builtin function in l: and g:.
11449 * Check the key to be valid when adding to any
11450 * scope. */
11451 if (d1->dv_scope == VAR_DEF_SCOPE
11452 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11453 && var_check_func_name(hi2->hi_key,
11454 di1 == NULL))
11455 break;
11456 if (!valid_varname(hi2->hi_key))
11457 break;
11458 }
11459 if (di1 == NULL)
11460 {
11461 di1 = dictitem_copy(HI2DI(hi2));
11462 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11463 dictitem_free(di1);
11464 }
11465 else if (*action == 'e')
11466 {
11467 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11468 break;
11469 }
11470 else if (*action == 'f' && HI2DI(hi2) != di1)
11471 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011472 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11473 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011474 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011475 clear_tv(&di1->di_tv);
11476 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11477 }
11478 }
11479 }
11480}
11481
11482/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011483 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011484 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011485 */
11486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011487f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011488{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011489 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011490
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011492 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 list_T *l1, *l2;
11494 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011497
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498 l1 = argvars[0].vval.v_list;
11499 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011500 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011501 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 {
11503 if (argvars[2].v_type != VAR_UNKNOWN)
11504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011505 before = get_tv_number_chk(&argvars[2], &error);
11506 if (error)
11507 return; /* type error; errmsg already given */
11508
Bram Moolenaar758711c2005-02-02 23:11:38 +000011509 if (before == l1->lv_len)
11510 item = NULL;
11511 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011513 item = list_find(l1, before);
11514 if (item == NULL)
11515 {
11516 EMSGN(_(e_listidx), before);
11517 return;
11518 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011519 }
11520 }
11521 else
11522 item = NULL;
11523 list_extend(l1, l2, item);
11524
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 copy_tv(&argvars[0], rettv);
11526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011527 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011528 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11529 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011530 dict_T *d1, *d2;
11531 char_u *action;
11532 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011533
11534 d1 = argvars[0].vval.v_dict;
11535 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011536 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011537 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011538 {
11539 /* Check the third argument. */
11540 if (argvars[2].v_type != VAR_UNKNOWN)
11541 {
11542 static char *(av[]) = {"keep", "force", "error"};
11543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011544 action = get_tv_string_chk(&argvars[2]);
11545 if (action == NULL)
11546 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011547 for (i = 0; i < 3; ++i)
11548 if (STRCMP(action, av[i]) == 0)
11549 break;
11550 if (i == 3)
11551 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011552 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011553 return;
11554 }
11555 }
11556 else
11557 action = (char_u *)"force";
11558
Bram Moolenaara9922d62013-05-30 13:01:18 +020011559 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011560
Bram Moolenaare9a41262005-01-15 22:18:47 +000011561 copy_tv(&argvars[0], rettv);
11562 }
11563 }
11564 else
11565 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011566}
11567
11568/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011569 * "feedkeys()" function
11570 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011572f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011573{
11574 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011575 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011576 char_u *keys, *flags;
11577 char_u nbuf[NUMBUFLEN];
11578 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011579 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011580 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011581 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011582
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011583 /* This is not allowed in the sandbox. If the commands would still be
11584 * executed in the sandbox it would be OK, but it probably happens later,
11585 * when "sandbox" is no longer set. */
11586 if (check_secure())
11587 return;
11588
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011589 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011590
11591 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011592 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011593 flags = get_tv_string_buf(&argvars[1], nbuf);
11594 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011595 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011596 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011597 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011598 case 'n': remap = FALSE; break;
11599 case 'm': remap = TRUE; break;
11600 case 't': typed = TRUE; break;
11601 case 'i': insert = TRUE; break;
11602 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011603 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011604 }
11605 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011606 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011607
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011608 if (*keys != NUL || execute)
11609 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011610 /* Need to escape K_SPECIAL and CSI before putting the string in the
11611 * typeahead buffer. */
11612 keys_esc = vim_strsave_escape_csi(keys);
11613 if (keys_esc != NULL)
11614 {
11615 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011616 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011617 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011618 if (vgetc_busy)
11619 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011620 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011621 {
11622 int save_msg_scroll = msg_scroll;
11623
11624 /* Avoid a 1 second delay when the keys start Insert mode. */
11625 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011626
Bram Moolenaar245c4102016-04-20 17:37:41 +020011627 if (!dangerous)
11628 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011629 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011630 if (!dangerous)
11631 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011632 msg_scroll |= save_msg_scroll;
11633 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011634 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011635 }
11636}
11637
11638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 * "filereadable()" function
11640 */
11641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011642f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011644 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645 char_u *p;
11646 int n;
11647
Bram Moolenaarc236c162008-07-13 17:41:49 +000011648#ifndef O_NONBLOCK
11649# define O_NONBLOCK 0
11650#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011652 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11653 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 {
11655 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011656 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657 }
11658 else
11659 n = FALSE;
11660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662}
11663
11664/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011665 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011666 * rights to write into.
11667 */
11668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011669f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011671 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011672}
11673
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011674 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011675findfilendir(
11676 typval_T *argvars UNUSED,
11677 typval_T *rettv,
11678 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011679{
11680#ifdef FEAT_SEARCHPATH
11681 char_u *fname;
11682 char_u *fresult = NULL;
11683 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11684 char_u *p;
11685 char_u pathbuf[NUMBUFLEN];
11686 int count = 1;
11687 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011688 int error = FALSE;
11689#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011690
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011691 rettv->vval.v_string = NULL;
11692 rettv->v_type = VAR_STRING;
11693
11694#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011695 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011696
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011697 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011699 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11700 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011701 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011702 else
11703 {
11704 if (*p != NUL)
11705 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011708 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011709 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011710 }
11711
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011712 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11713 error = TRUE;
11714
11715 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011717 do
11718 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011719 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011720 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011721 fresult = find_file_in_path_option(first ? fname : NULL,
11722 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011723 0, first, path,
11724 find_what,
11725 curbuf->b_ffname,
11726 find_what == FINDFILE_DIR
11727 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011728 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011729
11730 if (fresult != NULL && rettv->v_type == VAR_LIST)
11731 list_append_string(rettv->vval.v_list, fresult, -1);
11732
11733 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011734 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011735
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011736 if (rettv->v_type == VAR_STRING)
11737 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011738#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011739}
11740
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011741static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11742static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011743
11744/*
11745 * Implementation of map() and filter().
11746 */
11747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011748filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011749{
11750 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011752 listitem_T *li, *nli;
11753 list_T *l = NULL;
11754 dictitem_T *di;
11755 hashtab_T *ht;
11756 hashitem_T *hi;
11757 dict_T *d = NULL;
11758 typval_T save_val;
11759 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011760 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011761 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011762 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011763 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011764 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011765 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011766 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011767
Bram Moolenaare9a41262005-01-15 22:18:47 +000011768 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011769 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011770 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011771 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772 return;
11773 }
11774 else if (argvars[0].v_type == VAR_DICT)
11775 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011776 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011777 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011778 return;
11779 }
11780 else
11781 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011782 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011783 return;
11784 }
11785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011786 expr = get_tv_string_buf_chk(&argvars[1], buf);
11787 /* On type errors, the preceding call has already displayed an error
11788 * message. Avoid a misleading error message for an empty string that
11789 * was not passed as argument. */
11790 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 prepare_vimvar(VV_VAL, &save_val);
11793 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011794
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011795 /* We reset "did_emsg" to be able to detect whether an error
11796 * occurred during evaluation of the expression. */
11797 save_did_emsg = did_emsg;
11798 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011799
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011800 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 vimvars[VV_KEY].vv_type = VAR_STRING;
11804
11805 ht = &d->dv_hashtab;
11806 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011807 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011809 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011810 if (!HASHITEM_EMPTY(hi))
11811 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011812 int r;
11813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 --todo;
11815 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011816 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011817 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11818 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011819 break;
11820 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011821 r = filter_map_one(&di->di_tv, expr, map, &rem);
11822 clear_tv(&vimvars[VV_KEY].vv_tv);
11823 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011824 break;
11825 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011826 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011827 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11828 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011829 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011830 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011831 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011832 }
11833 }
11834 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011835 }
11836 else
11837 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011838 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011840 for (li = l->lv_first; li != NULL; li = nli)
11841 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011842 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011843 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011844 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011845 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011846 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011847 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011848 break;
11849 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011850 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011851 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011852 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011853 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011854
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011855 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011857
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011858 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860
11861 copy_tv(&argvars[0], rettv);
11862}
11863
11864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011865filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866{
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011868 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011869 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011870
Bram Moolenaar33570922005-01-25 22:26:29 +000011871 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 s = expr;
11873 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011874 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011875 if (*s != NUL) /* check for trailing chars after expr */
11876 {
11877 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011878 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011879 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011880 }
11881 if (map)
11882 {
11883 /* map(): replace the list item value */
11884 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011885 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011886 *tv = rettv;
11887 }
11888 else
11889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 int error = FALSE;
11891
Bram Moolenaare9a41262005-01-15 22:18:47 +000011892 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011893 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011894 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011895 /* On type error, nothing has been removed; return FAIL to stop the
11896 * loop. The error message was given by get_tv_number_chk(). */
11897 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011898 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011899 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011900 retval = OK;
11901theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011903 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011904}
11905
11906/*
11907 * "filter()" function
11908 */
11909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011910f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011911{
11912 filter_map(argvars, rettv, FALSE);
11913}
11914
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011915/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916 * "finddir({fname}[, {path}[, {count}]])" function
11917 */
11918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011919f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011920{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011921 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011922}
11923
11924/*
11925 * "findfile({fname}[, {path}[, {count}]])" function
11926 */
11927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011928f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011929{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011930 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011931}
11932
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011933#ifdef FEAT_FLOAT
11934/*
11935 * "float2nr({float})" function
11936 */
11937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011940 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011941
11942 if (get_float_arg(argvars, &f) == OK)
11943 {
11944 if (f < -0x7fffffff)
11945 rettv->vval.v_number = -0x7fffffff;
11946 else if (f > 0x7fffffff)
11947 rettv->vval.v_number = 0x7fffffff;
11948 else
11949 rettv->vval.v_number = (varnumber_T)f;
11950 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011951}
11952
11953/*
11954 * "floor({float})" function
11955 */
11956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011957f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011958{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011959 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011960
11961 rettv->v_type = VAR_FLOAT;
11962 if (get_float_arg(argvars, &f) == OK)
11963 rettv->vval.v_float = floor(f);
11964 else
11965 rettv->vval.v_float = 0.0;
11966}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011967
11968/*
11969 * "fmod()" function
11970 */
11971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011972f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011973{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011974 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011975
11976 rettv->v_type = VAR_FLOAT;
11977 if (get_float_arg(argvars, &fx) == OK
11978 && get_float_arg(&argvars[1], &fy) == OK)
11979 rettv->vval.v_float = fmod(fx, fy);
11980 else
11981 rettv->vval.v_float = 0.0;
11982}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011983#endif
11984
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011986 * "fnameescape({string})" function
11987 */
11988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011989f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011990{
11991 rettv->vval.v_string = vim_strsave_fnameescape(
11992 get_tv_string(&argvars[0]), FALSE);
11993 rettv->v_type = VAR_STRING;
11994}
11995
11996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 * "fnamemodify({fname}, {mods})" function
11998 */
11999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012000f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001{
12002 char_u *fname;
12003 char_u *mods;
12004 int usedlen = 0;
12005 int len;
12006 char_u *fbuf = NULL;
12007 char_u buf[NUMBUFLEN];
12008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012009 fname = get_tv_string_chk(&argvars[0]);
12010 mods = get_tv_string_buf_chk(&argvars[1], buf);
12011 if (fname == NULL || mods == NULL)
12012 fname = NULL;
12013 else
12014 {
12015 len = (int)STRLEN(fname);
12016 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012019 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 vim_free(fbuf);
12025}
12026
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012027static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012028
12029/*
12030 * "foldclosed()" function
12031 */
12032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012033foldclosed_both(
12034 typval_T *argvars UNUSED,
12035 typval_T *rettv,
12036 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037{
12038#ifdef FEAT_FOLDING
12039 linenr_T lnum;
12040 linenr_T first, last;
12041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012042 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12044 {
12045 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12046 {
12047 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012048 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012050 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051 return;
12052 }
12053 }
12054#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012055 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056}
12057
12058/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012059 * "foldclosed()" function
12060 */
12061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012062f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063{
12064 foldclosed_both(argvars, rettv, FALSE);
12065}
12066
12067/*
12068 * "foldclosedend()" function
12069 */
12070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012071f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012072{
12073 foldclosed_both(argvars, rettv, TRUE);
12074}
12075
12076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 * "foldlevel()" function
12078 */
12079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012080f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
12082#ifdef FEAT_FOLDING
12083 linenr_T lnum;
12084
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012085 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012087 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012088#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089}
12090
12091/*
12092 * "foldtext()" function
12093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012095f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012096{
12097#ifdef FEAT_FOLDING
12098 linenr_T lnum;
12099 char_u *s;
12100 char_u *r;
12101 int len;
12102 char *txt;
12103#endif
12104
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012105 rettv->v_type = VAR_STRING;
12106 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012108 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12109 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12110 <= curbuf->b_ml.ml_line_count
12111 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 {
12113 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012114 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12115 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 {
12117 if (!linewhite(lnum))
12118 break;
12119 ++lnum;
12120 }
12121
12122 /* Find interesting text in this line. */
12123 s = skipwhite(ml_get(lnum));
12124 /* skip C comment-start */
12125 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012126 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012127 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012128 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012129 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012130 {
12131 s = skipwhite(ml_get(lnum + 1));
12132 if (*s == '*')
12133 s = skipwhite(s + 1);
12134 }
12135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136 txt = _("+-%s%3ld lines: ");
12137 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012138 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 + 20 /* for %3ld */
12140 + STRLEN(s))); /* concatenated */
12141 if (r != NULL)
12142 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012143 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12144 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12145 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146 len = (int)STRLEN(r);
12147 STRCAT(r, s);
12148 /* remove 'foldmarker' and 'commentstring' */
12149 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012150 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 }
12152 }
12153#endif
12154}
12155
12156/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012157 * "foldtextresult(lnum)" function
12158 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012161{
12162#ifdef FEAT_FOLDING
12163 linenr_T lnum;
12164 char_u *text;
12165 char_u buf[51];
12166 foldinfo_T foldinfo;
12167 int fold_count;
12168#endif
12169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012170 rettv->v_type = VAR_STRING;
12171 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012172#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012174 /* treat illegal types and illegal string values for {lnum} the same */
12175 if (lnum < 0)
12176 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012177 fold_count = foldedCount(curwin, lnum, &foldinfo);
12178 if (fold_count > 0)
12179 {
12180 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12181 &foldinfo, buf);
12182 if (text == buf)
12183 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012184 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012185 }
12186#endif
12187}
12188
12189/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 * "foreground()" function
12191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012193f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195#ifdef FEAT_GUI
12196 if (gui.in_use)
12197 gui_mch_set_foreground();
12198#else
12199# ifdef WIN32
12200 win32_set_foreground();
12201# endif
12202#endif
12203}
12204
12205/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012206 * "function()" function
12207 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012209f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012210{
12211 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012212 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012213 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012214 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012215
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012216 if (argvars[0].v_type == VAR_FUNC)
12217 {
12218 /* function(MyFunc, [arg], dict) */
12219 s = argvars[0].vval.v_string;
12220 }
12221 else if (argvars[0].v_type == VAR_PARTIAL
12222 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012223 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012224 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012225 arg_pt = argvars[0].vval.v_partial;
12226 s = arg_pt->pt_name;
12227 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012228 else
12229 {
12230 /* function('MyFunc', [arg], dict) */
12231 s = get_tv_string(&argvars[0]);
12232 use_string = TRUE;
12233 }
12234
12235 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012236 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012237 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012238 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12239 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012240 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012241 else
12242 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012243 int dict_idx = 0;
12244 int arg_idx = 0;
12245 list_T *list = NULL;
12246
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012247 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012248 {
12249 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012250 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012251
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012252 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12253 * also be called from another script. Using trans_function_name()
12254 * would also work, but some plugins depend on the name being
12255 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012256 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012257 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12258 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012259 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012260 STRCPY(name, sid_buf);
12261 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012262 }
12263 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012264 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012265 name = vim_strsave(s);
12266
12267 if (argvars[1].v_type != VAR_UNKNOWN)
12268 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012269 if (argvars[2].v_type != VAR_UNKNOWN)
12270 {
12271 /* function(name, [args], dict) */
12272 arg_idx = 1;
12273 dict_idx = 2;
12274 }
12275 else if (argvars[1].v_type == VAR_DICT)
12276 /* function(name, dict) */
12277 dict_idx = 1;
12278 else
12279 /* function(name, [args]) */
12280 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012281 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012282 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012283 if (argvars[dict_idx].v_type != VAR_DICT)
12284 {
12285 EMSG(_("E922: expected a dict"));
12286 vim_free(name);
12287 return;
12288 }
12289 if (argvars[dict_idx].vval.v_dict == NULL)
12290 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012291 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012292 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012293 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012294 if (argvars[arg_idx].v_type != VAR_LIST)
12295 {
12296 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12297 vim_free(name);
12298 return;
12299 }
12300 list = argvars[arg_idx].vval.v_list;
12301 if (list == NULL || list->lv_len == 0)
12302 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012303 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012304 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012305 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012306 {
12307 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012308
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012309 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012310 if (pt == NULL)
12311 vim_free(name);
12312 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012313 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012314 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012315 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012316 listitem_T *li;
12317 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012318 int arg_len = 0;
12319 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012320
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012321 if (arg_pt != NULL)
12322 arg_len = arg_pt->pt_argc;
12323 if (list != NULL)
12324 lv_len = list->lv_len;
12325 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012326 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012327 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012328 if (pt->pt_argv == NULL)
12329 {
12330 vim_free(pt);
12331 vim_free(name);
12332 return;
12333 }
12334 else
12335 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012336 for (i = 0; i < arg_len; i++)
12337 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12338 if (lv_len > 0)
12339 for (li = list->lv_first; li != NULL;
12340 li = li->li_next)
12341 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012342 }
12343 }
12344
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012345 /* For "function(dict.func, [], dict)" and "func" is a partial
12346 * use "dict". That is backwards compatible. */
12347 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012348 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012349 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012350 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12351 ++pt->pt_dict->dv_refcount;
12352 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012353 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012354 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012355 /* If the dict was bound automatically the result is also
12356 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012357 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012358 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012359 if (pt->pt_dict != NULL)
12360 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012361 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012362
12363 pt->pt_refcount = 1;
12364 pt->pt_name = name;
12365 func_ref(pt->pt_name);
12366 }
12367 rettv->v_type = VAR_PARTIAL;
12368 rettv->vval.v_partial = pt;
12369 }
12370 else
12371 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012372 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012373 rettv->v_type = VAR_FUNC;
12374 rettv->vval.v_string = name;
12375 func_ref(name);
12376 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012377 }
12378}
12379
12380/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012381 * "garbagecollect()" function
12382 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012384f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012385{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012386 /* This is postponed until we are back at the toplevel, because we may be
12387 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12388 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012389
12390 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12391 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012392}
12393
12394/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012395 * "get()" function
12396 */
12397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012398f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012399{
Bram Moolenaar33570922005-01-25 22:26:29 +000012400 listitem_T *li;
12401 list_T *l;
12402 dictitem_T *di;
12403 dict_T *d;
12404 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405
Bram Moolenaare9a41262005-01-15 22:18:47 +000012406 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012407 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012408 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 int error = FALSE;
12411
12412 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12413 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012414 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012415 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012417 else if (argvars[0].v_type == VAR_DICT)
12418 {
12419 if ((d = argvars[0].vval.v_dict) != NULL)
12420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012421 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012422 if (di != NULL)
12423 tv = &di->di_tv;
12424 }
12425 }
12426 else
12427 EMSG2(_(e_listdictarg), "get()");
12428
12429 if (tv == NULL)
12430 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012431 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012432 copy_tv(&argvars[2], rettv);
12433 }
12434 else
12435 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012436}
12437
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012438static 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 +000012439
12440/*
12441 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012442 * Return a range (from start to end) of lines in rettv from the specified
12443 * buffer.
12444 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012445 */
12446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012447get_buffer_lines(
12448 buf_T *buf,
12449 linenr_T start,
12450 linenr_T end,
12451 int retlist,
12452 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012453{
12454 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012455
Bram Moolenaar959a1432013-12-14 12:17:38 +010012456 rettv->v_type = VAR_STRING;
12457 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012458 if (retlist && rettv_list_alloc(rettv) == FAIL)
12459 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012460
12461 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12462 return;
12463
12464 if (!retlist)
12465 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012466 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12467 p = ml_get_buf(buf, start, FALSE);
12468 else
12469 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012470 rettv->vval.v_string = vim_strsave(p);
12471 }
12472 else
12473 {
12474 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012475 return;
12476
12477 if (start < 1)
12478 start = 1;
12479 if (end > buf->b_ml.ml_line_count)
12480 end = buf->b_ml.ml_line_count;
12481 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012482 if (list_append_string(rettv->vval.v_list,
12483 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012484 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012485 }
12486}
12487
12488/*
12489 * "getbufline()" function
12490 */
12491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012492f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012493{
12494 linenr_T lnum;
12495 linenr_T end;
12496 buf_T *buf;
12497
12498 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12499 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012500 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012501 --emsg_off;
12502
Bram Moolenaar661b1822005-07-28 22:36:45 +000012503 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012504 if (argvars[2].v_type == VAR_UNKNOWN)
12505 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012506 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012507 end = get_tv_lnum_buf(&argvars[2], buf);
12508
Bram Moolenaar342337a2005-07-21 21:11:17 +000012509 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012510}
12511
Bram Moolenaar0d660222005-01-07 21:51:51 +000012512/*
12513 * "getbufvar()" function
12514 */
12515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012516f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517{
12518 buf_T *buf;
12519 buf_T *save_curbuf;
12520 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012521 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012522 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012524 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12525 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012527 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012528
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012529 rettv->v_type = VAR_STRING;
12530 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012531
12532 if (buf != NULL && varname != NULL)
12533 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012534 /* set curbuf to be our buf, temporarily */
12535 save_curbuf = curbuf;
12536 curbuf = buf;
12537
Bram Moolenaar0d660222005-01-07 21:51:51 +000012538 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012539 {
12540 if (get_option_tv(&varname, rettv, TRUE) == OK)
12541 done = TRUE;
12542 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012543 else if (STRCMP(varname, "changedtick") == 0)
12544 {
12545 rettv->v_type = VAR_NUMBER;
12546 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012547 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012548 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012549 else
12550 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012551 /* Look up the variable. */
12552 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12553 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12554 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012555 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012556 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012557 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012558 done = TRUE;
12559 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012560 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012561
12562 /* restore previous notion of curbuf */
12563 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564 }
12565
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012566 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12567 /* use the default value */
12568 copy_tv(&argvars[2], rettv);
12569
Bram Moolenaar0d660222005-01-07 21:51:51 +000012570 --emsg_off;
12571}
12572
12573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 * "getchar()" function
12575 */
12576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012577f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578{
12579 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012580 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012582 /* Position the cursor. Needed after a message that ends in a space. */
12583 windgoto(msg_row, msg_col);
12584
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 ++no_mapping;
12586 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012587 for (;;)
12588 {
12589 if (argvars[0].v_type == VAR_UNKNOWN)
12590 /* getchar(): blocking wait. */
12591 n = safe_vgetc();
12592 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12593 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012594 n = vpeekc_any();
12595 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012596 /* illegal argument or getchar(0) and no char avail: return zero */
12597 n = 0;
12598 else
12599 /* getchar(0) and char avail: return char */
12600 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012601
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012602 if (n == K_IGNORE)
12603 continue;
12604 break;
12605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 --no_mapping;
12607 --allow_keys;
12608
Bram Moolenaar219b8702006-11-01 14:32:36 +000012609 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12610 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12611 vimvars[VV_MOUSE_COL].vv_nr = 0;
12612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012613 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 if (IS_SPECIAL(n) || mod_mask != 0)
12615 {
12616 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12617 int i = 0;
12618
12619 /* Turn a special key into three bytes, plus modifier. */
12620 if (mod_mask != 0)
12621 {
12622 temp[i++] = K_SPECIAL;
12623 temp[i++] = KS_MODIFIER;
12624 temp[i++] = mod_mask;
12625 }
12626 if (IS_SPECIAL(n))
12627 {
12628 temp[i++] = K_SPECIAL;
12629 temp[i++] = K_SECOND(n);
12630 temp[i++] = K_THIRD(n);
12631 }
12632#ifdef FEAT_MBYTE
12633 else if (has_mbyte)
12634 i += (*mb_char2bytes)(n, temp + i);
12635#endif
12636 else
12637 temp[i++] = n;
12638 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639 rettv->v_type = VAR_STRING;
12640 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012641
12642#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012643 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012644 {
12645 int row = mouse_row;
12646 int col = mouse_col;
12647 win_T *win;
12648 linenr_T lnum;
12649# ifdef FEAT_WINDOWS
12650 win_T *wp;
12651# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012652 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012653
12654 if (row >= 0 && col >= 0)
12655 {
12656 /* Find the window at the mouse coordinates and compute the
12657 * text position. */
12658 win = mouse_find_win(&row, &col);
12659 (void)mouse_comp_pos(win, &row, &col, &lnum);
12660# ifdef FEAT_WINDOWS
12661 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012662 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012663# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012664 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012665 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12666 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12667 }
12668 }
12669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670 }
12671}
12672
12673/*
12674 * "getcharmod()" function
12675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012677f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012679 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680}
12681
12682/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012683 * "getcharsearch()" function
12684 */
12685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012686f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012687{
12688 if (rettv_dict_alloc(rettv) != FAIL)
12689 {
12690 dict_T *dict = rettv->vval.v_dict;
12691
12692 dict_add_nr_str(dict, "char", 0L, last_csearch());
12693 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12694 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12695 }
12696}
12697
12698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699 * "getcmdline()" function
12700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012702f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704 rettv->v_type = VAR_STRING;
12705 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706}
12707
12708/*
12709 * "getcmdpos()" function
12710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012718 * "getcmdtype()" function
12719 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012722{
12723 rettv->v_type = VAR_STRING;
12724 rettv->vval.v_string = alloc(2);
12725 if (rettv->vval.v_string != NULL)
12726 {
12727 rettv->vval.v_string[0] = get_cmdline_type();
12728 rettv->vval.v_string[1] = NUL;
12729 }
12730}
12731
12732/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012733 * "getcmdwintype()" function
12734 */
12735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012736f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012737{
12738 rettv->v_type = VAR_STRING;
12739 rettv->vval.v_string = NULL;
12740#ifdef FEAT_CMDWIN
12741 rettv->vval.v_string = alloc(2);
12742 if (rettv->vval.v_string != NULL)
12743 {
12744 rettv->vval.v_string[0] = cmdwin_type;
12745 rettv->vval.v_string[1] = NUL;
12746 }
12747#endif
12748}
12749
12750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 * "getcwd()" function
12752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012754f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012756 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012757 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012760 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012761
12762 wp = find_tabwin(&argvars[0], &argvars[1]);
12763 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012765 if (wp->w_localdir != NULL)
12766 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12767 else if(globaldir != NULL)
12768 rettv->vval.v_string = vim_strsave(globaldir);
12769 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012770 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012771 cwd = alloc(MAXPATHL);
12772 if (cwd != NULL)
12773 {
12774 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12775 rettv->vval.v_string = vim_strsave(cwd);
12776 vim_free(cwd);
12777 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012778 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012779#ifdef BACKSLASH_IN_FILENAME
12780 if (rettv->vval.v_string != NULL)
12781 slash_adjust(rettv->vval.v_string);
12782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783 }
12784}
12785
12786/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012787 * "getfontname()" function
12788 */
12789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012790f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012791{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->v_type = VAR_STRING;
12793 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012794#ifdef FEAT_GUI
12795 if (gui.in_use)
12796 {
12797 GuiFont font;
12798 char_u *name = NULL;
12799
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012800 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012801 {
12802 /* Get the "Normal" font. Either the name saved by
12803 * hl_set_font_name() or from the font ID. */
12804 font = gui.norm_font;
12805 name = hl_get_font_name();
12806 }
12807 else
12808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012810 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12811 return;
12812 font = gui_mch_get_font(name, FALSE);
12813 if (font == NOFONT)
12814 return; /* Invalid font name, return empty string. */
12815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012817 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012818 gui_mch_free_font(font);
12819 }
12820#endif
12821}
12822
12823/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012824 * "getfperm({fname})" function
12825 */
12826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012827f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012828{
12829 char_u *fname;
12830 struct stat st;
12831 char_u *perm = NULL;
12832 char_u flags[] = "rwx";
12833 int i;
12834
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012838 if (mch_stat((char *)fname, &st) >= 0)
12839 {
12840 perm = vim_strsave((char_u *)"---------");
12841 if (perm != NULL)
12842 {
12843 for (i = 0; i < 9; i++)
12844 {
12845 if (st.st_mode & (1 << (8 - i)))
12846 perm[i] = flags[i % 3];
12847 }
12848 }
12849 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012851}
12852
12853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 * "getfsize({fname})" function
12855 */
12856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012857f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
12859 char_u *fname;
12860 struct stat st;
12861
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865
12866 if (mch_stat((char *)fname, &st) >= 0)
12867 {
12868 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012871 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012873
12874 /* non-perfect check for overflow */
12875 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12876 rettv->vval.v_number = -2;
12877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 }
12879 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881}
12882
12883/*
12884 * "getftime({fname})" function
12885 */
12886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012887f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012888{
12889 char_u *fname;
12890 struct stat st;
12891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893
12894 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898}
12899
12900/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012901 * "getftype({fname})" function
12902 */
12903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012904f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012905{
12906 char_u *fname;
12907 struct stat st;
12908 char_u *type = NULL;
12909 char *t;
12910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012914 if (mch_lstat((char *)fname, &st) >= 0)
12915 {
12916#ifdef S_ISREG
12917 if (S_ISREG(st.st_mode))
12918 t = "file";
12919 else if (S_ISDIR(st.st_mode))
12920 t = "dir";
12921# ifdef S_ISLNK
12922 else if (S_ISLNK(st.st_mode))
12923 t = "link";
12924# endif
12925# ifdef S_ISBLK
12926 else if (S_ISBLK(st.st_mode))
12927 t = "bdev";
12928# endif
12929# ifdef S_ISCHR
12930 else if (S_ISCHR(st.st_mode))
12931 t = "cdev";
12932# endif
12933# ifdef S_ISFIFO
12934 else if (S_ISFIFO(st.st_mode))
12935 t = "fifo";
12936# endif
12937# ifdef S_ISSOCK
12938 else if (S_ISSOCK(st.st_mode))
12939 t = "fifo";
12940# endif
12941 else
12942 t = "other";
12943#else
12944# ifdef S_IFMT
12945 switch (st.st_mode & S_IFMT)
12946 {
12947 case S_IFREG: t = "file"; break;
12948 case S_IFDIR: t = "dir"; break;
12949# ifdef S_IFLNK
12950 case S_IFLNK: t = "link"; break;
12951# endif
12952# ifdef S_IFBLK
12953 case S_IFBLK: t = "bdev"; break;
12954# endif
12955# ifdef S_IFCHR
12956 case S_IFCHR: t = "cdev"; break;
12957# endif
12958# ifdef S_IFIFO
12959 case S_IFIFO: t = "fifo"; break;
12960# endif
12961# ifdef S_IFSOCK
12962 case S_IFSOCK: t = "socket"; break;
12963# endif
12964 default: t = "other";
12965 }
12966# else
12967 if (mch_isdir(fname))
12968 t = "dir";
12969 else
12970 t = "file";
12971# endif
12972#endif
12973 type = vim_strsave((char_u *)t);
12974 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012975 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012976}
12977
12978/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012979 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012980 */
12981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012982f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012983{
12984 linenr_T lnum;
12985 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012986 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012987
12988 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012989 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012990 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012991 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012992 retlist = FALSE;
12993 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012994 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012995 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012996 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012997 retlist = TRUE;
12998 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012999
Bram Moolenaar342337a2005-07-21 21:11:17 +000013000 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013001}
13002
13003/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013004 * "getmatches()" function
13005 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013007f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013008{
13009#ifdef FEAT_SEARCH_EXTRA
13010 dict_T *dict;
13011 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013012 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013013
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013014 if (rettv_list_alloc(rettv) == OK)
13015 {
13016 while (cur != NULL)
13017 {
13018 dict = dict_alloc();
13019 if (dict == NULL)
13020 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013021 if (cur->match.regprog == NULL)
13022 {
13023 /* match added with matchaddpos() */
13024 for (i = 0; i < MAXPOSMATCH; ++i)
13025 {
13026 llpos_T *llpos;
13027 char buf[6];
13028 list_T *l;
13029
13030 llpos = &cur->pos.pos[i];
13031 if (llpos->lnum == 0)
13032 break;
13033 l = list_alloc();
13034 if (l == NULL)
13035 break;
13036 list_append_number(l, (varnumber_T)llpos->lnum);
13037 if (llpos->col > 0)
13038 {
13039 list_append_number(l, (varnumber_T)llpos->col);
13040 list_append_number(l, (varnumber_T)llpos->len);
13041 }
13042 sprintf(buf, "pos%d", i + 1);
13043 dict_add_list(dict, buf, l);
13044 }
13045 }
13046 else
13047 {
13048 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13049 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013050 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013051 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13052 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013053# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013054 if (cur->conceal_char)
13055 {
13056 char_u buf[MB_MAXBYTES + 1];
13057
13058 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13059 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13060 }
13061# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013062 list_append_dict(rettv->vval.v_list, dict);
13063 cur = cur->next;
13064 }
13065 }
13066#endif
13067}
13068
13069/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013070 * "getpid()" function
13071 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013073f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013074{
13075 rettv->vval.v_number = mch_get_pid();
13076}
13077
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013079getpos_both(
13080 typval_T *argvars,
13081 typval_T *rettv,
13082 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013083{
Bram Moolenaara5525202006-03-02 22:52:09 +000013084 pos_T *fp;
13085 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013086 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013087
13088 if (rettv_list_alloc(rettv) == OK)
13089 {
13090 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013091 if (getcurpos)
13092 fp = &curwin->w_cursor;
13093 else
13094 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013095 if (fnum != -1)
13096 list_append_number(l, (varnumber_T)fnum);
13097 else
13098 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013099 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13100 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013101 list_append_number(l, (fp != NULL)
13102 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013103 : (varnumber_T)0);
13104 list_append_number(l,
13105#ifdef FEAT_VIRTUALEDIT
13106 (fp != NULL) ? (varnumber_T)fp->coladd :
13107#endif
13108 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013109 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013110 {
13111 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013112 list_append_number(l, curwin->w_curswant == MAXCOL ?
13113 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013114 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013115 }
13116 else
13117 rettv->vval.v_number = FALSE;
13118}
13119
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013120
13121/*
13122 * "getcurpos()" function
13123 */
13124 static void
13125f_getcurpos(typval_T *argvars, typval_T *rettv)
13126{
13127 getpos_both(argvars, rettv, TRUE);
13128}
13129
13130/*
13131 * "getpos(string)" function
13132 */
13133 static void
13134f_getpos(typval_T *argvars, typval_T *rettv)
13135{
13136 getpos_both(argvars, rettv, FALSE);
13137}
13138
Bram Moolenaara5525202006-03-02 22:52:09 +000013139/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013140 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013141 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013143f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013144{
13145#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013146 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013147#endif
13148
Bram Moolenaar2641f772005-03-25 21:58:17 +000013149#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013150 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013151 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013152 wp = NULL;
13153 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13154 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013155 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013156 if (wp == NULL)
13157 return;
13158 }
13159
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013160 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013161 }
13162#endif
13163}
13164
13165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 * "getreg()" function
13167 */
13168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013169f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170{
13171 char_u *strregname;
13172 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013173 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013174 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013175 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013177 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013178 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013179 strregname = get_tv_string_chk(&argvars[0]);
13180 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013181 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013182 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013183 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013184 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13185 return_list = get_tv_number_chk(&argvars[2], &error);
13186 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013189 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013190
13191 if (error)
13192 return;
13193
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 regname = (strregname == NULL ? '"' : *strregname);
13195 if (regname == 0)
13196 regname = '"';
13197
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013198 if (return_list)
13199 {
13200 rettv->v_type = VAR_LIST;
13201 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13202 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013203 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013204 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013205 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013206 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013207 }
13208 else
13209 {
13210 rettv->v_type = VAR_STRING;
13211 rettv->vval.v_string = get_reg_contents(regname,
13212 arg2 ? GREG_EXPR_SRC : 0);
13213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214}
13215
13216/*
13217 * "getregtype()" function
13218 */
13219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013220f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013221{
13222 char_u *strregname;
13223 int regname;
13224 char_u buf[NUMBUFLEN + 2];
13225 long reglen = 0;
13226
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013227 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013228 {
13229 strregname = get_tv_string_chk(&argvars[0]);
13230 if (strregname == NULL) /* type error; errmsg already given */
13231 {
13232 rettv->v_type = VAR_STRING;
13233 rettv->vval.v_string = NULL;
13234 return;
13235 }
13236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 else
13238 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013239 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240
13241 regname = (strregname == NULL ? '"' : *strregname);
13242 if (regname == 0)
13243 regname = '"';
13244
13245 buf[0] = NUL;
13246 buf[1] = NUL;
13247 switch (get_reg_type(regname, &reglen))
13248 {
13249 case MLINE: buf[0] = 'V'; break;
13250 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251 case MBLOCK:
13252 buf[0] = Ctrl_V;
13253 sprintf((char *)buf + 1, "%ld", reglen + 1);
13254 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013256 rettv->v_type = VAR_STRING;
13257 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013258}
13259
13260/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013261 * "gettabvar()" function
13262 */
13263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013264f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013265{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013266 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013267 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013268 dictitem_T *v;
13269 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013270 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013271
13272 rettv->v_type = VAR_STRING;
13273 rettv->vval.v_string = NULL;
13274
13275 varname = get_tv_string_chk(&argvars[1]);
13276 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13277 if (tp != NULL && varname != NULL)
13278 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013279 /* Set tp to be our tabpage, temporarily. Also set the window to the
13280 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013281 if (switch_win(&oldcurwin, &oldtabpage,
13282 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013283 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013284 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013285 /* look up the variable */
13286 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13287 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13288 if (v != NULL)
13289 {
13290 copy_tv(&v->di_tv, rettv);
13291 done = TRUE;
13292 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013293 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013294
13295 /* restore previous notion of curwin */
13296 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013297 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013298
13299 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013300 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013301}
13302
13303/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013304 * "gettabwinvar()" function
13305 */
13306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013307f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013308{
13309 getwinvar(argvars, rettv, 1);
13310}
13311
13312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 * "getwinposx()" function
13314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013316f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013318 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319#ifdef FEAT_GUI
13320 if (gui.in_use)
13321 {
13322 int x, y;
13323
13324 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 }
13327#endif
13328}
13329
13330/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013331 * "win_findbuf()" function
13332 */
13333 static void
13334f_win_findbuf(typval_T *argvars, typval_T *rettv)
13335{
13336 if (rettv_list_alloc(rettv) != FAIL)
13337 win_findbuf(argvars, rettv->vval.v_list);
13338}
13339
13340/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013341 * "win_getid()" function
13342 */
13343 static void
13344f_win_getid(typval_T *argvars, typval_T *rettv)
13345{
13346 rettv->vval.v_number = win_getid(argvars);
13347}
13348
13349/*
13350 * "win_gotoid()" function
13351 */
13352 static void
13353f_win_gotoid(typval_T *argvars, typval_T *rettv)
13354{
13355 rettv->vval.v_number = win_gotoid(argvars);
13356}
13357
13358/*
13359 * "win_id2tabwin()" function
13360 */
13361 static void
13362f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13363{
13364 if (rettv_list_alloc(rettv) != FAIL)
13365 win_id2tabwin(argvars, rettv->vval.v_list);
13366}
13367
13368/*
13369 * "win_id2win()" function
13370 */
13371 static void
13372f_win_id2win(typval_T *argvars, typval_T *rettv)
13373{
13374 rettv->vval.v_number = win_id2win(argvars);
13375}
13376
13377/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 * "getwinposy()" function
13379 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013381f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013382{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384#ifdef FEAT_GUI
13385 if (gui.in_use)
13386 {
13387 int x, y;
13388
13389 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013390 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391 }
13392#endif
13393}
13394
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013395/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013396 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013397 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013398 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013399find_win_by_nr(
13400 typval_T *vp,
13401 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013402{
13403#ifdef FEAT_WINDOWS
13404 win_T *wp;
13405#endif
13406 int nr;
13407
13408 nr = get_tv_number_chk(vp, NULL);
13409
13410#ifdef FEAT_WINDOWS
13411 if (nr < 0)
13412 return NULL;
13413 if (nr == 0)
13414 return curwin;
13415
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013416 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13417 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013418 if (--nr <= 0)
13419 break;
13420 return wp;
13421#else
13422 if (nr == 0 || nr == 1)
13423 return curwin;
13424 return NULL;
13425#endif
13426}
13427
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013429 * Find window specified by "wvp" in tabpage "tvp".
13430 */
13431 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013432find_tabwin(
13433 typval_T *wvp, /* VAR_UNKNOWN for current window */
13434 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013435{
13436 win_T *wp = NULL;
13437 tabpage_T *tp = NULL;
13438 long n;
13439
13440 if (wvp->v_type != VAR_UNKNOWN)
13441 {
13442 if (tvp->v_type != VAR_UNKNOWN)
13443 {
13444 n = get_tv_number(tvp);
13445 if (n >= 0)
13446 tp = find_tabpage(n);
13447 }
13448 else
13449 tp = curtab;
13450
13451 if (tp != NULL)
13452 wp = find_win_by_nr(wvp, tp);
13453 }
13454 else
13455 wp = curwin;
13456
13457 return wp;
13458}
13459
13460/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013461 * "getwinvar()" function
13462 */
13463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013464f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013465{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013466 getwinvar(argvars, rettv, 0);
13467}
13468
13469/*
13470 * getwinvar() and gettabwinvar()
13471 */
13472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013473getwinvar(
13474 typval_T *argvars,
13475 typval_T *rettv,
13476 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013477{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013478 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013481 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013482 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013483#ifdef FEAT_WINDOWS
13484 win_T *oldcurwin;
13485 tabpage_T *oldtabpage;
13486 int need_switch_win;
13487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013489#ifdef FEAT_WINDOWS
13490 if (off == 1)
13491 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13492 else
13493 tp = curtab;
13494#endif
13495 win = find_win_by_nr(&argvars[off], tp);
13496 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013497 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013499 rettv->v_type = VAR_STRING;
13500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501
13502 if (win != NULL && varname != NULL)
13503 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013504#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013505 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013506 * otherwise the window is not valid. Only do this when needed,
13507 * autocommands get blocked. */
13508 need_switch_win = !(tp == curtab && win == curwin);
13509 if (!need_switch_win
13510 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13511#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013512 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013513 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013514 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013515 if (get_option_tv(&varname, rettv, 1) == OK)
13516 done = TRUE;
13517 }
13518 else
13519 {
13520 /* Look up the variable. */
13521 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13522 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13523 varname, FALSE);
13524 if (v != NULL)
13525 {
13526 copy_tv(&v->di_tv, rettv);
13527 done = TRUE;
13528 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013531
Bram Moolenaarba117c22015-09-29 16:53:22 +020013532#ifdef FEAT_WINDOWS
13533 if (need_switch_win)
13534 /* restore previous notion of curwin */
13535 restore_win(oldcurwin, oldtabpage, TRUE);
13536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 }
13538
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013539 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13540 /* use the default return value */
13541 copy_tv(&argvars[off + 2], rettv);
13542
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543 --emsg_off;
13544}
13545
13546/*
13547 * "glob()" function
13548 */
13549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013550f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013552 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013553 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013554 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013555
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013556 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013557 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013559 if (argvars[1].v_type != VAR_UNKNOWN)
13560 {
13561 if (get_tv_number_chk(&argvars[1], &error))
13562 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013563 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013564 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013565 if (get_tv_number_chk(&argvars[2], &error))
13566 {
13567 rettv->v_type = VAR_LIST;
13568 rettv->vval.v_list = NULL;
13569 }
13570 if (argvars[3].v_type != VAR_UNKNOWN
13571 && get_tv_number_chk(&argvars[3], &error))
13572 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013573 }
13574 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013575 if (!error)
13576 {
13577 ExpandInit(&xpc);
13578 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013579 if (p_wic)
13580 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013581 if (rettv->v_type == VAR_STRING)
13582 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013583 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013584 else if (rettv_list_alloc(rettv) != FAIL)
13585 {
13586 int i;
13587
13588 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13589 NULL, options, WILD_ALL_KEEP);
13590 for (i = 0; i < xpc.xp_numfiles; i++)
13591 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13592
13593 ExpandCleanup(&xpc);
13594 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013595 }
13596 else
13597 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598}
13599
13600/*
13601 * "globpath()" function
13602 */
13603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013604f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013606 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013609 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013610 garray_T ga;
13611 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013613 /* When the optional second argument is non-zero, don't remove matches
13614 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013616 if (argvars[2].v_type != VAR_UNKNOWN)
13617 {
13618 if (get_tv_number_chk(&argvars[2], &error))
13619 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013620 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013621 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013622 if (get_tv_number_chk(&argvars[3], &error))
13623 {
13624 rettv->v_type = VAR_LIST;
13625 rettv->vval.v_list = NULL;
13626 }
13627 if (argvars[4].v_type != VAR_UNKNOWN
13628 && get_tv_number_chk(&argvars[4], &error))
13629 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013630 }
13631 }
13632 if (file != NULL && !error)
13633 {
13634 ga_init2(&ga, (int)sizeof(char_u *), 10);
13635 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13636 if (rettv->v_type == VAR_STRING)
13637 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13638 else if (rettv_list_alloc(rettv) != FAIL)
13639 for (i = 0; i < ga.ga_len; ++i)
13640 list_append_string(rettv->vval.v_list,
13641 ((char_u **)(ga.ga_data))[i], -1);
13642 ga_clear_strings(&ga);
13643 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013644 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646}
13647
13648/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013649 * "glob2regpat()" function
13650 */
13651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013652f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013653{
13654 char_u *pat = get_tv_string_chk(&argvars[0]);
13655
13656 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013657 rettv->vval.v_string = (pat == NULL)
13658 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013659}
13660
13661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 * "has()" function
13663 */
13664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013665f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666{
13667 int i;
13668 char_u *name;
13669 int n = FALSE;
13670 static char *(has_list[]) =
13671 {
13672#ifdef AMIGA
13673 "amiga",
13674# ifdef FEAT_ARP
13675 "arp",
13676# endif
13677#endif
13678#ifdef __BEOS__
13679 "beos",
13680#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013681#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 "mac",
13683#endif
13684#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013685 "macunix", /* built with 'darwin' enabled */
13686#endif
13687#if defined(__APPLE__) && __APPLE__ == 1
13688 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690#ifdef __QNX__
13691 "qnx",
13692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693#ifdef UNIX
13694 "unix",
13695#endif
13696#ifdef VMS
13697 "vms",
13698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#ifdef WIN32
13700 "win32",
13701#endif
13702#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13703 "win32unix",
13704#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013705#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 "win64",
13707#endif
13708#ifdef EBCDIC
13709 "ebcdic",
13710#endif
13711#ifndef CASE_INSENSITIVE_FILENAME
13712 "fname_case",
13713#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013714#ifdef HAVE_ACL
13715 "acl",
13716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717#ifdef FEAT_ARABIC
13718 "arabic",
13719#endif
13720#ifdef FEAT_AUTOCMD
13721 "autocmd",
13722#endif
13723#ifdef FEAT_BEVAL
13724 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013725# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13726 "balloon_multiline",
13727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728#endif
13729#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13730 "builtin_terms",
13731# ifdef ALL_BUILTIN_TCAPS
13732 "all_builtin_terms",
13733# endif
13734#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013735#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13736 || defined(FEAT_GUI_W32) \
13737 || defined(FEAT_GUI_MOTIF))
13738 "browsefilter",
13739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740#ifdef FEAT_BYTEOFF
13741 "byte_offset",
13742#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013743#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013744 "channel",
13745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746#ifdef FEAT_CINDENT
13747 "cindent",
13748#endif
13749#ifdef FEAT_CLIENTSERVER
13750 "clientserver",
13751#endif
13752#ifdef FEAT_CLIPBOARD
13753 "clipboard",
13754#endif
13755#ifdef FEAT_CMDL_COMPL
13756 "cmdline_compl",
13757#endif
13758#ifdef FEAT_CMDHIST
13759 "cmdline_hist",
13760#endif
13761#ifdef FEAT_COMMENTS
13762 "comments",
13763#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013764#ifdef FEAT_CONCEAL
13765 "conceal",
13766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767#ifdef FEAT_CRYPT
13768 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013769 "crypt-blowfish",
13770 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013771#endif
13772#ifdef FEAT_CSCOPE
13773 "cscope",
13774#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013775#ifdef FEAT_CURSORBIND
13776 "cursorbind",
13777#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013778#ifdef CURSOR_SHAPE
13779 "cursorshape",
13780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781#ifdef DEBUG
13782 "debug",
13783#endif
13784#ifdef FEAT_CON_DIALOG
13785 "dialog_con",
13786#endif
13787#ifdef FEAT_GUI_DIALOG
13788 "dialog_gui",
13789#endif
13790#ifdef FEAT_DIFF
13791 "diff",
13792#endif
13793#ifdef FEAT_DIGRAPHS
13794 "digraphs",
13795#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013796#ifdef FEAT_DIRECTX
13797 "directx",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_DND
13800 "dnd",
13801#endif
13802#ifdef FEAT_EMACS_TAGS
13803 "emacs_tags",
13804#endif
13805 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013806 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807#ifdef FEAT_SEARCH_EXTRA
13808 "extra_search",
13809#endif
13810#ifdef FEAT_FKMAP
13811 "farsi",
13812#endif
13813#ifdef FEAT_SEARCHPATH
13814 "file_in_path",
13815#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013816#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013817 "filterpipe",
13818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#ifdef FEAT_FIND_ID
13820 "find_in_path",
13821#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013822#ifdef FEAT_FLOAT
13823 "float",
13824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825#ifdef FEAT_FOLDING
13826 "folding",
13827#endif
13828#ifdef FEAT_FOOTER
13829 "footer",
13830#endif
13831#if !defined(USE_SYSTEM) && defined(UNIX)
13832 "fork",
13833#endif
13834#ifdef FEAT_GETTEXT
13835 "gettext",
13836#endif
13837#ifdef FEAT_GUI
13838 "gui",
13839#endif
13840#ifdef FEAT_GUI_ATHENA
13841# ifdef FEAT_GUI_NEXTAW
13842 "gui_neXtaw",
13843# else
13844 "gui_athena",
13845# endif
13846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#ifdef FEAT_GUI_GTK
13848 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013849# ifdef USE_GTK3
13850 "gui_gtk3",
13851# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013852 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013855#ifdef FEAT_GUI_GNOME
13856 "gui_gnome",
13857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858#ifdef FEAT_GUI_MAC
13859 "gui_mac",
13860#endif
13861#ifdef FEAT_GUI_MOTIF
13862 "gui_motif",
13863#endif
13864#ifdef FEAT_GUI_PHOTON
13865 "gui_photon",
13866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867#ifdef FEAT_GUI_W32
13868 "gui_win32",
13869#endif
13870#ifdef FEAT_HANGULIN
13871 "hangul_input",
13872#endif
13873#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13874 "iconv",
13875#endif
13876#ifdef FEAT_INS_EXPAND
13877 "insert_expand",
13878#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013879#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013880 "job",
13881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882#ifdef FEAT_JUMPLIST
13883 "jumplist",
13884#endif
13885#ifdef FEAT_KEYMAP
13886 "keymap",
13887#endif
13888#ifdef FEAT_LANGMAP
13889 "langmap",
13890#endif
13891#ifdef FEAT_LIBCALL
13892 "libcall",
13893#endif
13894#ifdef FEAT_LINEBREAK
13895 "linebreak",
13896#endif
13897#ifdef FEAT_LISP
13898 "lispindent",
13899#endif
13900#ifdef FEAT_LISTCMDS
13901 "listcmds",
13902#endif
13903#ifdef FEAT_LOCALMAP
13904 "localmap",
13905#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013906#ifdef FEAT_LUA
13907# ifndef DYNAMIC_LUA
13908 "lua",
13909# endif
13910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911#ifdef FEAT_MENU
13912 "menu",
13913#endif
13914#ifdef FEAT_SESSION
13915 "mksession",
13916#endif
13917#ifdef FEAT_MODIFY_FNAME
13918 "modify_fname",
13919#endif
13920#ifdef FEAT_MOUSE
13921 "mouse",
13922#endif
13923#ifdef FEAT_MOUSESHAPE
13924 "mouseshape",
13925#endif
13926#if defined(UNIX) || defined(VMS)
13927# ifdef FEAT_MOUSE_DEC
13928 "mouse_dec",
13929# endif
13930# ifdef FEAT_MOUSE_GPM
13931 "mouse_gpm",
13932# endif
13933# ifdef FEAT_MOUSE_JSB
13934 "mouse_jsbterm",
13935# endif
13936# ifdef FEAT_MOUSE_NET
13937 "mouse_netterm",
13938# endif
13939# ifdef FEAT_MOUSE_PTERM
13940 "mouse_pterm",
13941# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013942# ifdef FEAT_MOUSE_SGR
13943 "mouse_sgr",
13944# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013945# ifdef FEAT_SYSMOUSE
13946 "mouse_sysmouse",
13947# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013948# ifdef FEAT_MOUSE_URXVT
13949 "mouse_urxvt",
13950# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951# ifdef FEAT_MOUSE_XTERM
13952 "mouse_xterm",
13953# endif
13954#endif
13955#ifdef FEAT_MBYTE
13956 "multi_byte",
13957#endif
13958#ifdef FEAT_MBYTE_IME
13959 "multi_byte_ime",
13960#endif
13961#ifdef FEAT_MULTI_LANG
13962 "multi_lang",
13963#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013964#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013965#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013966 "mzscheme",
13967#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013969#ifdef FEAT_OLE
13970 "ole",
13971#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013972 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013973#ifdef FEAT_PATH_EXTRA
13974 "path_extra",
13975#endif
13976#ifdef FEAT_PERL
13977#ifndef DYNAMIC_PERL
13978 "perl",
13979#endif
13980#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013981#ifdef FEAT_PERSISTENT_UNDO
13982 "persistent_undo",
13983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984#ifdef FEAT_PYTHON
13985#ifndef DYNAMIC_PYTHON
13986 "python",
13987#endif
13988#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013989#ifdef FEAT_PYTHON3
13990#ifndef DYNAMIC_PYTHON3
13991 "python3",
13992#endif
13993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994#ifdef FEAT_POSTSCRIPT
13995 "postscript",
13996#endif
13997#ifdef FEAT_PRINTER
13998 "printer",
13999#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014000#ifdef FEAT_PROFILE
14001 "profile",
14002#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014003#ifdef FEAT_RELTIME
14004 "reltime",
14005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006#ifdef FEAT_QUICKFIX
14007 "quickfix",
14008#endif
14009#ifdef FEAT_RIGHTLEFT
14010 "rightleft",
14011#endif
14012#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14013 "ruby",
14014#endif
14015#ifdef FEAT_SCROLLBIND
14016 "scrollbind",
14017#endif
14018#ifdef FEAT_CMDL_INFO
14019 "showcmd",
14020 "cmdline_info",
14021#endif
14022#ifdef FEAT_SIGNS
14023 "signs",
14024#endif
14025#ifdef FEAT_SMARTINDENT
14026 "smartindent",
14027#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014028#ifdef STARTUPTIME
14029 "startuptime",
14030#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031#ifdef FEAT_STL_OPT
14032 "statusline",
14033#endif
14034#ifdef FEAT_SUN_WORKSHOP
14035 "sun_workshop",
14036#endif
14037#ifdef FEAT_NETBEANS_INTG
14038 "netbeans_intg",
14039#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014040#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014041 "spell",
14042#endif
14043#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 "syntax",
14045#endif
14046#if defined(USE_SYSTEM) || !defined(UNIX)
14047 "system",
14048#endif
14049#ifdef FEAT_TAG_BINS
14050 "tag_binary",
14051#endif
14052#ifdef FEAT_TAG_OLDSTATIC
14053 "tag_old_static",
14054#endif
14055#ifdef FEAT_TAG_ANYWHITE
14056 "tag_any_white",
14057#endif
14058#ifdef FEAT_TCL
14059# ifndef DYNAMIC_TCL
14060 "tcl",
14061# endif
14062#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014063#ifdef FEAT_TERMGUICOLORS
14064 "termguicolors",
14065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066#ifdef TERMINFO
14067 "terminfo",
14068#endif
14069#ifdef FEAT_TERMRESPONSE
14070 "termresponse",
14071#endif
14072#ifdef FEAT_TEXTOBJ
14073 "textobjects",
14074#endif
14075#ifdef HAVE_TGETENT
14076 "tgetent",
14077#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014078#ifdef FEAT_TIMERS
14079 "timers",
14080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081#ifdef FEAT_TITLE
14082 "title",
14083#endif
14084#ifdef FEAT_TOOLBAR
14085 "toolbar",
14086#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014087#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14088 "unnamedplus",
14089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090#ifdef FEAT_USR_CMDS
14091 "user-commands", /* was accidentally included in 5.4 */
14092 "user_commands",
14093#endif
14094#ifdef FEAT_VIMINFO
14095 "viminfo",
14096#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014097#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098 "vertsplit",
14099#endif
14100#ifdef FEAT_VIRTUALEDIT
14101 "virtualedit",
14102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104#ifdef FEAT_VISUALEXTRA
14105 "visualextra",
14106#endif
14107#ifdef FEAT_VREPLACE
14108 "vreplace",
14109#endif
14110#ifdef FEAT_WILDIGN
14111 "wildignore",
14112#endif
14113#ifdef FEAT_WILDMENU
14114 "wildmenu",
14115#endif
14116#ifdef FEAT_WINDOWS
14117 "windows",
14118#endif
14119#ifdef FEAT_WAK
14120 "winaltkeys",
14121#endif
14122#ifdef FEAT_WRITEBACKUP
14123 "writebackup",
14124#endif
14125#ifdef FEAT_XIM
14126 "xim",
14127#endif
14128#ifdef FEAT_XFONTSET
14129 "xfontset",
14130#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014131#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014132 "xpm",
14133 "xpm_w32", /* for backward compatibility */
14134#else
14135# if defined(HAVE_XPM)
14136 "xpm",
14137# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139#ifdef USE_XSMP
14140 "xsmp",
14141#endif
14142#ifdef USE_XSMP_INTERACT
14143 "xsmp_interact",
14144#endif
14145#ifdef FEAT_XCLIPBOARD
14146 "xterm_clipboard",
14147#endif
14148#ifdef FEAT_XTERM_SAVE
14149 "xterm_save",
14150#endif
14151#if defined(UNIX) && defined(FEAT_X11)
14152 "X11",
14153#endif
14154 NULL
14155 };
14156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 for (i = 0; has_list[i] != NULL; ++i)
14159 if (STRICMP(name, has_list[i]) == 0)
14160 {
14161 n = TRUE;
14162 break;
14163 }
14164
14165 if (n == FALSE)
14166 {
14167 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014168 {
14169 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014170 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014171 && vim_isdigit(name[6])
14172 && vim_isdigit(name[8])
14173 && vim_isdigit(name[10]))
14174 {
14175 int major = atoi((char *)name + 6);
14176 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014177
14178 /* Expect "patch-9.9.01234". */
14179 n = (major < VIM_VERSION_MAJOR
14180 || (major == VIM_VERSION_MAJOR
14181 && (minor < VIM_VERSION_MINOR
14182 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014183 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014184 }
14185 else
14186 n = has_patch(atoi((char *)name + 5));
14187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 else if (STRICMP(name, "vim_starting") == 0)
14189 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014190#ifdef FEAT_MBYTE
14191 else if (STRICMP(name, "multi_byte_encoding") == 0)
14192 n = has_mbyte;
14193#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014194#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14195 else if (STRICMP(name, "balloon_multiline") == 0)
14196 n = multiline_balloon_available();
14197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198#ifdef DYNAMIC_TCL
14199 else if (STRICMP(name, "tcl") == 0)
14200 n = tcl_enabled(FALSE);
14201#endif
14202#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14203 else if (STRICMP(name, "iconv") == 0)
14204 n = iconv_enabled(FALSE);
14205#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014206#ifdef DYNAMIC_LUA
14207 else if (STRICMP(name, "lua") == 0)
14208 n = lua_enabled(FALSE);
14209#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014210#ifdef DYNAMIC_MZSCHEME
14211 else if (STRICMP(name, "mzscheme") == 0)
14212 n = mzscheme_enabled(FALSE);
14213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214#ifdef DYNAMIC_RUBY
14215 else if (STRICMP(name, "ruby") == 0)
14216 n = ruby_enabled(FALSE);
14217#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014218#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#ifdef DYNAMIC_PYTHON
14220 else if (STRICMP(name, "python") == 0)
14221 n = python_enabled(FALSE);
14222#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014223#endif
14224#ifdef FEAT_PYTHON3
14225#ifdef DYNAMIC_PYTHON3
14226 else if (STRICMP(name, "python3") == 0)
14227 n = python3_enabled(FALSE);
14228#endif
14229#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230#ifdef DYNAMIC_PERL
14231 else if (STRICMP(name, "perl") == 0)
14232 n = perl_enabled(FALSE);
14233#endif
14234#ifdef FEAT_GUI
14235 else if (STRICMP(name, "gui_running") == 0)
14236 n = (gui.in_use || gui.starting);
14237# ifdef FEAT_GUI_W32
14238 else if (STRICMP(name, "gui_win32s") == 0)
14239 n = gui_is_win32s();
14240# endif
14241# ifdef FEAT_BROWSE
14242 else if (STRICMP(name, "browse") == 0)
14243 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14244# endif
14245#endif
14246#ifdef FEAT_SYN_HL
14247 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014248 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249#endif
14250#if defined(WIN3264)
14251 else if (STRICMP(name, "win95") == 0)
14252 n = mch_windows95();
14253#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014254#ifdef FEAT_NETBEANS_INTG
14255 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014256 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258 }
14259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014260 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261}
14262
14263/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014264 * "has_key()" function
14265 */
14266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014267f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014268{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014269 if (argvars[0].v_type != VAR_DICT)
14270 {
14271 EMSG(_(e_dictreq));
14272 return;
14273 }
14274 if (argvars[0].vval.v_dict == NULL)
14275 return;
14276
14277 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014278 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014279}
14280
14281/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014282 * "haslocaldir()" function
14283 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014285f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014286{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014287 win_T *wp = NULL;
14288
14289 wp = find_tabwin(&argvars[0], &argvars[1]);
14290 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014291}
14292
14293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 * "hasmapto()" function
14295 */
14296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014297f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298{
14299 char_u *name;
14300 char_u *mode;
14301 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014302 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014304 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014305 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306 mode = (char_u *)"nvo";
14307 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014310 if (argvars[2].v_type != VAR_UNKNOWN)
14311 abbr = get_tv_number(&argvars[2]);
14312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
Bram Moolenaar2c932302006-03-18 21:42:09 +000014314 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014315 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014317 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318}
14319
14320/*
14321 * "histadd()" function
14322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014324f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325{
14326#ifdef FEAT_CMDHIST
14327 int histype;
14328 char_u *str;
14329 char_u buf[NUMBUFLEN];
14330#endif
14331
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 if (check_restricted() || check_secure())
14334 return;
14335#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014336 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14337 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338 if (histype >= 0)
14339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014340 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 if (*str != NUL)
14342 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014343 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014344 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014345 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346 return;
14347 }
14348 }
14349#endif
14350}
14351
14352/*
14353 * "histdel()" function
14354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014356f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357{
14358#ifdef FEAT_CMDHIST
14359 int n;
14360 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014363 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14364 if (str == NULL)
14365 n = 0;
14366 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014368 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014369 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014372 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373 else
14374 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 get_tv_string_buf(&argvars[1], buf));
14377 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378#endif
14379}
14380
14381/*
14382 * "histget()" function
14383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014385f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386{
14387#ifdef FEAT_CMDHIST
14388 int type;
14389 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014390 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014392 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14393 if (str == NULL)
14394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014396 {
14397 type = get_histtype(str);
14398 if (argvars[1].v_type == VAR_UNKNOWN)
14399 idx = get_history_idx(type);
14400 else
14401 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14402 /* -1 on type error */
14403 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409}
14410
14411/*
14412 * "histnr()" function
14413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416{
14417 int i;
14418
14419#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 char_u *history = get_tv_string_chk(&argvars[0]);
14421
14422 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 if (i >= HIST_CMD && i < HIST_COUNT)
14424 i = get_history_idx(i);
14425 else
14426#endif
14427 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429}
14430
14431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 * "highlightID(name)" function
14433 */
14434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014435f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438}
14439
14440/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014441 * "highlight_exists()" function
14442 */
14443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014444f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445{
14446 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14447}
14448
14449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450 * "hostname()" function
14451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014453f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454{
14455 char_u hostname[256];
14456
14457 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014458 rettv->v_type = VAR_STRING;
14459 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460}
14461
14462/*
14463 * iconv() function
14464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014466f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467{
14468#ifdef FEAT_MBYTE
14469 char_u buf1[NUMBUFLEN];
14470 char_u buf2[NUMBUFLEN];
14471 char_u *from, *to, *str;
14472 vimconv_T vimconv;
14473#endif
14474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014475 rettv->v_type = VAR_STRING;
14476 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477
14478#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 str = get_tv_string(&argvars[0]);
14480 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14481 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 vimconv.vc_type = CONV_NONE;
14483 convert_setup(&vimconv, from, to);
14484
14485 /* If the encodings are equal, no conversion needed. */
14486 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490
14491 convert_setup(&vimconv, NULL, NULL);
14492 vim_free(from);
14493 vim_free(to);
14494#endif
14495}
14496
14497/*
14498 * "indent()" function
14499 */
14500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014501f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502{
14503 linenr_T lnum;
14504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510}
14511
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014512/*
14513 * "index()" function
14514 */
14515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014516f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014517{
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 list_T *l;
14519 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014520 long idx = 0;
14521 int ic = FALSE;
14522
14523 rettv->vval.v_number = -1;
14524 if (argvars[0].v_type != VAR_LIST)
14525 {
14526 EMSG(_(e_listreq));
14527 return;
14528 }
14529 l = argvars[0].vval.v_list;
14530 if (l != NULL)
14531 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014532 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014533 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014535 int error = FALSE;
14536
Bram Moolenaar758711c2005-02-02 23:11:38 +000014537 /* Start at specified item. Use the cached index that list_find()
14538 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014539 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014540 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014541 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 ic = get_tv_number_chk(&argvars[3], &error);
14543 if (error)
14544 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014545 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014546
Bram Moolenaar758711c2005-02-02 23:11:38 +000014547 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014548 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014549 {
14550 rettv->vval.v_number = idx;
14551 break;
14552 }
14553 }
14554}
14555
Bram Moolenaar071d4272004-06-13 20:20:40 +000014556static int inputsecret_flag = 0;
14557
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014558static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014559
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014561 * This function is used by f_input() and f_inputdialog() functions. The third
14562 * argument to f_input() specifies the type of completion to use at the
14563 * prompt. The third argument to f_inputdialog() specifies the value to return
14564 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565 */
14566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014567get_user_input(
14568 typval_T *argvars,
14569 typval_T *rettv,
14570 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014571{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014572 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 char_u *p = NULL;
14574 int c;
14575 char_u buf[NUMBUFLEN];
14576 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014577 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014578 int xp_type = EXPAND_NOTHING;
14579 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014581 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583
14584#ifdef NO_CONSOLE_INPUT
14585 /* While starting up, there is no place to enter text. */
14586 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588#endif
14589
14590 cmd_silent = FALSE; /* Want to see the prompt. */
14591 if (prompt != NULL)
14592 {
14593 /* Only the part of the message after the last NL is considered as
14594 * prompt for the command line */
14595 p = vim_strrchr(prompt, '\n');
14596 if (p == NULL)
14597 p = prompt;
14598 else
14599 {
14600 ++p;
14601 c = *p;
14602 *p = NUL;
14603 msg_start();
14604 msg_clr_eos();
14605 msg_puts_attr(prompt, echo_attr);
14606 msg_didout = FALSE;
14607 msg_starthere();
14608 *p = c;
14609 }
14610 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014612 if (argvars[1].v_type != VAR_UNKNOWN)
14613 {
14614 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14615 if (defstr != NULL)
14616 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014618 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014619 {
14620 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014621 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014622 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014623
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014624 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014625 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014626
Bram Moolenaar4463f292005-09-25 22:20:24 +000014627 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14628 if (xp_name == NULL)
14629 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014630
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014631 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014632
Bram Moolenaar4463f292005-09-25 22:20:24 +000014633 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14634 &xp_arg) == FAIL)
14635 return;
14636 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014637 }
14638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014640 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014641 int save_ex_normal_busy = ex_normal_busy;
14642 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014643 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014644 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14645 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014646 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014647 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014648 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014649 && argvars[1].v_type != VAR_UNKNOWN
14650 && argvars[2].v_type != VAR_UNKNOWN)
14651 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14652 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014653
14654 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014656 /* since the user typed this, no need to wait for return */
14657 need_wait_return = FALSE;
14658 msg_didout = FALSE;
14659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 cmd_silent = cmd_silent_save;
14661}
14662
14663/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014664 * "input()" function
14665 * Also handles inputsecret() when inputsecret is set.
14666 */
14667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014668f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014669{
14670 get_user_input(argvars, rettv, FALSE);
14671}
14672
14673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 * "inputdialog()" function
14675 */
14676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014677f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678{
14679#if defined(FEAT_GUI_TEXTDIALOG)
14680 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14681 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14682 {
14683 char_u *message;
14684 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014685 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014687 message = get_tv_string_chk(&argvars[0]);
14688 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014689 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014690 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014691 else
14692 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014693 if (message != NULL && defstr != NULL
14694 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014695 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014697 else
14698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014699 if (message != NULL && defstr != NULL
14700 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014701 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014702 rettv->vval.v_string = vim_strsave(
14703 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014707 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014708 }
14709 else
14710#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014711 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712}
14713
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014714/*
14715 * "inputlist()" function
14716 */
14717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014718f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014719{
14720 listitem_T *li;
14721 int selected;
14722 int mouse_used;
14723
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014724#ifdef NO_CONSOLE_INPUT
14725 /* While starting up, there is no place to enter text. */
14726 if (no_console_input())
14727 return;
14728#endif
14729 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14730 {
14731 EMSG2(_(e_listarg), "inputlist()");
14732 return;
14733 }
14734
14735 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014736 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014737 lines_left = Rows; /* avoid more prompt */
14738 msg_scroll = TRUE;
14739 msg_clr_eos();
14740
14741 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14742 {
14743 msg_puts(get_tv_string(&li->li_tv));
14744 msg_putchar('\n');
14745 }
14746
14747 /* Ask for choice. */
14748 selected = prompt_for_number(&mouse_used);
14749 if (mouse_used)
14750 selected -= lines_left;
14751
14752 rettv->vval.v_number = selected;
14753}
14754
14755
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14757
14758/*
14759 * "inputrestore()" function
14760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014762f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763{
14764 if (ga_userinput.ga_len > 0)
14765 {
14766 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14768 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014769 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014770 }
14771 else if (p_verbose > 1)
14772 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014773 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775 }
14776}
14777
14778/*
14779 * "inputsave()" function
14780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014782f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014783{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014784 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 if (ga_grow(&ga_userinput, 1) == OK)
14786 {
14787 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14788 + ga_userinput.ga_len);
14789 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014790 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014791 }
14792 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014793 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014794}
14795
14796/*
14797 * "inputsecret()" function
14798 */
14799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014800f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014801{
14802 ++cmdline_star;
14803 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014804 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805 --cmdline_star;
14806 --inputsecret_flag;
14807}
14808
14809/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014810 * "insert()" function
14811 */
14812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014813f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014814{
14815 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014816 listitem_T *item;
14817 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014818 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014819
14820 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014821 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014822 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014823 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014824 {
14825 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014826 before = get_tv_number_chk(&argvars[2], &error);
14827 if (error)
14828 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014829
Bram Moolenaar758711c2005-02-02 23:11:38 +000014830 if (before == l->lv_len)
14831 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014832 else
14833 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014834 item = list_find(l, before);
14835 if (item == NULL)
14836 {
14837 EMSGN(_(e_listidx), before);
14838 l = NULL;
14839 }
14840 }
14841 if (l != NULL)
14842 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014843 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014844 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014845 }
14846 }
14847}
14848
14849/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014850 * "invert(expr)" function
14851 */
14852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014853f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014854{
14855 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14856}
14857
14858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859 * "isdirectory()" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865}
14866
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014867/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014868 * "islocked()" function
14869 */
14870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014871f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014872{
14873 lval_T lv;
14874 char_u *end;
14875 dictitem_T *di;
14876
14877 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014878 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14879 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014880 if (end != NULL && lv.ll_name != NULL)
14881 {
14882 if (*end != NUL)
14883 EMSG(_(e_trailing));
14884 else
14885 {
14886 if (lv.ll_tv == NULL)
14887 {
14888 if (check_changedtick(lv.ll_name))
14889 rettv->vval.v_number = 1; /* always locked */
14890 else
14891 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014892 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014893 if (di != NULL)
14894 {
14895 /* Consider a variable locked when:
14896 * 1. the variable itself is locked
14897 * 2. the value of the variable is locked.
14898 * 3. the List or Dict value is locked.
14899 */
14900 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14901 || tv_islocked(&di->di_tv));
14902 }
14903 }
14904 }
14905 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014906 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014907 else if (lv.ll_newkey != NULL)
14908 EMSG2(_(e_dictkey), lv.ll_newkey);
14909 else if (lv.ll_list != NULL)
14910 /* List item. */
14911 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14912 else
14913 /* Dictionary item. */
14914 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14915 }
14916 }
14917
14918 clear_lval(&lv);
14919}
14920
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014921#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14922/*
14923 * "isnan()" function
14924 */
14925 static void
14926f_isnan(typval_T *argvars, typval_T *rettv)
14927{
14928 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14929 && isnan(argvars[0].vval.v_float);
14930}
14931#endif
14932
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014933static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014934
14935/*
14936 * Turn a dict into a list:
14937 * "what" == 0: list of keys
14938 * "what" == 1: list of values
14939 * "what" == 2: list of items
14940 */
14941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943{
Bram Moolenaar33570922005-01-25 22:26:29 +000014944 list_T *l2;
14945 dictitem_T *di;
14946 hashitem_T *hi;
14947 listitem_T *li;
14948 listitem_T *li2;
14949 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014950 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014951
Bram Moolenaar8c711452005-01-14 21:53:12 +000014952 if (argvars[0].v_type != VAR_DICT)
14953 {
14954 EMSG(_(e_dictreq));
14955 return;
14956 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014957 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014958 return;
14959
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014960 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014961 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014962
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014963 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014964 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014965 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014966 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014967 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014968 --todo;
14969 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014970
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014971 li = listitem_alloc();
14972 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014973 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014974 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014975
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014976 if (what == 0)
14977 {
14978 /* keys() */
14979 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014980 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014981 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14982 }
14983 else if (what == 1)
14984 {
14985 /* values() */
14986 copy_tv(&di->di_tv, &li->li_tv);
14987 }
14988 else
14989 {
14990 /* items() */
14991 l2 = list_alloc();
14992 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014993 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014994 li->li_tv.vval.v_list = l2;
14995 if (l2 == NULL)
14996 break;
14997 ++l2->lv_refcount;
14998
14999 li2 = listitem_alloc();
15000 if (li2 == NULL)
15001 break;
15002 list_append(l2, li2);
15003 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015004 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015005 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15006
15007 li2 = listitem_alloc();
15008 if (li2 == NULL)
15009 break;
15010 list_append(l2, li2);
15011 copy_tv(&di->di_tv, &li2->li_tv);
15012 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015013 }
15014 }
15015}
15016
15017/*
15018 * "items(dict)" function
15019 */
15020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015021f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015022{
15023 dict_list(argvars, rettv, 2);
15024}
15025
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015026#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015027/*
15028 * Get the job from the argument.
15029 * Returns NULL if the job is invalid.
15030 */
15031 static job_T *
15032get_job_arg(typval_T *tv)
15033{
15034 job_T *job;
15035
15036 if (tv->v_type != VAR_JOB)
15037 {
15038 EMSG2(_(e_invarg2), get_tv_string(tv));
15039 return NULL;
15040 }
15041 job = tv->vval.v_job;
15042
15043 if (job == NULL)
15044 EMSG(_("E916: not a valid job"));
15045 return job;
15046}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015047
Bram Moolenaar835dc632016-02-07 14:27:38 +010015048/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015049 * "job_getchannel()" function
15050 */
15051 static void
15052f_job_getchannel(typval_T *argvars, typval_T *rettv)
15053{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015054 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015055
Bram Moolenaar65edff82016-02-21 16:40:11 +010015056 if (job != NULL)
15057 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015058 rettv->v_type = VAR_CHANNEL;
15059 rettv->vval.v_channel = job->jv_channel;
15060 if (job->jv_channel != NULL)
15061 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015062 }
15063}
15064
15065/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015066 * "job_info()" function
15067 */
15068 static void
15069f_job_info(typval_T *argvars, typval_T *rettv)
15070{
15071 job_T *job = get_job_arg(&argvars[0]);
15072
15073 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15074 job_info(job, rettv->vval.v_dict);
15075}
15076
15077/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015078 * "job_setoptions()" function
15079 */
15080 static void
15081f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15082{
15083 job_T *job = get_job_arg(&argvars[0]);
15084 jobopt_T opt;
15085
15086 if (job == NULL)
15087 return;
15088 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015089 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15090 job_set_options(job, &opt);
15091 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015092}
15093
15094/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015095 * "job_start()" function
15096 */
15097 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015098f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015099{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015100 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015101 if (check_restricted() || check_secure())
15102 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015103 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015104}
15105
15106/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015107 * "job_status()" function
15108 */
15109 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015110f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015111{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015112 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015113
Bram Moolenaar65edff82016-02-21 16:40:11 +010015114 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015115 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015116 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015117 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015118 }
15119}
15120
15121/*
15122 * "job_stop()" function
15123 */
15124 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015125f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015126{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015127 job_T *job = get_job_arg(&argvars[0]);
15128
15129 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015130 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015131}
15132#endif
15133
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015135 * "join()" function
15136 */
15137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015138f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139{
15140 garray_T ga;
15141 char_u *sep;
15142
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015143 if (argvars[0].v_type != VAR_LIST)
15144 {
15145 EMSG(_(e_listreq));
15146 return;
15147 }
15148 if (argvars[0].vval.v_list == NULL)
15149 return;
15150 if (argvars[1].v_type == VAR_UNKNOWN)
15151 sep = (char_u *)" ";
15152 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015153 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015154
15155 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015156
15157 if (sep != NULL)
15158 {
15159 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015160 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015161 ga_append(&ga, NUL);
15162 rettv->vval.v_string = (char_u *)ga.ga_data;
15163 }
15164 else
15165 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015166}
15167
15168/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015169 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015170 */
15171 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015172f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015173{
15174 js_read_T reader;
15175
15176 reader.js_buf = get_tv_string(&argvars[0]);
15177 reader.js_fill = NULL;
15178 reader.js_used = 0;
15179 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15180 EMSG(_(e_invarg));
15181}
15182
15183/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015184 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015185 */
15186 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015187f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015188{
15189 rettv->v_type = VAR_STRING;
15190 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15191}
15192
15193/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015194 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015195 */
15196 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015197f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015198{
15199 js_read_T reader;
15200
15201 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015202 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015203 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015204 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015205 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015206}
15207
15208/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015209 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015210 */
15211 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015212f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015213{
15214 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015215 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015216}
15217
15218/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015219 * "keys()" function
15220 */
15221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015222f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015223{
15224 dict_list(argvars, rettv, 0);
15225}
15226
15227/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 * "last_buffer_nr()" function.
15229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015231f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232{
15233 int n = 0;
15234 buf_T *buf;
15235
15236 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15237 if (n < buf->b_fnum)
15238 n = buf->b_fnum;
15239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015240 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015241}
15242
15243/*
15244 * "len()" function
15245 */
15246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015247f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248{
15249 switch (argvars[0].v_type)
15250 {
15251 case VAR_STRING:
15252 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015253 rettv->vval.v_number = (varnumber_T)STRLEN(
15254 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255 break;
15256 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015258 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015259 case VAR_DICT:
15260 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15261 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015262 case VAR_UNKNOWN:
15263 case VAR_SPECIAL:
15264 case VAR_FLOAT:
15265 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015266 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015267 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015268 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015269 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270 break;
15271 }
15272}
15273
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015274static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015275
15276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015277libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015278{
15279#ifdef FEAT_LIBCALL
15280 char_u *string_in;
15281 char_u **string_result;
15282 int nr_result;
15283#endif
15284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015285 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015286 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015287 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015288
15289 if (check_restricted() || check_secure())
15290 return;
15291
15292#ifdef FEAT_LIBCALL
15293 /* The first two args must be strings, otherwise its meaningless */
15294 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15295 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015296 string_in = NULL;
15297 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015298 string_in = argvars[2].vval.v_string;
15299 if (type == VAR_NUMBER)
15300 string_result = NULL;
15301 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015302 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015303 if (mch_libcall(argvars[0].vval.v_string,
15304 argvars[1].vval.v_string,
15305 string_in,
15306 argvars[2].vval.v_number,
15307 string_result,
15308 &nr_result) == OK
15309 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015311 }
15312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313}
15314
15315/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015316 * "libcall()" function
15317 */
15318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015319f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015320{
15321 libcall_common(argvars, rettv, VAR_STRING);
15322}
15323
15324/*
15325 * "libcallnr()" function
15326 */
15327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015328f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015329{
15330 libcall_common(argvars, rettv, VAR_NUMBER);
15331}
15332
15333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015334 * "line(string)" function
15335 */
15336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015337f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338{
15339 linenr_T lnum = 0;
15340 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015341 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015343 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 if (fp != NULL)
15345 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347}
15348
15349/*
15350 * "line2byte(lnum)" function
15351 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015353f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354{
15355#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357#else
15358 linenr_T lnum;
15359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15365 if (rettv->vval.v_number >= 0)
15366 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367#endif
15368}
15369
15370/*
15371 * "lispindent(lnum)" function
15372 */
15373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015374f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375{
15376#ifdef FEAT_LISP
15377 pos_T pos;
15378 linenr_T lnum;
15379
15380 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015381 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015382 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15383 {
15384 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015385 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 curwin->w_cursor = pos;
15387 }
15388 else
15389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391}
15392
15393/*
15394 * "localtime()" function
15395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015397f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400}
15401
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015402static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403
15404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015405get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406{
15407 char_u *keys;
15408 char_u *which;
15409 char_u buf[NUMBUFLEN];
15410 char_u *keys_buf = NULL;
15411 char_u *rhs;
15412 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015413 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015414 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015415 mapblock_T *mp;
15416 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417
15418 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 rettv->v_type = VAR_STRING;
15420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015422 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 if (*keys == NUL)
15424 return;
15425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015428 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015429 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015430 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015431 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015432 if (argvars[3].v_type != VAR_UNKNOWN)
15433 get_dict = get_tv_number(&argvars[3]);
15434 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015436 else
15437 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015438 if (which == NULL)
15439 return;
15440
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 mode = get_map_mode(&which, 0);
15442
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015443 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015444 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015446
15447 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015449 /* Return a string. */
15450 if (rhs != NULL)
15451 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452
Bram Moolenaarbd743252010-10-20 21:23:33 +020015453 }
15454 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15455 {
15456 /* Return a dictionary. */
15457 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15458 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15459 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460
Bram Moolenaarbd743252010-10-20 21:23:33 +020015461 dict_add_nr_str(dict, "lhs", 0L, lhs);
15462 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15463 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15464 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15465 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15466 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15467 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015468 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015469 dict_add_nr_str(dict, "mode", 0L, mapmode);
15470
15471 vim_free(lhs);
15472 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 }
15474}
15475
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015476#ifdef FEAT_FLOAT
15477/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015478 * "log()" function
15479 */
15480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015481f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015482{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015483 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015484
15485 rettv->v_type = VAR_FLOAT;
15486 if (get_float_arg(argvars, &f) == OK)
15487 rettv->vval.v_float = log(f);
15488 else
15489 rettv->vval.v_float = 0.0;
15490}
15491
15492/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015493 * "log10()" function
15494 */
15495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015496f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015497{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015498 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015499
15500 rettv->v_type = VAR_FLOAT;
15501 if (get_float_arg(argvars, &f) == OK)
15502 rettv->vval.v_float = log10(f);
15503 else
15504 rettv->vval.v_float = 0.0;
15505}
15506#endif
15507
Bram Moolenaar1dced572012-04-05 16:54:08 +020015508#ifdef FEAT_LUA
15509/*
15510 * "luaeval()" function
15511 */
15512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015513f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015514{
15515 char_u *str;
15516 char_u buf[NUMBUFLEN];
15517
15518 str = get_tv_string_buf(&argvars[0], buf);
15519 do_luaeval(str, argvars + 1, rettv);
15520}
15521#endif
15522
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015524 * "map()" function
15525 */
15526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015527f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015528{
15529 filter_map(argvars, rettv, TRUE);
15530}
15531
15532/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015533 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015534 */
15535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015536f_maparg(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, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
15541/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015542 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 */
15544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015545f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015547 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548}
15549
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015550static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551
15552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015553find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015555 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015556 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015557 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 char_u *pat;
15559 regmatch_T regmatch;
15560 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015561 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 char_u *save_cpo;
15563 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015564 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015565 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015566 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015567 list_T *l = NULL;
15568 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015569 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015570 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571
15572 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15573 save_cpo = p_cpo;
15574 p_cpo = (char_u *)"";
15575
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015576 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015577 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015578 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015579 /* type 3: return empty list when there are no matches.
15580 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015581 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015582 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015583 if (type == 4
15584 && (list_append_string(rettv->vval.v_list,
15585 (char_u *)"", 0) == FAIL
15586 || list_append_number(rettv->vval.v_list,
15587 (varnumber_T)-1) == FAIL
15588 || list_append_number(rettv->vval.v_list,
15589 (varnumber_T)-1) == FAIL
15590 || list_append_number(rettv->vval.v_list,
15591 (varnumber_T)-1) == FAIL))
15592 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015593 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015594 rettv->vval.v_list = NULL;
15595 goto theend;
15596 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597 }
15598 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015600 rettv->v_type = VAR_STRING;
15601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015604 if (argvars[0].v_type == VAR_LIST)
15605 {
15606 if ((l = argvars[0].vval.v_list) == NULL)
15607 goto theend;
15608 li = l->lv_first;
15609 }
15610 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015611 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015612 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015613 len = (long)STRLEN(str);
15614 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015616 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15617 if (pat == NULL)
15618 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015619
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015622 int error = FALSE;
15623
15624 start = get_tv_number_chk(&argvars[2], &error);
15625 if (error)
15626 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015627 if (l != NULL)
15628 {
15629 li = list_find(l, start);
15630 if (li == NULL)
15631 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015632 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015633 }
15634 else
15635 {
15636 if (start < 0)
15637 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015638 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015639 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015640 /* When "count" argument is there ignore matches before "start",
15641 * otherwise skip part of the string. Differs when pattern is "^"
15642 * or "\<". */
15643 if (argvars[3].v_type != VAR_UNKNOWN)
15644 startcol = start;
15645 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015646 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015647 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015648 len -= start;
15649 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015650 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015651
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015652 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015653 nth = get_tv_number_chk(&argvars[3], &error);
15654 if (error)
15655 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656 }
15657
15658 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15659 if (regmatch.regprog != NULL)
15660 {
15661 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015662
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015663 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015664 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015665 if (l != NULL)
15666 {
15667 if (li == NULL)
15668 {
15669 match = FALSE;
15670 break;
15671 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015672 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015673 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015674 if (str == NULL)
15675 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015676 }
15677
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015678 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015679
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015680 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015681 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015682 if (l == NULL && !match)
15683 break;
15684
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015685 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686 if (l != NULL)
15687 {
15688 li = li->li_next;
15689 ++idx;
15690 }
15691 else
15692 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015693#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015694 startcol = (colnr_T)(regmatch.startp[0]
15695 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015696#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015697 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015698#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015699 if (startcol > (colnr_T)len
15700 || str + startcol <= regmatch.startp[0])
15701 {
15702 match = FALSE;
15703 break;
15704 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015705 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015706 }
15707
15708 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015710 if (type == 4)
15711 {
15712 listitem_T *li1 = rettv->vval.v_list->lv_first;
15713 listitem_T *li2 = li1->li_next;
15714 listitem_T *li3 = li2->li_next;
15715 listitem_T *li4 = li3->li_next;
15716
15717 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15718 (int)(regmatch.endp[0] - regmatch.startp[0]));
15719 li3->li_tv.vval.v_number =
15720 (varnumber_T)(regmatch.startp[0] - expr);
15721 li4->li_tv.vval.v_number =
15722 (varnumber_T)(regmatch.endp[0] - expr);
15723 if (l != NULL)
15724 li2->li_tv.vval.v_number = (varnumber_T)idx;
15725 }
15726 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015727 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015728 int i;
15729
15730 /* return list with matched string and submatches */
15731 for (i = 0; i < NSUBEXP; ++i)
15732 {
15733 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015734 {
15735 if (list_append_string(rettv->vval.v_list,
15736 (char_u *)"", 0) == FAIL)
15737 break;
15738 }
15739 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015740 regmatch.startp[i],
15741 (int)(regmatch.endp[i] - regmatch.startp[i]))
15742 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015743 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015744 }
15745 }
15746 else if (type == 2)
15747 {
15748 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015749 if (l != NULL)
15750 copy_tv(&li->li_tv, rettv);
15751 else
15752 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015754 }
15755 else if (l != NULL)
15756 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015757 else
15758 {
15759 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 (varnumber_T)(regmatch.startp[0] - str);
15762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015763 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015765 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 }
15767 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015768 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015769 }
15770
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015771 if (type == 4 && l == NULL)
15772 /* matchstrpos() without a list: drop the second item. */
15773 listitem_remove(rettv->vval.v_list,
15774 rettv->vval.v_list->lv_first->li_next);
15775
Bram Moolenaar071d4272004-06-13 20:20:40 +000015776theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 p_cpo = save_cpo;
15779}
15780
15781/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015782 * "match()" function
15783 */
15784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015785f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015786{
15787 find_some_match(argvars, rettv, 1);
15788}
15789
15790/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015791 * "matchadd()" function
15792 */
15793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015794f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015795{
15796#ifdef FEAT_SEARCH_EXTRA
15797 char_u buf[NUMBUFLEN];
15798 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15799 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15800 int prio = 10; /* default priority */
15801 int id = -1;
15802 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015803 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015804
15805 rettv->vval.v_number = -1;
15806
15807 if (grp == NULL || pat == NULL)
15808 return;
15809 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015810 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015811 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015812 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015813 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015814 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015815 if (argvars[4].v_type != VAR_UNKNOWN)
15816 {
15817 if (argvars[4].v_type != VAR_DICT)
15818 {
15819 EMSG(_(e_dictreq));
15820 return;
15821 }
15822 if (dict_find(argvars[4].vval.v_dict,
15823 (char_u *)"conceal", -1) != NULL)
15824 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15825 (char_u *)"conceal", FALSE);
15826 }
15827 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015828 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015829 if (error == TRUE)
15830 return;
15831 if (id >= 1 && id <= 3)
15832 {
15833 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15834 return;
15835 }
15836
Bram Moolenaar6561d522015-07-21 15:48:27 +020015837 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15838 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015839#endif
15840}
15841
15842/*
15843 * "matchaddpos()" function
15844 */
15845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015846f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015847{
15848#ifdef FEAT_SEARCH_EXTRA
15849 char_u buf[NUMBUFLEN];
15850 char_u *group;
15851 int prio = 10;
15852 int id = -1;
15853 int error = FALSE;
15854 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015855 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015856
15857 rettv->vval.v_number = -1;
15858
15859 group = get_tv_string_buf_chk(&argvars[0], buf);
15860 if (group == NULL)
15861 return;
15862
15863 if (argvars[1].v_type != VAR_LIST)
15864 {
15865 EMSG2(_(e_listarg), "matchaddpos()");
15866 return;
15867 }
15868 l = argvars[1].vval.v_list;
15869 if (l == NULL)
15870 return;
15871
15872 if (argvars[2].v_type != VAR_UNKNOWN)
15873 {
15874 prio = get_tv_number_chk(&argvars[2], &error);
15875 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015876 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015877 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015878 if (argvars[4].v_type != VAR_UNKNOWN)
15879 {
15880 if (argvars[4].v_type != VAR_DICT)
15881 {
15882 EMSG(_(e_dictreq));
15883 return;
15884 }
15885 if (dict_find(argvars[4].vval.v_dict,
15886 (char_u *)"conceal", -1) != NULL)
15887 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15888 (char_u *)"conceal", FALSE);
15889 }
15890 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015891 }
15892 if (error == TRUE)
15893 return;
15894
15895 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15896 if (id == 1 || id == 2)
15897 {
15898 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15899 return;
15900 }
15901
Bram Moolenaar6561d522015-07-21 15:48:27 +020015902 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15903 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015904#endif
15905}
15906
15907/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015908 * "matcharg()" function
15909 */
15910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015911f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015912{
15913 if (rettv_list_alloc(rettv) == OK)
15914 {
15915#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015916 int id = get_tv_number(&argvars[0]);
15917 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015918
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015919 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015920 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015921 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15922 {
15923 list_append_string(rettv->vval.v_list,
15924 syn_id2name(m->hlg_id), -1);
15925 list_append_string(rettv->vval.v_list, m->pattern, -1);
15926 }
15927 else
15928 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015929 list_append_string(rettv->vval.v_list, NULL, -1);
15930 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015931 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015932 }
15933#endif
15934 }
15935}
15936
15937/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015938 * "matchdelete()" function
15939 */
15940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015941f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015942{
15943#ifdef FEAT_SEARCH_EXTRA
15944 rettv->vval.v_number = match_delete(curwin,
15945 (int)get_tv_number(&argvars[0]), TRUE);
15946#endif
15947}
15948
15949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950 * "matchend()" function
15951 */
15952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015953f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015954{
15955 find_some_match(argvars, rettv, 0);
15956}
15957
15958/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015959 * "matchlist()" function
15960 */
15961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015962f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015963{
15964 find_some_match(argvars, rettv, 3);
15965}
15966
15967/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015968 * "matchstr()" function
15969 */
15970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015971f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015972{
15973 find_some_match(argvars, rettv, 2);
15974}
15975
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015976/*
15977 * "matchstrpos()" function
15978 */
15979 static void
15980f_matchstrpos(typval_T *argvars, typval_T *rettv)
15981{
15982 find_some_match(argvars, rettv, 4);
15983}
15984
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015985static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015986
15987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015988max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015989{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015990 long n = 0;
15991 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015993
15994 if (argvars[0].v_type == VAR_LIST)
15995 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015996 list_T *l;
15997 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015998
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015999 l = argvars[0].vval.v_list;
16000 if (l != NULL)
16001 {
16002 li = l->lv_first;
16003 if (li != NULL)
16004 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016005 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016006 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016007 {
16008 li = li->li_next;
16009 if (li == NULL)
16010 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016011 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016012 if (domax ? i > n : i < n)
16013 n = i;
16014 }
16015 }
16016 }
16017 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016018 else if (argvars[0].v_type == VAR_DICT)
16019 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016020 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016021 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016022 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016023 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016024
16025 d = argvars[0].vval.v_dict;
16026 if (d != NULL)
16027 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016028 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016029 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016031 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016032 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016033 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016034 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016035 if (first)
16036 {
16037 n = i;
16038 first = FALSE;
16039 }
16040 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016041 n = i;
16042 }
16043 }
16044 }
16045 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016046 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016047 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016048 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016049}
16050
16051/*
16052 * "max()" function
16053 */
16054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016055f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016056{
16057 max_min(argvars, rettv, TRUE);
16058}
16059
16060/*
16061 * "min()" function
16062 */
16063 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016064f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016065{
16066 max_min(argvars, rettv, FALSE);
16067}
16068
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016069static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016070
16071/*
16072 * Create the directory in which "dir" is located, and higher levels when
16073 * needed.
16074 */
16075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016076mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016077{
16078 char_u *p;
16079 char_u *updir;
16080 int r = FAIL;
16081
16082 /* Get end of directory name in "dir".
16083 * We're done when it's "/" or "c:/". */
16084 p = gettail_sep(dir);
16085 if (p <= get_past_head(dir))
16086 return OK;
16087
16088 /* If the directory exists we're done. Otherwise: create it.*/
16089 updir = vim_strnsave(dir, (int)(p - dir));
16090 if (updir == NULL)
16091 return FAIL;
16092 if (mch_isdir(updir))
16093 r = OK;
16094 else if (mkdir_recurse(updir, prot) == OK)
16095 r = vim_mkdir_emsg(updir, prot);
16096 vim_free(updir);
16097 return r;
16098}
16099
16100#ifdef vim_mkdir
16101/*
16102 * "mkdir()" function
16103 */
16104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016105f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016106{
16107 char_u *dir;
16108 char_u buf[NUMBUFLEN];
16109 int prot = 0755;
16110
16111 rettv->vval.v_number = FAIL;
16112 if (check_restricted() || check_secure())
16113 return;
16114
16115 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016116 if (*dir == NUL)
16117 rettv->vval.v_number = FAIL;
16118 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016119 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016120 if (*gettail(dir) == NUL)
16121 /* remove trailing slashes */
16122 *gettail_sep(dir) = NUL;
16123
16124 if (argvars[1].v_type != VAR_UNKNOWN)
16125 {
16126 if (argvars[2].v_type != VAR_UNKNOWN)
16127 prot = get_tv_number_chk(&argvars[2], NULL);
16128 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16129 mkdir_recurse(dir, prot);
16130 }
16131 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016132 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016133}
16134#endif
16135
Bram Moolenaar0d660222005-01-07 21:51:51 +000016136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 * "mode()" function
16138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016140f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016141{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016142 char_u buf[3];
16143
16144 buf[1] = NUL;
16145 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016146
Bram Moolenaar071d4272004-06-13 20:20:40 +000016147 if (VIsual_active)
16148 {
16149 if (VIsual_select)
16150 buf[0] = VIsual_mode + 's' - 'v';
16151 else
16152 buf[0] = VIsual_mode;
16153 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016154 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016155 || State == CONFIRM)
16156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016158 if (State == ASKMORE)
16159 buf[1] = 'm';
16160 else if (State == CONFIRM)
16161 buf[1] = '?';
16162 }
16163 else if (State == EXTERNCMD)
16164 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016165 else if (State & INSERT)
16166 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016167#ifdef FEAT_VREPLACE
16168 if (State & VREPLACE_FLAG)
16169 {
16170 buf[0] = 'R';
16171 buf[1] = 'v';
16172 }
16173 else
16174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 if (State & REPLACE_FLAG)
16176 buf[0] = 'R';
16177 else
16178 buf[0] = 'i';
16179 }
16180 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016182 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016183 if (exmode_active)
16184 buf[1] = 'v';
16185 }
16186 else if (exmode_active)
16187 {
16188 buf[0] = 'c';
16189 buf[1] = 'e';
16190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016191 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016192 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016194 if (finish_op)
16195 buf[1] = 'o';
16196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016197
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016198 /* Clear out the minor mode when the argument is not a non-zero number or
16199 * non-empty string. */
16200 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016201 buf[1] = NUL;
16202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016203 rettv->vval.v_string = vim_strsave(buf);
16204 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205}
16206
Bram Moolenaar429fa852013-04-15 12:27:36 +020016207#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016208/*
16209 * "mzeval()" function
16210 */
16211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016212f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016213{
16214 char_u *str;
16215 char_u buf[NUMBUFLEN];
16216
16217 str = get_tv_string_buf(&argvars[0], buf);
16218 do_mzeval(str, rettv);
16219}
Bram Moolenaar75676462013-01-30 14:55:42 +010016220
16221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016223{
16224 typval_T argvars[3];
16225
16226 argvars[0].v_type = VAR_STRING;
16227 argvars[0].vval.v_string = name;
16228 copy_tv(args, &argvars[1]);
16229 argvars[2].v_type = VAR_UNKNOWN;
16230 f_call(argvars, rettv);
16231 clear_tv(&argvars[1]);
16232}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016233#endif
16234
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016236 * "nextnonblank()" function
16237 */
16238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016239f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016240{
16241 linenr_T lnum;
16242
16243 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016245 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016246 {
16247 lnum = 0;
16248 break;
16249 }
16250 if (*skipwhite(ml_get(lnum)) != NUL)
16251 break;
16252 }
16253 rettv->vval.v_number = lnum;
16254}
16255
16256/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 * "nr2char()" function
16258 */
16259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016260f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261{
16262 char_u buf[NUMBUFLEN];
16263
16264#ifdef FEAT_MBYTE
16265 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016266 {
16267 int utf8 = 0;
16268
16269 if (argvars[1].v_type != VAR_UNKNOWN)
16270 utf8 = get_tv_number_chk(&argvars[1], NULL);
16271 if (utf8)
16272 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16273 else
16274 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276 else
16277#endif
16278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016279 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280 buf[1] = NUL;
16281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016282 rettv->v_type = VAR_STRING;
16283 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016284}
16285
16286/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016287 * "or(expr, expr)" function
16288 */
16289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016290f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016291{
16292 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16293 | get_tv_number_chk(&argvars[1], NULL);
16294}
16295
16296/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016297 * "pathshorten()" function
16298 */
16299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016300f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016301{
16302 char_u *p;
16303
16304 rettv->v_type = VAR_STRING;
16305 p = get_tv_string_chk(&argvars[0]);
16306 if (p == NULL)
16307 rettv->vval.v_string = NULL;
16308 else
16309 {
16310 p = vim_strsave(p);
16311 rettv->vval.v_string = p;
16312 if (p != NULL)
16313 shorten_dir(p);
16314 }
16315}
16316
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016317#ifdef FEAT_PERL
16318/*
16319 * "perleval()" function
16320 */
16321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016322f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016323{
16324 char_u *str;
16325 char_u buf[NUMBUFLEN];
16326
16327 str = get_tv_string_buf(&argvars[0], buf);
16328 do_perleval(str, rettv);
16329}
16330#endif
16331
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016332#ifdef FEAT_FLOAT
16333/*
16334 * "pow()" function
16335 */
16336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016337f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016338{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016339 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016340
16341 rettv->v_type = VAR_FLOAT;
16342 if (get_float_arg(argvars, &fx) == OK
16343 && get_float_arg(&argvars[1], &fy) == OK)
16344 rettv->vval.v_float = pow(fx, fy);
16345 else
16346 rettv->vval.v_float = 0.0;
16347}
16348#endif
16349
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016350/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016351 * "prevnonblank()" function
16352 */
16353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016354f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016355{
16356 linenr_T lnum;
16357
16358 lnum = get_tv_lnum(argvars);
16359 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16360 lnum = 0;
16361 else
16362 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16363 --lnum;
16364 rettv->vval.v_number = lnum;
16365}
16366
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016367/* This dummy va_list is here because:
16368 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16369 * - locally in the function results in a "used before set" warning
16370 * - using va_start() to initialize it gives "function with fixed args" error */
16371static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016372
Bram Moolenaar8c711452005-01-14 21:53:12 +000016373/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016374 * "printf()" function
16375 */
16376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016377f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016378{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016379 char_u buf[NUMBUFLEN];
16380 int len;
16381 char_u *s;
16382 int saved_did_emsg = did_emsg;
16383 char *fmt;
16384
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016385 rettv->v_type = VAR_STRING;
16386 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016387
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016388 /* Get the required length, allocate the buffer and do it for real. */
16389 did_emsg = FALSE;
16390 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16391 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16392 if (!did_emsg)
16393 {
16394 s = alloc(len + 1);
16395 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016396 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016397 rettv->vval.v_string = s;
16398 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016399 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016400 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016401 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016402}
16403
16404/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016405 * "pumvisible()" function
16406 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016408f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016409{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016410#ifdef FEAT_INS_EXPAND
16411 if (pum_visible())
16412 rettv->vval.v_number = 1;
16413#endif
16414}
16415
Bram Moolenaardb913952012-06-29 12:54:53 +020016416#ifdef FEAT_PYTHON3
16417/*
16418 * "py3eval()" function
16419 */
16420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016421f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016422{
16423 char_u *str;
16424 char_u buf[NUMBUFLEN];
16425
16426 str = get_tv_string_buf(&argvars[0], buf);
16427 do_py3eval(str, rettv);
16428}
16429#endif
16430
16431#ifdef FEAT_PYTHON
16432/*
16433 * "pyeval()" function
16434 */
16435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016436f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016437{
16438 char_u *str;
16439 char_u buf[NUMBUFLEN];
16440
16441 str = get_tv_string_buf(&argvars[0], buf);
16442 do_pyeval(str, rettv);
16443}
16444#endif
16445
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016446/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016447 * "range()" function
16448 */
16449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016450f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016451{
16452 long start;
16453 long end;
16454 long stride = 1;
16455 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016456 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016458 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016459 if (argvars[1].v_type == VAR_UNKNOWN)
16460 {
16461 end = start - 1;
16462 start = 0;
16463 }
16464 else
16465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016466 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016467 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016468 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016469 }
16470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016471 if (error)
16472 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016473 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016474 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016475 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016476 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016477 else
16478 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016479 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016480 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016481 if (list_append_number(rettv->vval.v_list,
16482 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016483 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016484 }
16485}
16486
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016487/*
16488 * "readfile()" function
16489 */
16490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016491f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016492{
16493 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016494 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016495 char_u *fname;
16496 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016497 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16498 int io_size = sizeof(buf);
16499 int readlen; /* size of last fread() */
16500 char_u *prev = NULL; /* previously read bytes, if any */
16501 long prevlen = 0; /* length of data in prev */
16502 long prevsize = 0; /* size of prev buffer */
16503 long maxline = MAXLNUM;
16504 long cnt = 0;
16505 char_u *p; /* position in buf */
16506 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016507
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016508 if (argvars[1].v_type != VAR_UNKNOWN)
16509 {
16510 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16511 binary = TRUE;
16512 if (argvars[2].v_type != VAR_UNKNOWN)
16513 maxline = get_tv_number(&argvars[2]);
16514 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016515
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016516 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016517 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016518
16519 /* Always open the file in binary mode, library functions have a mind of
16520 * their own about CR-LF conversion. */
16521 fname = get_tv_string(&argvars[0]);
16522 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16523 {
16524 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16525 return;
16526 }
16527
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016528 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016529 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016530 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016531
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016532 /* This for loop processes what was read, but is also entered at end
16533 * of file so that either:
16534 * - an incomplete line gets written
16535 * - a "binary" file gets an empty line at the end if it ends in a
16536 * newline. */
16537 for (p = buf, start = buf;
16538 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16539 ++p)
16540 {
16541 if (*p == '\n' || readlen <= 0)
16542 {
16543 listitem_T *li;
16544 char_u *s = NULL;
16545 long_u len = p - start;
16546
16547 /* Finished a line. Remove CRs before NL. */
16548 if (readlen > 0 && !binary)
16549 {
16550 while (len > 0 && start[len - 1] == '\r')
16551 --len;
16552 /* removal may cross back to the "prev" string */
16553 if (len == 0)
16554 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16555 --prevlen;
16556 }
16557 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016558 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016559 else
16560 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 /* Change "prev" buffer to be the right size. This way
16562 * the bytes are only copied once, and very long lines are
16563 * allocated only once. */
16564 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016565 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016566 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016567 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016568 prev = NULL; /* the list will own the string */
16569 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016570 }
16571 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016572 if (s == NULL)
16573 {
16574 do_outofmem_msg((long_u) prevlen + len + 1);
16575 failed = TRUE;
16576 break;
16577 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016578
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016580 {
16581 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016582 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016583 break;
16584 }
16585 li->li_tv.v_type = VAR_STRING;
16586 li->li_tv.v_lock = 0;
16587 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016588 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016589
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016590 start = p + 1; /* step over newline */
16591 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016592 break;
16593 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016594 else if (*p == NUL)
16595 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016596#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016597 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16598 * when finding the BF and check the previous two bytes. */
16599 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016600 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016601 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16602 * + 1, these may be in the "prev" string. */
16603 char_u back1 = p >= buf + 1 ? p[-1]
16604 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16605 char_u back2 = p >= buf + 2 ? p[-2]
16606 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16607 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016608
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016609 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016610 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 char_u *dest = p - 2;
16612
16613 /* Usually a BOM is at the beginning of a file, and so at
16614 * the beginning of a line; then we can just step over it.
16615 */
16616 if (start == dest)
16617 start = p + 1;
16618 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016619 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016620 /* have to shuffle buf to close gap */
16621 int adjust_prevlen = 0;
16622
16623 if (dest < buf)
16624 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016625 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016626 dest = buf;
16627 }
16628 if (readlen > p - buf + 1)
16629 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16630 readlen -= 3 - adjust_prevlen;
16631 prevlen -= adjust_prevlen;
16632 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016633 }
16634 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016635 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636#endif
16637 } /* for */
16638
16639 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16640 break;
16641 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 /* There's part of a line in buf, store it in "prev". */
16644 if (p - start + prevlen >= prevsize)
16645 {
16646 /* need bigger "prev" buffer */
16647 char_u *newprev;
16648
16649 /* A common use case is ordinary text files and "prev" gets a
16650 * fragment of a line, so the first allocation is made
16651 * small, to avoid repeatedly 'allocing' large and
16652 * 'reallocing' small. */
16653 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016654 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016655 else
16656 {
16657 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016658 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016659 prevsize = grow50pc > growmin ? grow50pc : growmin;
16660 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016661 newprev = prev == NULL ? alloc(prevsize)
16662 : vim_realloc(prev, prevsize);
16663 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016664 {
16665 do_outofmem_msg((long_u)prevsize);
16666 failed = TRUE;
16667 break;
16668 }
16669 prev = newprev;
16670 }
16671 /* Add the line part to end of "prev". */
16672 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016673 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016674 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016675 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016676
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016677 /*
16678 * For a negative line count use only the lines at the end of the file,
16679 * free the rest.
16680 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016681 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016682 while (cnt > -maxline)
16683 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016684 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016685 --cnt;
16686 }
16687
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016688 if (failed)
16689 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016690 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016691 /* readfile doc says an empty list is returned on error */
16692 rettv->vval.v_list = list_alloc();
16693 }
16694
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016695 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696 fclose(fd);
16697}
16698
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016699#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016700static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016701
16702/*
16703 * Convert a List to proftime_T.
16704 * Return FAIL when there is something wrong.
16705 */
16706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016707list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016708{
16709 long n1, n2;
16710 int error = FALSE;
16711
16712 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16713 || arg->vval.v_list->lv_len != 2)
16714 return FAIL;
16715 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16716 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16717# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016718 tm->HighPart = n1;
16719 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016720# else
16721 tm->tv_sec = n1;
16722 tm->tv_usec = n2;
16723# endif
16724 return error ? FAIL : OK;
16725}
16726#endif /* FEAT_RELTIME */
16727
16728/*
16729 * "reltime()" function
16730 */
16731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016732f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016733{
16734#ifdef FEAT_RELTIME
16735 proftime_T res;
16736 proftime_T start;
16737
16738 if (argvars[0].v_type == VAR_UNKNOWN)
16739 {
16740 /* No arguments: get current time. */
16741 profile_start(&res);
16742 }
16743 else if (argvars[1].v_type == VAR_UNKNOWN)
16744 {
16745 if (list2proftime(&argvars[0], &res) == FAIL)
16746 return;
16747 profile_end(&res);
16748 }
16749 else
16750 {
16751 /* Two arguments: compute the difference. */
16752 if (list2proftime(&argvars[0], &start) == FAIL
16753 || list2proftime(&argvars[1], &res) == FAIL)
16754 return;
16755 profile_sub(&res, &start);
16756 }
16757
16758 if (rettv_list_alloc(rettv) == OK)
16759 {
16760 long n1, n2;
16761
16762# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016763 n1 = res.HighPart;
16764 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016765# else
16766 n1 = res.tv_sec;
16767 n2 = res.tv_usec;
16768# endif
16769 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16770 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16771 }
16772#endif
16773}
16774
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016775#ifdef FEAT_FLOAT
16776/*
16777 * "reltimefloat()" function
16778 */
16779 static void
16780f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16781{
16782# ifdef FEAT_RELTIME
16783 proftime_T tm;
16784# endif
16785
16786 rettv->v_type = VAR_FLOAT;
16787 rettv->vval.v_float = 0;
16788# ifdef FEAT_RELTIME
16789 if (list2proftime(&argvars[0], &tm) == OK)
16790 rettv->vval.v_float = profile_float(&tm);
16791# endif
16792}
16793#endif
16794
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016795/*
16796 * "reltimestr()" function
16797 */
16798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016799f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016800{
16801#ifdef FEAT_RELTIME
16802 proftime_T tm;
16803#endif
16804
16805 rettv->v_type = VAR_STRING;
16806 rettv->vval.v_string = NULL;
16807#ifdef FEAT_RELTIME
16808 if (list2proftime(&argvars[0], &tm) == OK)
16809 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16810#endif
16811}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016812
Bram Moolenaar0d660222005-01-07 21:51:51 +000016813#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016814static void make_connection(void);
16815static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016816
16817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016818make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016819{
16820 if (X_DISPLAY == NULL
16821# ifdef FEAT_GUI
16822 && !gui.in_use
16823# endif
16824 )
16825 {
16826 x_force_connect = TRUE;
16827 setup_term_clip();
16828 x_force_connect = FALSE;
16829 }
16830}
16831
16832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016833check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016834{
16835 make_connection();
16836 if (X_DISPLAY == NULL)
16837 {
16838 EMSG(_("E240: No connection to Vim server"));
16839 return FAIL;
16840 }
16841 return OK;
16842}
16843#endif
16844
16845#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016847remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016848{
16849 char_u *server_name;
16850 char_u *keys;
16851 char_u *r = NULL;
16852 char_u buf[NUMBUFLEN];
16853# ifdef WIN32
16854 HWND w;
16855# else
16856 Window w;
16857# endif
16858
16859 if (check_restricted() || check_secure())
16860 return;
16861
16862# ifdef FEAT_X11
16863 if (check_connection() == FAIL)
16864 return;
16865# endif
16866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016867 server_name = get_tv_string_chk(&argvars[0]);
16868 if (server_name == NULL)
16869 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870 keys = get_tv_string_buf(&argvars[1], buf);
16871# ifdef WIN32
16872 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16873# else
16874 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16875 < 0)
16876# endif
16877 {
16878 if (r != NULL)
16879 EMSG(r); /* sending worked but evaluation failed */
16880 else
16881 EMSG2(_("E241: Unable to send to %s"), server_name);
16882 return;
16883 }
16884
16885 rettv->vval.v_string = r;
16886
16887 if (argvars[2].v_type != VAR_UNKNOWN)
16888 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016889 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016890 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016891 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016892
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016893 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016894 v.di_tv.v_type = VAR_STRING;
16895 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016896 idvar = get_tv_string_chk(&argvars[2]);
16897 if (idvar != NULL)
16898 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016899 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900 }
16901}
16902#endif
16903
16904/*
16905 * "remote_expr()" function
16906 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016908f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909{
16910 rettv->v_type = VAR_STRING;
16911 rettv->vval.v_string = NULL;
16912#ifdef FEAT_CLIENTSERVER
16913 remote_common(argvars, rettv, TRUE);
16914#endif
16915}
16916
16917/*
16918 * "remote_foreground()" function
16919 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016921f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016922{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923#ifdef FEAT_CLIENTSERVER
16924# ifdef WIN32
16925 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016926 {
16927 char_u *server_name = get_tv_string_chk(&argvars[0]);
16928
16929 if (server_name != NULL)
16930 serverForeground(server_name);
16931 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016932# else
16933 /* Send a foreground() expression to the server. */
16934 argvars[1].v_type = VAR_STRING;
16935 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16936 argvars[2].v_type = VAR_UNKNOWN;
16937 remote_common(argvars, rettv, TRUE);
16938 vim_free(argvars[1].vval.v_string);
16939# endif
16940#endif
16941}
16942
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016944f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945{
16946#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016947 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 char_u *s = NULL;
16949# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016950 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016952 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016953
16954 if (check_restricted() || check_secure())
16955 {
16956 rettv->vval.v_number = -1;
16957 return;
16958 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 serverid = get_tv_string_chk(&argvars[0]);
16960 if (serverid == NULL)
16961 {
16962 rettv->vval.v_number = -1;
16963 return; /* type error; errmsg already given */
16964 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016966 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 if (n == 0)
16968 rettv->vval.v_number = -1;
16969 else
16970 {
16971 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16972 rettv->vval.v_number = (s != NULL);
16973 }
16974# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 if (check_connection() == FAIL)
16976 return;
16977
16978 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016979 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980# endif
16981
16982 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 char_u *retvar;
16985
Bram Moolenaar33570922005-01-25 22:26:29 +000016986 v.di_tv.v_type = VAR_STRING;
16987 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016988 retvar = get_tv_string_chk(&argvars[1]);
16989 if (retvar != NULL)
16990 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016991 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 }
16993#else
16994 rettv->vval.v_number = -1;
16995#endif
16996}
16997
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016999f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000{
17001 char_u *r = NULL;
17002
17003#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017004 char_u *serverid = get_tv_string_chk(&argvars[0]);
17005
17006 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007 {
17008# ifdef WIN32
17009 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017010 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017012 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013 if (n != 0)
17014 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17015 if (r == NULL)
17016# else
17017 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019# endif
17020 EMSG(_("E277: Unable to read a server reply"));
17021 }
17022#endif
17023 rettv->v_type = VAR_STRING;
17024 rettv->vval.v_string = r;
17025}
17026
17027/*
17028 * "remote_send()" function
17029 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017031f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032{
17033 rettv->v_type = VAR_STRING;
17034 rettv->vval.v_string = NULL;
17035#ifdef FEAT_CLIENTSERVER
17036 remote_common(argvars, rettv, FALSE);
17037#endif
17038}
17039
17040/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017041 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017042 */
17043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017044f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017045{
Bram Moolenaar33570922005-01-25 22:26:29 +000017046 list_T *l;
17047 listitem_T *item, *item2;
17048 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017049 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017050 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017051 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017052 dict_T *d;
17053 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017054 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017055
Bram Moolenaar8c711452005-01-14 21:53:12 +000017056 if (argvars[0].v_type == VAR_DICT)
17057 {
17058 if (argvars[2].v_type != VAR_UNKNOWN)
17059 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017060 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017061 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017062 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017063 key = get_tv_string_chk(&argvars[1]);
17064 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017066 di = dict_find(d, key, -1);
17067 if (di == NULL)
17068 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017069 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17070 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017071 {
17072 *rettv = di->di_tv;
17073 init_tv(&di->di_tv);
17074 dictitem_remove(d, di);
17075 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017076 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017077 }
17078 }
17079 else if (argvars[0].v_type != VAR_LIST)
17080 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017081 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017082 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 int error = FALSE;
17085
17086 idx = get_tv_number_chk(&argvars[1], &error);
17087 if (error)
17088 ; /* type error: do nothing, errmsg already given */
17089 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017090 EMSGN(_(e_listidx), idx);
17091 else
17092 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017093 if (argvars[2].v_type == VAR_UNKNOWN)
17094 {
17095 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017096 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017097 *rettv = item->li_tv;
17098 vim_free(item);
17099 }
17100 else
17101 {
17102 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017103 end = get_tv_number_chk(&argvars[2], &error);
17104 if (error)
17105 ; /* type error: do nothing */
17106 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017107 EMSGN(_(e_listidx), end);
17108 else
17109 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017110 int cnt = 0;
17111
17112 for (li = item; li != NULL; li = li->li_next)
17113 {
17114 ++cnt;
17115 if (li == item2)
17116 break;
17117 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017118 if (li == NULL) /* didn't find "item2" after "item" */
17119 EMSG(_(e_invrange));
17120 else
17121 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017122 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017123 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017124 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017125 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017126 l->lv_first = item;
17127 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017128 item->li_prev = NULL;
17129 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017130 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017131 }
17132 }
17133 }
17134 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017135 }
17136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017137}
17138
17139/*
17140 * "rename({from}, {to})" function
17141 */
17142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017143f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017144{
17145 char_u buf[NUMBUFLEN];
17146
17147 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017148 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017149 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017150 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17151 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152}
17153
17154/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017155 * "repeat()" function
17156 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017158f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017159{
17160 char_u *p;
17161 int n;
17162 int slen;
17163 int len;
17164 char_u *r;
17165 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017166
17167 n = get_tv_number(&argvars[1]);
17168 if (argvars[0].v_type == VAR_LIST)
17169 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017170 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017171 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017172 if (list_extend(rettv->vval.v_list,
17173 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017174 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017175 }
17176 else
17177 {
17178 p = get_tv_string(&argvars[0]);
17179 rettv->v_type = VAR_STRING;
17180 rettv->vval.v_string = NULL;
17181
17182 slen = (int)STRLEN(p);
17183 len = slen * n;
17184 if (len <= 0)
17185 return;
17186
17187 r = alloc(len + 1);
17188 if (r != NULL)
17189 {
17190 for (i = 0; i < n; i++)
17191 mch_memmove(r + i * slen, p, (size_t)slen);
17192 r[len] = NUL;
17193 }
17194
17195 rettv->vval.v_string = r;
17196 }
17197}
17198
17199/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 * "resolve()" function
17201 */
17202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017203f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204{
17205 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017206#ifdef HAVE_READLINK
17207 char_u *buf = NULL;
17208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017210 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211#ifdef FEAT_SHORTCUT
17212 {
17213 char_u *v = NULL;
17214
17215 v = mch_resolve_shortcut(p);
17216 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017217 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017219 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 }
17221#else
17222# ifdef HAVE_READLINK
17223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 char_u *cpy;
17225 int len;
17226 char_u *remain = NULL;
17227 char_u *q;
17228 int is_relative_to_current = FALSE;
17229 int has_trailing_pathsep = FALSE;
17230 int limit = 100;
17231
17232 p = vim_strsave(p);
17233
17234 if (p[0] == '.' && (vim_ispathsep(p[1])
17235 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17236 is_relative_to_current = TRUE;
17237
17238 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017239 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017242 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244
17245 q = getnextcomp(p);
17246 if (*q != NUL)
17247 {
17248 /* Separate the first path component in "p", and keep the
17249 * remainder (beginning with the path separator). */
17250 remain = vim_strsave(q - 1);
17251 q[-1] = NUL;
17252 }
17253
Bram Moolenaard9462e32011-04-11 21:35:11 +020017254 buf = alloc(MAXPATHL + 1);
17255 if (buf == NULL)
17256 goto fail;
17257
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 for (;;)
17259 {
17260 for (;;)
17261 {
17262 len = readlink((char *)p, (char *)buf, MAXPATHL);
17263 if (len <= 0)
17264 break;
17265 buf[len] = NUL;
17266
17267 if (limit-- == 0)
17268 {
17269 vim_free(p);
17270 vim_free(remain);
17271 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017272 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 goto fail;
17274 }
17275
17276 /* Ensure that the result will have a trailing path separator
17277 * if the argument has one. */
17278 if (remain == NULL && has_trailing_pathsep)
17279 add_pathsep(buf);
17280
17281 /* Separate the first path component in the link value and
17282 * concatenate the remainders. */
17283 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17284 if (*q != NUL)
17285 {
17286 if (remain == NULL)
17287 remain = vim_strsave(q - 1);
17288 else
17289 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017290 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017291 if (cpy != NULL)
17292 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293 vim_free(remain);
17294 remain = cpy;
17295 }
17296 }
17297 q[-1] = NUL;
17298 }
17299
17300 q = gettail(p);
17301 if (q > p && *q == NUL)
17302 {
17303 /* Ignore trailing path separator. */
17304 q[-1] = NUL;
17305 q = gettail(p);
17306 }
17307 if (q > p && !mch_isFullName(buf))
17308 {
17309 /* symlink is relative to directory of argument */
17310 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17311 if (cpy != NULL)
17312 {
17313 STRCPY(cpy, p);
17314 STRCPY(gettail(cpy), buf);
17315 vim_free(p);
17316 p = cpy;
17317 }
17318 }
17319 else
17320 {
17321 vim_free(p);
17322 p = vim_strsave(buf);
17323 }
17324 }
17325
17326 if (remain == NULL)
17327 break;
17328
17329 /* Append the first path component of "remain" to "p". */
17330 q = getnextcomp(remain + 1);
17331 len = q - remain - (*q != NUL);
17332 cpy = vim_strnsave(p, STRLEN(p) + len);
17333 if (cpy != NULL)
17334 {
17335 STRNCAT(cpy, remain, len);
17336 vim_free(p);
17337 p = cpy;
17338 }
17339 /* Shorten "remain". */
17340 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017341 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 else
17343 {
17344 vim_free(remain);
17345 remain = NULL;
17346 }
17347 }
17348
17349 /* If the result is a relative path name, make it explicitly relative to
17350 * the current directory if and only if the argument had this form. */
17351 if (!vim_ispathsep(*p))
17352 {
17353 if (is_relative_to_current
17354 && *p != NUL
17355 && !(p[0] == '.'
17356 && (p[1] == NUL
17357 || vim_ispathsep(p[1])
17358 || (p[1] == '.'
17359 && (p[2] == NUL
17360 || vim_ispathsep(p[2]))))))
17361 {
17362 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017363 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017364 if (cpy != NULL)
17365 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 vim_free(p);
17367 p = cpy;
17368 }
17369 }
17370 else if (!is_relative_to_current)
17371 {
17372 /* Strip leading "./". */
17373 q = p;
17374 while (q[0] == '.' && vim_ispathsep(q[1]))
17375 q += 2;
17376 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017377 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378 }
17379 }
17380
17381 /* Ensure that the result will have no trailing path separator
17382 * if the argument had none. But keep "/" or "//". */
17383 if (!has_trailing_pathsep)
17384 {
17385 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017386 if (after_pathsep(p, q))
17387 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 }
17389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017390 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 }
17392# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017393 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394# endif
17395#endif
17396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017397 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017398
17399#ifdef HAVE_READLINK
17400fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017401 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017403 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404}
17405
17406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017407 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 */
17409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017410f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411{
Bram Moolenaar33570922005-01-25 22:26:29 +000017412 list_T *l;
17413 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017414
Bram Moolenaar0d660222005-01-07 21:51:51 +000017415 if (argvars[0].v_type != VAR_LIST)
17416 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017417 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017418 && !tv_check_lock(l->lv_lock,
17419 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017420 {
17421 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017422 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017423 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017424 while (li != NULL)
17425 {
17426 ni = li->li_prev;
17427 list_append(l, li);
17428 li = ni;
17429 }
17430 rettv->vval.v_list = l;
17431 rettv->v_type = VAR_LIST;
17432 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017433 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435}
17436
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017437#define SP_NOMOVE 0x01 /* don't move cursor */
17438#define SP_REPEAT 0x02 /* repeat to find outer pair */
17439#define SP_RETCOUNT 0x04 /* return matchcount */
17440#define SP_SETPCMARK 0x08 /* set previous context mark */
17441#define SP_START 0x10 /* accept match at start position */
17442#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17443#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017444#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017445
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017446static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017447
17448/*
17449 * Get flags for a search function.
17450 * Possibly sets "p_ws".
17451 * Returns BACKWARD, FORWARD or zero (for an error).
17452 */
17453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017454get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017455{
17456 int dir = FORWARD;
17457 char_u *flags;
17458 char_u nbuf[NUMBUFLEN];
17459 int mask;
17460
17461 if (varp->v_type != VAR_UNKNOWN)
17462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017463 flags = get_tv_string_buf_chk(varp, nbuf);
17464 if (flags == NULL)
17465 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017466 while (*flags != NUL)
17467 {
17468 switch (*flags)
17469 {
17470 case 'b': dir = BACKWARD; break;
17471 case 'w': p_ws = TRUE; break;
17472 case 'W': p_ws = FALSE; break;
17473 default: mask = 0;
17474 if (flagsp != NULL)
17475 switch (*flags)
17476 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017477 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017478 case 'e': mask = SP_END; break;
17479 case 'm': mask = SP_RETCOUNT; break;
17480 case 'n': mask = SP_NOMOVE; break;
17481 case 'p': mask = SP_SUBPAT; break;
17482 case 'r': mask = SP_REPEAT; break;
17483 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017484 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017485 }
17486 if (mask == 0)
17487 {
17488 EMSG2(_(e_invarg2), flags);
17489 dir = 0;
17490 }
17491 else
17492 *flagsp |= mask;
17493 }
17494 if (dir == 0)
17495 break;
17496 ++flags;
17497 }
17498 }
17499 return dir;
17500}
17501
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017503 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017506search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017508 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509 char_u *pat;
17510 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017511 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 int save_p_ws = p_ws;
17513 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017514 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017515 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017516 proftime_T tm;
17517#ifdef FEAT_RELTIME
17518 long time_limit = 0;
17519#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017520 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017521 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017524 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017525 if (dir == 0)
17526 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017527 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017528 if (flags & SP_START)
17529 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017530 if (flags & SP_END)
17531 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017532 if (flags & SP_COLUMN)
17533 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017534
Bram Moolenaar76929292008-01-06 19:07:36 +000017535 /* Optional arguments: line number to stop searching and timeout. */
17536 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017537 {
17538 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17539 if (lnum_stop < 0)
17540 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017541#ifdef FEAT_RELTIME
17542 if (argvars[3].v_type != VAR_UNKNOWN)
17543 {
17544 time_limit = get_tv_number_chk(&argvars[3], NULL);
17545 if (time_limit < 0)
17546 goto theend;
17547 }
17548#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017549 }
17550
Bram Moolenaar76929292008-01-06 19:07:36 +000017551#ifdef FEAT_RELTIME
17552 /* Set the time limit, if there is one. */
17553 profile_setlimit(time_limit, &tm);
17554#endif
17555
Bram Moolenaar231334e2005-07-25 20:46:57 +000017556 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017557 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017558 * Check to make sure only those flags are set.
17559 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17560 * flags cannot be set. Check for that condition also.
17561 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017562 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017563 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017565 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017566 goto theend;
17567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017569 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017570 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017571 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017572 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017574 if (flags & SP_SUBPAT)
17575 retval = subpatnum;
17576 else
17577 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017578 if (flags & SP_SETPCMARK)
17579 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017580 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017581 if (match_pos != NULL)
17582 {
17583 /* Store the match cursor position */
17584 match_pos->lnum = pos.lnum;
17585 match_pos->col = pos.col + 1;
17586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587 /* "/$" will put the cursor after the end of the line, may need to
17588 * correct that here */
17589 check_cursor();
17590 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017591
17592 /* If 'n' flag is used: restore cursor position. */
17593 if (flags & SP_NOMOVE)
17594 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017595 else
17596 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017597theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017599
17600 return retval;
17601}
17602
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017603#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017604
17605/*
17606 * round() is not in C90, use ceil() or floor() instead.
17607 */
17608 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017609vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017610{
17611 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17612}
17613
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017614/*
17615 * "round({float})" function
17616 */
17617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017618f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017619{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017620 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017621
17622 rettv->v_type = VAR_FLOAT;
17623 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017624 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017625 else
17626 rettv->vval.v_float = 0.0;
17627}
17628#endif
17629
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017630/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017631 * "screenattr()" function
17632 */
17633 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017634f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017635{
17636 int row;
17637 int col;
17638 int c;
17639
17640 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17641 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17642 if (row < 0 || row >= screen_Rows
17643 || col < 0 || col >= screen_Columns)
17644 c = -1;
17645 else
17646 c = ScreenAttrs[LineOffset[row] + col];
17647 rettv->vval.v_number = c;
17648}
17649
17650/*
17651 * "screenchar()" function
17652 */
17653 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017654f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017655{
17656 int row;
17657 int col;
17658 int off;
17659 int c;
17660
17661 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17662 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17663 if (row < 0 || row >= screen_Rows
17664 || col < 0 || col >= screen_Columns)
17665 c = -1;
17666 else
17667 {
17668 off = LineOffset[row] + col;
17669#ifdef FEAT_MBYTE
17670 if (enc_utf8 && ScreenLinesUC[off] != 0)
17671 c = ScreenLinesUC[off];
17672 else
17673#endif
17674 c = ScreenLines[off];
17675 }
17676 rettv->vval.v_number = c;
17677}
17678
17679/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017680 * "screencol()" function
17681 *
17682 * First column is 1 to be consistent with virtcol().
17683 */
17684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017685f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017686{
17687 rettv->vval.v_number = screen_screencol() + 1;
17688}
17689
17690/*
17691 * "screenrow()" function
17692 */
17693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017694f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017695{
17696 rettv->vval.v_number = screen_screenrow() + 1;
17697}
17698
17699/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017700 * "search()" function
17701 */
17702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017703f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017704{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017705 int flags = 0;
17706
17707 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708}
17709
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017711 * "searchdecl()" function
17712 */
17713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017714f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017715{
17716 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017717 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017718 int error = FALSE;
17719 char_u *name;
17720
17721 rettv->vval.v_number = 1; /* default: FAIL */
17722
17723 name = get_tv_string_chk(&argvars[0]);
17724 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017725 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017726 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017727 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17728 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17729 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017730 if (!error && name != NULL)
17731 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017732 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017733}
17734
17735/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017736 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017738 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017739searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740{
17741 char_u *spat, *mpat, *epat;
17742 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 int dir;
17745 int flags = 0;
17746 char_u nbuf1[NUMBUFLEN];
17747 char_u nbuf2[NUMBUFLEN];
17748 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017749 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017750 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017751 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017754 spat = get_tv_string_chk(&argvars[0]);
17755 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17756 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17757 if (spat == NULL || mpat == NULL || epat == NULL)
17758 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 /* Handle the optional fourth argument: flags */
17761 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017762 if (dir == 0)
17763 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017764
17765 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017766 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17767 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017768 if ((flags & (SP_END | SP_SUBPAT)) != 0
17769 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017770 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017771 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017772 goto theend;
17773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017775 /* Using 'r' implies 'W', otherwise it doesn't work. */
17776 if (flags & SP_REPEAT)
17777 p_ws = FALSE;
17778
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017779 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017780 if (argvars[3].v_type == VAR_UNKNOWN
17781 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 skip = (char_u *)"";
17783 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017784 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017786 if (argvars[5].v_type != VAR_UNKNOWN)
17787 {
17788 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17789 if (lnum_stop < 0)
17790 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017791#ifdef FEAT_RELTIME
17792 if (argvars[6].v_type != VAR_UNKNOWN)
17793 {
17794 time_limit = get_tv_number_chk(&argvars[6], NULL);
17795 if (time_limit < 0)
17796 goto theend;
17797 }
17798#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017799 }
17800 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017801 if (skip == NULL)
17802 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017804 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017805 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017806
17807theend:
17808 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017809
17810 return retval;
17811}
17812
17813/*
17814 * "searchpair()" function
17815 */
17816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017817f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017818{
17819 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17820}
17821
17822/*
17823 * "searchpairpos()" function
17824 */
17825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017826f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017827{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017828 pos_T match_pos;
17829 int lnum = 0;
17830 int col = 0;
17831
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017832 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017833 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017834
17835 if (searchpair_cmn(argvars, &match_pos) > 0)
17836 {
17837 lnum = match_pos.lnum;
17838 col = match_pos.col;
17839 }
17840
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017841 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17842 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017843}
17844
17845/*
17846 * Search for a start/middle/end thing.
17847 * Used by searchpair(), see its documentation for the details.
17848 * Returns 0 or -1 for no match,
17849 */
17850 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017851do_searchpair(
17852 char_u *spat, /* start pattern */
17853 char_u *mpat, /* middle pattern */
17854 char_u *epat, /* end pattern */
17855 int dir, /* BACKWARD or FORWARD */
17856 char_u *skip, /* skip expression */
17857 int flags, /* SP_SETPCMARK and other SP_ values */
17858 pos_T *match_pos,
17859 linenr_T lnum_stop, /* stop at this line if not zero */
17860 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017861{
17862 char_u *save_cpo;
17863 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17864 long retval = 0;
17865 pos_T pos;
17866 pos_T firstpos;
17867 pos_T foundpos;
17868 pos_T save_cursor;
17869 pos_T save_pos;
17870 int n;
17871 int r;
17872 int nest = 1;
17873 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017874 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017875 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017876
17877 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17878 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017879 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017880
Bram Moolenaar76929292008-01-06 19:07:36 +000017881#ifdef FEAT_RELTIME
17882 /* Set the time limit, if there is one. */
17883 profile_setlimit(time_limit, &tm);
17884#endif
17885
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017886 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17887 * start/middle/end (pat3, for the top pair). */
17888 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17889 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17890 if (pat2 == NULL || pat3 == NULL)
17891 goto theend;
17892 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17893 if (*mpat == NUL)
17894 STRCPY(pat3, pat2);
17895 else
17896 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17897 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017898 if (flags & SP_START)
17899 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017900
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 save_cursor = curwin->w_cursor;
17902 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017903 clearpos(&firstpos);
17904 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905 pat = pat3;
17906 for (;;)
17907 {
17908 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017909 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17911 /* didn't find it or found the first match again: FAIL */
17912 break;
17913
17914 if (firstpos.lnum == 0)
17915 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017916 if (equalpos(pos, foundpos))
17917 {
17918 /* Found the same position again. Can happen with a pattern that
17919 * has "\zs" at the end and searching backwards. Advance one
17920 * character and try again. */
17921 if (dir == BACKWARD)
17922 decl(&pos);
17923 else
17924 incl(&pos);
17925 }
17926 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017928 /* clear the start flag to avoid getting stuck here */
17929 options &= ~SEARCH_START;
17930
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 /* If the skip pattern matches, ignore this match. */
17932 if (*skip != NUL)
17933 {
17934 save_pos = curwin->w_cursor;
17935 curwin->w_cursor = pos;
17936 r = eval_to_bool(skip, &err, NULL, FALSE);
17937 curwin->w_cursor = save_pos;
17938 if (err)
17939 {
17940 /* Evaluating {skip} caused an error, break here. */
17941 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017942 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017943 break;
17944 }
17945 if (r)
17946 continue;
17947 }
17948
17949 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17950 {
17951 /* Found end when searching backwards or start when searching
17952 * forward: nested pair. */
17953 ++nest;
17954 pat = pat2; /* nested, don't search for middle */
17955 }
17956 else
17957 {
17958 /* Found end when searching forward or start when searching
17959 * backward: end of (nested) pair; or found middle in outer pair. */
17960 if (--nest == 1)
17961 pat = pat3; /* outer level, search for middle */
17962 }
17963
17964 if (nest == 0)
17965 {
17966 /* Found the match: return matchcount or line number. */
17967 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017968 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017970 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017971 if (flags & SP_SETPCMARK)
17972 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 curwin->w_cursor = pos;
17974 if (!(flags & SP_REPEAT))
17975 break;
17976 nest = 1; /* search for next unmatched */
17977 }
17978 }
17979
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017980 if (match_pos != NULL)
17981 {
17982 /* Store the match cursor position */
17983 match_pos->lnum = curwin->w_cursor.lnum;
17984 match_pos->col = curwin->w_cursor.col + 1;
17985 }
17986
Bram Moolenaar071d4272004-06-13 20:20:40 +000017987 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017988 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989 curwin->w_cursor = save_cursor;
17990
17991theend:
17992 vim_free(pat2);
17993 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017994 if (p_cpo == empty_option)
17995 p_cpo = save_cpo;
17996 else
17997 /* Darn, evaluating the {skip} expression changed the value. */
17998 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017999
18000 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001}
18002
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018003/*
18004 * "searchpos()" function
18005 */
18006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018007f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018008{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018009 pos_T match_pos;
18010 int lnum = 0;
18011 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018012 int n;
18013 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018014
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018015 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018016 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018017
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018018 n = search_cmn(argvars, &match_pos, &flags);
18019 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018020 {
18021 lnum = match_pos.lnum;
18022 col = match_pos.col;
18023 }
18024
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018025 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18026 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018027 if (flags & SP_SUBPAT)
18028 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018029}
18030
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018032f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034#ifdef FEAT_CLIENTSERVER
18035 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018036 char_u *server = get_tv_string_chk(&argvars[0]);
18037 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038
Bram Moolenaar0d660222005-01-07 21:51:51 +000018039 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018040 if (server == NULL || reply == NULL)
18041 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042 if (check_restricted() || check_secure())
18043 return;
18044# ifdef FEAT_X11
18045 if (check_connection() == FAIL)
18046 return;
18047# endif
18048
18049 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018051 EMSG(_("E258: Unable to send to client"));
18052 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018054 rettv->vval.v_number = 0;
18055#else
18056 rettv->vval.v_number = -1;
18057#endif
18058}
18059
Bram Moolenaar0d660222005-01-07 21:51:51 +000018060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018061f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018062{
18063 char_u *r = NULL;
18064
18065#ifdef FEAT_CLIENTSERVER
18066# ifdef WIN32
18067 r = serverGetVimNames();
18068# else
18069 make_connection();
18070 if (X_DISPLAY != NULL)
18071 r = serverGetVimNames(X_DISPLAY);
18072# endif
18073#endif
18074 rettv->v_type = VAR_STRING;
18075 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076}
18077
18078/*
18079 * "setbufvar()" function
18080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018082f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018083{
18084 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018085 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018086 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018087 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 char_u nbuf[NUMBUFLEN];
18089
18090 if (check_restricted() || check_secure())
18091 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018092 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18093 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018094 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 varp = &argvars[2];
18096
18097 if (buf != NULL && varname != NULL && varp != NULL)
18098 {
18099 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101
18102 if (*varname == '&')
18103 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018104 long numval;
18105 char_u *strval;
18106 int error = FALSE;
18107
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018109 numval = get_tv_number_chk(varp, &error);
18110 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018111 if (!error && strval != NULL)
18112 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 }
18114 else
18115 {
18116 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18117 if (bufvarname != NULL)
18118 {
18119 STRCPY(bufvarname, "b:");
18120 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018121 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018122 vim_free(bufvarname);
18123 }
18124 }
18125
18126 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018127 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129}
18130
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018132f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018133{
18134 dict_T *d;
18135 dictitem_T *di;
18136 char_u *csearch;
18137
18138 if (argvars[0].v_type != VAR_DICT)
18139 {
18140 EMSG(_(e_dictreq));
18141 return;
18142 }
18143
18144 if ((d = argvars[0].vval.v_dict) != NULL)
18145 {
18146 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18147 if (csearch != NULL)
18148 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018149#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018150 if (enc_utf8)
18151 {
18152 int pcc[MAX_MCO];
18153 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018154
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018155 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18156 }
18157 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018158#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018159 set_last_csearch(PTR2CHAR(csearch),
18160 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018161 }
18162
18163 di = dict_find(d, (char_u *)"forward", -1);
18164 if (di != NULL)
18165 set_csearch_direction(get_tv_number(&di->di_tv)
18166 ? FORWARD : BACKWARD);
18167
18168 di = dict_find(d, (char_u *)"until", -1);
18169 if (di != NULL)
18170 set_csearch_until(!!get_tv_number(&di->di_tv));
18171 }
18172}
18173
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174/*
18175 * "setcmdpos()" function
18176 */
18177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018178f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018180 int pos = (int)get_tv_number(&argvars[0]) - 1;
18181
18182 if (pos >= 0)
18183 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184}
18185
18186/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018187 * "setfperm({fname}, {mode})" function
18188 */
18189 static void
18190f_setfperm(typval_T *argvars, typval_T *rettv)
18191{
18192 char_u *fname;
18193 char_u modebuf[NUMBUFLEN];
18194 char_u *mode_str;
18195 int i;
18196 int mask;
18197 int mode = 0;
18198
18199 rettv->vval.v_number = 0;
18200 fname = get_tv_string_chk(&argvars[0]);
18201 if (fname == NULL)
18202 return;
18203 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18204 if (mode_str == NULL)
18205 return;
18206 if (STRLEN(mode_str) != 9)
18207 {
18208 EMSG2(_(e_invarg2), mode_str);
18209 return;
18210 }
18211
18212 mask = 1;
18213 for (i = 8; i >= 0; --i)
18214 {
18215 if (mode_str[i] != '-')
18216 mode |= mask;
18217 mask = mask << 1;
18218 }
18219 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18220}
18221
18222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 * "setline()" function
18224 */
18225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018226f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227{
18228 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018229 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018230 list_T *l = NULL;
18231 listitem_T *li = NULL;
18232 long added = 0;
18233 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018235 lnum = get_tv_lnum(&argvars[0]);
18236 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 l = argvars[1].vval.v_list;
18239 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018241 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018242 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018243
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018244 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018245 for (;;)
18246 {
18247 if (l != NULL)
18248 {
18249 /* list argument, get next string */
18250 if (li == NULL)
18251 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018252 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018253 li = li->li_next;
18254 }
18255
18256 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018257 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018258 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018259
18260 /* When coming here from Insert mode, sync undo, so that this can be
18261 * undone separately from what was previously inserted. */
18262 if (u_sync_once == 2)
18263 {
18264 u_sync_once = 1; /* notify that u_sync() was called */
18265 u_sync(TRUE);
18266 }
18267
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018268 if (lnum <= curbuf->b_ml.ml_line_count)
18269 {
18270 /* existing line, replace it */
18271 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18272 {
18273 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018274 if (lnum == curwin->w_cursor.lnum)
18275 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018276 rettv->vval.v_number = 0; /* OK */
18277 }
18278 }
18279 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18280 {
18281 /* lnum is one past the last line, append the line */
18282 ++added;
18283 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18284 rettv->vval.v_number = 0; /* OK */
18285 }
18286
18287 if (l == NULL) /* only one string argument */
18288 break;
18289 ++lnum;
18290 }
18291
18292 if (added > 0)
18293 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018294}
18295
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018296static 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 +000018297
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018299 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018300 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018302set_qf_ll_list(
18303 win_T *wp UNUSED,
18304 typval_T *list_arg UNUSED,
18305 typval_T *action_arg UNUSED,
18306 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018307{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018308#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018309 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018310 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018311 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018312#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018313
Bram Moolenaar2641f772005-03-25 21:58:17 +000018314 rettv->vval.v_number = -1;
18315
18316#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018317 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018318 EMSG(_(e_listreq));
18319 else
18320 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018321 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018322
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018323 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018324 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018325 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018326 if (act == NULL)
18327 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018328 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018329 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018330 else
18331 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018332 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018333 else if (action_arg->v_type == VAR_UNKNOWN)
18334 action = ' ';
18335 else
18336 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018337
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018338 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018339 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018340 rettv->vval.v_number = 0;
18341 }
18342#endif
18343}
18344
18345/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018346 * "setloclist()" function
18347 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018349f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018350{
18351 win_T *win;
18352
18353 rettv->vval.v_number = -1;
18354
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018355 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018356 if (win != NULL)
18357 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18358}
18359
18360/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018361 * "setmatches()" function
18362 */
18363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018364f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018365{
18366#ifdef FEAT_SEARCH_EXTRA
18367 list_T *l;
18368 listitem_T *li;
18369 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018370 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018371
18372 rettv->vval.v_number = -1;
18373 if (argvars[0].v_type != VAR_LIST)
18374 {
18375 EMSG(_(e_listreq));
18376 return;
18377 }
18378 if ((l = argvars[0].vval.v_list) != NULL)
18379 {
18380
18381 /* To some extent make sure that we are dealing with a list from
18382 * "getmatches()". */
18383 li = l->lv_first;
18384 while (li != NULL)
18385 {
18386 if (li->li_tv.v_type != VAR_DICT
18387 || (d = li->li_tv.vval.v_dict) == NULL)
18388 {
18389 EMSG(_(e_invarg));
18390 return;
18391 }
18392 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018393 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18394 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018395 && dict_find(d, (char_u *)"priority", -1) != NULL
18396 && dict_find(d, (char_u *)"id", -1) != NULL))
18397 {
18398 EMSG(_(e_invarg));
18399 return;
18400 }
18401 li = li->li_next;
18402 }
18403
18404 clear_matches(curwin);
18405 li = l->lv_first;
18406 while (li != NULL)
18407 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018408 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018409 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018410 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018411 char_u *group;
18412 int priority;
18413 int id;
18414 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018415
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018416 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018417 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18418 {
18419 if (s == NULL)
18420 {
18421 s = list_alloc();
18422 if (s == NULL)
18423 return;
18424 }
18425
18426 /* match from matchaddpos() */
18427 for (i = 1; i < 9; i++)
18428 {
18429 sprintf((char *)buf, (char *)"pos%d", i);
18430 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18431 {
18432 if (di->di_tv.v_type != VAR_LIST)
18433 return;
18434
18435 list_append_tv(s, &di->di_tv);
18436 s->lv_refcount++;
18437 }
18438 else
18439 break;
18440 }
18441 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018442
18443 group = get_dict_string(d, (char_u *)"group", FALSE);
18444 priority = (int)get_dict_number(d, (char_u *)"priority");
18445 id = (int)get_dict_number(d, (char_u *)"id");
18446 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18447 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18448 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018449 if (i == 0)
18450 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018451 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018452 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018453 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018454 }
18455 else
18456 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018457 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018458 list_unref(s);
18459 s = NULL;
18460 }
18461
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018462 li = li->li_next;
18463 }
18464 rettv->vval.v_number = 0;
18465 }
18466#endif
18467}
18468
18469/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018470 * "setpos()" function
18471 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018473f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018474{
18475 pos_T pos;
18476 int fnum;
18477 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018478 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018479
Bram Moolenaar08250432008-02-13 11:42:46 +000018480 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018481 name = get_tv_string_chk(argvars);
18482 if (name != NULL)
18483 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018484 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018485 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018486 if (--pos.col < 0)
18487 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018488 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018489 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018490 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018491 if (fnum == curbuf->b_fnum)
18492 {
18493 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018494 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018495 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018496 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018497 curwin->w_set_curswant = FALSE;
18498 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018499 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018500 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018501 }
18502 else
18503 EMSG(_(e_invarg));
18504 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018505 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18506 {
18507 /* set mark */
18508 if (setmark_pos(name[1], &pos, fnum) == OK)
18509 rettv->vval.v_number = 0;
18510 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018511 else
18512 EMSG(_(e_invarg));
18513 }
18514 }
18515}
18516
18517/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018518 * "setqflist()" function
18519 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018521f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018522{
18523 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18524}
18525
18526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527 * "setreg()" function
18528 */
18529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018530f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018531{
18532 int regname;
18533 char_u *strregname;
18534 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018535 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 int append;
18537 char_u yank_type;
18538 long block_len;
18539
18540 block_len = -1;
18541 yank_type = MAUTO;
18542 append = FALSE;
18543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018544 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018545 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018547 if (strregname == NULL)
18548 return; /* type error; errmsg already given */
18549 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 if (regname == 0 || regname == '@')
18551 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018552
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018553 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018555 stropt = get_tv_string_chk(&argvars[2]);
18556 if (stropt == NULL)
18557 return; /* type error */
18558 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 switch (*stropt)
18560 {
18561 case 'a': case 'A': /* append */
18562 append = TRUE;
18563 break;
18564 case 'v': case 'c': /* character-wise selection */
18565 yank_type = MCHAR;
18566 break;
18567 case 'V': case 'l': /* line-wise selection */
18568 yank_type = MLINE;
18569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 case 'b': case Ctrl_V: /* block-wise selection */
18571 yank_type = MBLOCK;
18572 if (VIM_ISDIGIT(stropt[1]))
18573 {
18574 ++stropt;
18575 block_len = getdigits(&stropt) - 1;
18576 --stropt;
18577 }
18578 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 }
18580 }
18581
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018582 if (argvars[1].v_type == VAR_LIST)
18583 {
18584 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018585 char_u **allocval;
18586 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018587 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018588 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018589 int len = argvars[1].vval.v_list->lv_len;
18590 listitem_T *li;
18591
Bram Moolenaar7d647822014-04-05 21:28:56 +020018592 /* First half: use for pointers to result lines; second half: use for
18593 * pointers to allocated copies. */
18594 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018595 if (lstval == NULL)
18596 return;
18597 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018598 allocval = lstval + len + 2;
18599 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018600
18601 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18602 li = li->li_next)
18603 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018604 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018605 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018606 goto free_lstval;
18607 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018608 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018609 /* Need to make a copy, next get_tv_string_buf_chk() will
18610 * overwrite the string. */
18611 strval = vim_strsave(buf);
18612 if (strval == NULL)
18613 goto free_lstval;
18614 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018615 }
18616 *curval++ = strval;
18617 }
18618 *curval++ = NULL;
18619
18620 write_reg_contents_lst(regname, lstval, -1,
18621 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018622free_lstval:
18623 while (curallocval > allocval)
18624 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018625 vim_free(lstval);
18626 }
18627 else
18628 {
18629 strval = get_tv_string_chk(&argvars[1]);
18630 if (strval == NULL)
18631 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018632 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018633 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636}
18637
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018638/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018639 * "settabvar()" function
18640 */
18641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018642f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018643{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018644#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018645 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018646 tabpage_T *tp;
18647#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018648 char_u *varname, *tabvarname;
18649 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018650
18651 rettv->vval.v_number = 0;
18652
18653 if (check_restricted() || check_secure())
18654 return;
18655
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018656#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018657 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018658#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018659 varname = get_tv_string_chk(&argvars[1]);
18660 varp = &argvars[2];
18661
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018662 if (varname != NULL && varp != NULL
18663#ifdef FEAT_WINDOWS
18664 && tp != NULL
18665#endif
18666 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018667 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018668#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018669 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018670 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018671#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018672
18673 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18674 if (tabvarname != NULL)
18675 {
18676 STRCPY(tabvarname, "t:");
18677 STRCPY(tabvarname + 2, varname);
18678 set_var(tabvarname, varp, TRUE);
18679 vim_free(tabvarname);
18680 }
18681
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018682#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018683 /* Restore current tabpage */
18684 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018685 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018686#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018687 }
18688}
18689
18690/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018691 * "settabwinvar()" function
18692 */
18693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018694f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018695{
18696 setwinvar(argvars, rettv, 1);
18697}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
18699/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018700 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018703f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018705 setwinvar(argvars, rettv, 0);
18706}
18707
18708/*
18709 * "setwinvar()" and "settabwinvar()" functions
18710 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018711
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018713setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018714{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 win_T *win;
18716#ifdef FEAT_WINDOWS
18717 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018718 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018719 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720#endif
18721 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018722 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018724 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725
18726 if (check_restricted() || check_secure())
18727 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018728
18729#ifdef FEAT_WINDOWS
18730 if (off == 1)
18731 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18732 else
18733 tp = curtab;
18734#endif
18735 win = find_win_by_nr(&argvars[off], tp);
18736 varname = get_tv_string_chk(&argvars[off + 1]);
18737 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738
18739 if (win != NULL && varname != NULL && varp != NULL)
18740 {
18741#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018742 need_switch_win = !(tp == curtab && win == curwin);
18743 if (!need_switch_win
18744 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018747 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018749 long numval;
18750 char_u *strval;
18751 int error = FALSE;
18752
18753 ++varname;
18754 numval = get_tv_number_chk(varp, &error);
18755 strval = get_tv_string_buf_chk(varp, nbuf);
18756 if (!error && strval != NULL)
18757 set_option_value(varname, numval, strval, OPT_LOCAL);
18758 }
18759 else
18760 {
18761 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18762 if (winvarname != NULL)
18763 {
18764 STRCPY(winvarname, "w:");
18765 STRCPY(winvarname + 2, varname);
18766 set_var(winvarname, varp, TRUE);
18767 vim_free(winvarname);
18768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 }
18770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018772 if (need_switch_win)
18773 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774#endif
18775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776}
18777
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018778#ifdef FEAT_CRYPT
18779/*
18780 * "sha256({string})" function
18781 */
18782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018783f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018784{
18785 char_u *p;
18786
18787 p = get_tv_string(&argvars[0]);
18788 rettv->vval.v_string = vim_strsave(
18789 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18790 rettv->v_type = VAR_STRING;
18791}
18792#endif /* FEAT_CRYPT */
18793
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018795 * "shellescape({string})" function
18796 */
18797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018798f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018799{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018800 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018801 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018802 rettv->v_type = VAR_STRING;
18803}
18804
18805/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018806 * shiftwidth() function
18807 */
18808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018809f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018810{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018811 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018812}
18813
18814/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018815 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816 */
18817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018818f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018820 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821
Bram Moolenaar0d660222005-01-07 21:51:51 +000018822 p = get_tv_string(&argvars[0]);
18823 rettv->vval.v_string = vim_strsave(p);
18824 simplify_filename(rettv->vval.v_string); /* simplify in place */
18825 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826}
18827
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018828#ifdef FEAT_FLOAT
18829/*
18830 * "sin()" function
18831 */
18832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018833f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018834{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018835 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018836
18837 rettv->v_type = VAR_FLOAT;
18838 if (get_float_arg(argvars, &f) == OK)
18839 rettv->vval.v_float = sin(f);
18840 else
18841 rettv->vval.v_float = 0.0;
18842}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018843
18844/*
18845 * "sinh()" function
18846 */
18847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018848f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018849{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018850 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018851
18852 rettv->v_type = VAR_FLOAT;
18853 if (get_float_arg(argvars, &f) == OK)
18854 rettv->vval.v_float = sinh(f);
18855 else
18856 rettv->vval.v_float = 0.0;
18857}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018858#endif
18859
Bram Moolenaar0d660222005-01-07 21:51:51 +000018860static int
18861#ifdef __BORLANDC__
18862 _RTLENTRYF
18863#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018864 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865static int
18866#ifdef __BORLANDC__
18867 _RTLENTRYF
18868#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018869 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018870
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018871/* struct used in the array that's given to qsort() */
18872typedef struct
18873{
18874 listitem_T *item;
18875 int idx;
18876} sortItem_T;
18877
Bram Moolenaar0b962472016-02-22 22:51:33 +010018878/* struct storing information about current sort */
18879typedef struct
18880{
18881 int item_compare_ic;
18882 int item_compare_numeric;
18883 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018884#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018885 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018886#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018887 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018888 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018889 dict_T *item_compare_selfdict;
18890 int item_compare_func_err;
18891 int item_compare_keep_zero;
18892} sortinfo_T;
18893static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018894static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018895#define ITEM_COMPARE_FAIL 999
18896
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018898 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018900 static int
18901#ifdef __BORLANDC__
18902_RTLENTRYF
18903#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018904item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018906 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018907 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018908 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018909 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018910 int res;
18911 char_u numbuf1[NUMBUFLEN];
18912 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018913
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018914 si1 = (sortItem_T *)s1;
18915 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018916 tv1 = &si1->item->li_tv;
18917 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018918
Bram Moolenaar0b962472016-02-22 22:51:33 +010018919 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018920 {
18921 long v1 = get_tv_number(tv1);
18922 long v2 = get_tv_number(tv2);
18923
18924 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18925 }
18926
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018927#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018928 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018929 {
18930 float_T v1 = get_tv_float(tv1);
18931 float_T v2 = get_tv_float(tv2);
18932
18933 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18934 }
18935#endif
18936
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018937 /* tv2string() puts quotes around a string and allocates memory. Don't do
18938 * that for string variables. Use a single quote when comparing with a
18939 * non-string to do what the docs promise. */
18940 if (tv1->v_type == VAR_STRING)
18941 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018942 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018943 p1 = (char_u *)"'";
18944 else
18945 p1 = tv1->vval.v_string;
18946 }
18947 else
18948 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18949 if (tv2->v_type == VAR_STRING)
18950 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018951 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018952 p2 = (char_u *)"'";
18953 else
18954 p2 = tv2->vval.v_string;
18955 }
18956 else
18957 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018958 if (p1 == NULL)
18959 p1 = (char_u *)"";
18960 if (p2 == NULL)
18961 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018962 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018963 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018964 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018965 res = STRICMP(p1, p2);
18966 else
18967 res = STRCMP(p1, p2);
18968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018970 {
18971 double n1, n2;
18972 n1 = strtod((char *)p1, (char **)&p1);
18973 n2 = strtod((char *)p2, (char **)&p2);
18974 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18975 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018976
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018977 /* When the result would be zero, compare the item indexes. Makes the
18978 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018979 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018980 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018981
Bram Moolenaar0d660222005-01-07 21:51:51 +000018982 vim_free(tofree1);
18983 vim_free(tofree2);
18984 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985}
18986
18987 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018988#ifdef __BORLANDC__
18989_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018990#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018991item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018993 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018995 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018996 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018998 char_u *func_name;
18999 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019001 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019002 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019003 return 0;
19004
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019005 si1 = (sortItem_T *)s1;
19006 si2 = (sortItem_T *)s2;
19007
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019008 if (partial == NULL)
19009 func_name = sortinfo->item_compare_func;
19010 else
19011 func_name = partial->pt_name;
19012
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019013 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019014 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019015 copy_tv(&si1->item->li_tv, &argv[0]);
19016 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017
19018 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019019 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019020 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019021 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019022 clear_tv(&argv[0]);
19023 clear_tv(&argv[1]);
19024
19025 if (res == FAIL)
19026 res = ITEM_COMPARE_FAIL;
19027 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019028 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19029 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019030 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019032
19033 /* When the result would be zero, compare the pointers themselves. Makes
19034 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019035 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019036 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019037
Bram Moolenaar0d660222005-01-07 21:51:51 +000019038 return res;
19039}
19040
19041/*
19042 * "sort({list})" function
19043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019045do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019046{
Bram Moolenaar33570922005-01-25 22:26:29 +000019047 list_T *l;
19048 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019049 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019050 sortinfo_T *old_sortinfo;
19051 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019052 long len;
19053 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019054
Bram Moolenaar0b962472016-02-22 22:51:33 +010019055 /* Pointer to current info struct used in compare function. Save and
19056 * restore the current one for nested calls. */
19057 old_sortinfo = sortinfo;
19058 sortinfo = &info;
19059
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019061 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062 else
19063 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019065 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019066 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19067 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019069 rettv->vval.v_list = l;
19070 rettv->v_type = VAR_LIST;
19071 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019072
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 len = list_len(l);
19074 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019075 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076
Bram Moolenaar0b962472016-02-22 22:51:33 +010019077 info.item_compare_ic = FALSE;
19078 info.item_compare_numeric = FALSE;
19079 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019080#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019081 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019082#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019084 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019085 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 if (argvars[1].v_type != VAR_UNKNOWN)
19087 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019088 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019090 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019091 else if (argvars[1].v_type == VAR_PARTIAL)
19092 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019093 else
19094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019095 int error = FALSE;
19096
19097 i = get_tv_number_chk(&argvars[1], &error);
19098 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019099 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019100 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019101 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019102 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019103 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019104 else if (i != 0)
19105 {
19106 EMSG(_(e_invarg));
19107 goto theend;
19108 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019110 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019111 if (*info.item_compare_func == NUL)
19112 {
19113 /* empty string means default sort */
19114 info.item_compare_func = NULL;
19115 }
19116 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019117 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 info.item_compare_func = NULL;
19119 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019120 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019121 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019122 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019123 info.item_compare_func = NULL;
19124 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019125 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019126#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019128 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019129 info.item_compare_func = NULL;
19130 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019131 }
19132#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019133 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019134 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019135 info.item_compare_func = NULL;
19136 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019137 }
19138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019139 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019140
19141 if (argvars[2].v_type != VAR_UNKNOWN)
19142 {
19143 /* optional third argument: {dict} */
19144 if (argvars[2].v_type != VAR_DICT)
19145 {
19146 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019147 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019148 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152
Bram Moolenaar0d660222005-01-07 21:51:51 +000019153 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019154 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019156 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157
Bram Moolenaar327aa022014-03-25 18:24:23 +010019158 i = 0;
19159 if (sort)
19160 {
19161 /* sort(): ptrs will be the list to sort */
19162 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019163 {
19164 ptrs[i].item = li;
19165 ptrs[i].idx = i;
19166 ++i;
19167 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019168
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 info.item_compare_func_err = FALSE;
19170 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019171 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019172 if ((info.item_compare_func != NULL
19173 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019174 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019175 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019176 EMSG(_("E702: Sort compare function failed"));
19177 else
19178 {
19179 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019180 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019182 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019183 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019184
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019186 {
19187 /* Clear the List and append the items in sorted order. */
19188 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19189 l->lv_len = 0;
19190 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019191 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019192 }
19193 }
19194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019195 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019196 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019197 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019198
19199 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019200 info.item_compare_func_err = FALSE;
19201 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019202 item_compare_func_ptr = info.item_compare_func != NULL
19203 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019204 ? item_compare2 : item_compare;
19205
19206 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19207 li = li->li_next)
19208 {
19209 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19210 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019211 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019213 {
19214 EMSG(_("E882: Uniq compare function failed"));
19215 break;
19216 }
19217 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019218
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019220 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019221 while (--i >= 0)
19222 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019223 li = ptrs[i].item->li_next;
19224 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019225 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019226 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019227 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019228 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019229 list_fix_watch(l, li);
19230 listitem_free(li);
19231 l->lv_len--;
19232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019233 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 }
19235
19236 vim_free(ptrs);
19237 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019238theend:
19239 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240}
19241
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019242/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019243 * "sort({list})" function
19244 */
19245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019246f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019247{
19248 do_sort_uniq(argvars, rettv, TRUE);
19249}
19250
19251/*
19252 * "uniq({list})" function
19253 */
19254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019255f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019256{
19257 do_sort_uniq(argvars, rettv, FALSE);
19258}
19259
19260/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019261 * "soundfold({word})" function
19262 */
19263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019264f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019265{
19266 char_u *s;
19267
19268 rettv->v_type = VAR_STRING;
19269 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019270#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019271 rettv->vval.v_string = eval_soundfold(s);
19272#else
19273 rettv->vval.v_string = vim_strsave(s);
19274#endif
19275}
19276
19277/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019278 * "spellbadword()" function
19279 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019281f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019282{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019283 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019284 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019285 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019286
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019287 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019288 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019289
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019290#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019291 if (argvars[0].v_type == VAR_UNKNOWN)
19292 {
19293 /* Find the start and length of the badly spelled word. */
19294 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19295 if (len != 0)
19296 word = ml_get_cursor();
19297 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019298 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019299 {
19300 char_u *str = get_tv_string_chk(&argvars[0]);
19301 int capcol = -1;
19302
19303 if (str != NULL)
19304 {
19305 /* Check the argument for spelling. */
19306 while (*str != NUL)
19307 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019308 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019309 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019310 {
19311 word = str;
19312 break;
19313 }
19314 str += len;
19315 }
19316 }
19317 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019318#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019320 list_append_string(rettv->vval.v_list, word, len);
19321 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019322 attr == HLF_SPB ? "bad" :
19323 attr == HLF_SPR ? "rare" :
19324 attr == HLF_SPL ? "local" :
19325 attr == HLF_SPC ? "caps" :
19326 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327}
19328
19329/*
19330 * "spellsuggest()" function
19331 */
19332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019333f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019334{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019335#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019336 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019337 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019338 int maxcount;
19339 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019340 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019341 listitem_T *li;
19342 int need_capital = FALSE;
19343#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019345 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019346 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019347
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019348#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019349 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019350 {
19351 str = get_tv_string(&argvars[0]);
19352 if (argvars[1].v_type != VAR_UNKNOWN)
19353 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019354 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019355 if (maxcount <= 0)
19356 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019357 if (argvars[2].v_type != VAR_UNKNOWN)
19358 {
19359 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19360 if (typeerr)
19361 return;
19362 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019363 }
19364 else
19365 maxcount = 25;
19366
Bram Moolenaar4770d092006-01-12 23:22:24 +000019367 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019368
19369 for (i = 0; i < ga.ga_len; ++i)
19370 {
19371 str = ((char_u **)ga.ga_data)[i];
19372
19373 li = listitem_alloc();
19374 if (li == NULL)
19375 vim_free(str);
19376 else
19377 {
19378 li->li_tv.v_type = VAR_STRING;
19379 li->li_tv.v_lock = 0;
19380 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019381 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019382 }
19383 }
19384 ga_clear(&ga);
19385 }
19386#endif
19387}
19388
Bram Moolenaar0d660222005-01-07 21:51:51 +000019389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019390f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019391{
19392 char_u *str;
19393 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019394 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395 regmatch_T regmatch;
19396 char_u patbuf[NUMBUFLEN];
19397 char_u *save_cpo;
19398 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019400 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019401 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402
19403 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19404 save_cpo = p_cpo;
19405 p_cpo = (char_u *)"";
19406
19407 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019408 if (argvars[1].v_type != VAR_UNKNOWN)
19409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019410 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19411 if (pat == NULL)
19412 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019414 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019415 }
19416 if (pat == NULL || *pat == NUL)
19417 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019419 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019421 if (typeerr)
19422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19425 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019428 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019430 if (*str == NUL)
19431 match = FALSE; /* empty item at the end */
19432 else
19433 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434 if (match)
19435 end = regmatch.startp[0];
19436 else
19437 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019438 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19439 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019441 if (list_append_string(rettv->vval.v_list, str,
19442 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019444 }
19445 if (!match)
19446 break;
19447 /* Advance to just after the match. */
19448 if (regmatch.endp[0] > str)
19449 col = 0;
19450 else
19451 {
19452 /* Don't get stuck at the same match. */
19453#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019454 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019455#else
19456 col = 1;
19457#endif
19458 }
19459 str = regmatch.endp[0];
19460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461
Bram Moolenaar473de612013-06-08 18:19:48 +020019462 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466}
19467
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019468#ifdef FEAT_FLOAT
19469/*
19470 * "sqrt()" function
19471 */
19472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019474{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019475 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019476
19477 rettv->v_type = VAR_FLOAT;
19478 if (get_float_arg(argvars, &f) == OK)
19479 rettv->vval.v_float = sqrt(f);
19480 else
19481 rettv->vval.v_float = 0.0;
19482}
19483
19484/*
19485 * "str2float()" function
19486 */
19487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019488f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019489{
19490 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19491
19492 if (*p == '+')
19493 p = skipwhite(p + 1);
19494 (void)string2float(p, &rettv->vval.v_float);
19495 rettv->v_type = VAR_FLOAT;
19496}
19497#endif
19498
Bram Moolenaar2c932302006-03-18 21:42:09 +000019499/*
19500 * "str2nr()" function
19501 */
19502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019503f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019504{
19505 int base = 10;
19506 char_u *p;
19507 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019508 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019509
19510 if (argvars[1].v_type != VAR_UNKNOWN)
19511 {
19512 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019513 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019514 {
19515 EMSG(_(e_invarg));
19516 return;
19517 }
19518 }
19519
19520 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019521 if (*p == '+')
19522 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019523 switch (base)
19524 {
19525 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19526 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19527 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19528 default: what = 0;
19529 }
19530 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019531 rettv->vval.v_number = n;
19532}
19533
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534#ifdef HAVE_STRFTIME
19535/*
19536 * "strftime({format}[, {time}])" function
19537 */
19538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019539f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540{
19541 char_u result_buf[256];
19542 struct tm *curtime;
19543 time_t seconds;
19544 char_u *p;
19545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019546 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019549 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 seconds = time(NULL);
19551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019552 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 curtime = localtime(&seconds);
19554 /* MSVC returns NULL for an invalid value of seconds. */
19555 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 else
19558 {
19559# ifdef FEAT_MBYTE
19560 vimconv_T conv;
19561 char_u *enc;
19562
19563 conv.vc_type = CONV_NONE;
19564 enc = enc_locale();
19565 convert_setup(&conv, p_enc, enc);
19566 if (conv.vc_type != CONV_NONE)
19567 p = string_convert(&conv, p, NULL);
19568# endif
19569 if (p != NULL)
19570 (void)strftime((char *)result_buf, sizeof(result_buf),
19571 (char *)p, curtime);
19572 else
19573 result_buf[0] = NUL;
19574
19575# ifdef FEAT_MBYTE
19576 if (conv.vc_type != CONV_NONE)
19577 vim_free(p);
19578 convert_setup(&conv, enc, p_enc);
19579 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 else
19582# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019583 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584
19585# ifdef FEAT_MBYTE
19586 /* Release conversion descriptors */
19587 convert_setup(&conv, NULL, NULL);
19588 vim_free(enc);
19589# endif
19590 }
19591}
19592#endif
19593
19594/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019595 * "strgetchar()" function
19596 */
19597 static void
19598f_strgetchar(typval_T *argvars, typval_T *rettv)
19599{
19600 char_u *str;
19601 int len;
19602 int error = FALSE;
19603 int charidx;
19604
19605 rettv->vval.v_number = -1;
19606 str = get_tv_string_chk(&argvars[0]);
19607 if (str == NULL)
19608 return;
19609 len = (int)STRLEN(str);
19610 charidx = get_tv_number_chk(&argvars[1], &error);
19611 if (error)
19612 return;
19613#ifdef FEAT_MBYTE
19614 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019615 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019616
19617 while (charidx >= 0 && byteidx < len)
19618 {
19619 if (charidx == 0)
19620 {
19621 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19622 break;
19623 }
19624 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019625 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019626 }
19627 }
19628#else
19629 if (charidx < len)
19630 rettv->vval.v_number = str[charidx];
19631#endif
19632}
19633
19634/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 * "stridx()" function
19636 */
19637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019638f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639{
19640 char_u buf[NUMBUFLEN];
19641 char_u *needle;
19642 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019643 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019645 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019647 needle = get_tv_string_chk(&argvars[1]);
19648 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019649 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019650 if (needle == NULL || haystack == NULL)
19651 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652
Bram Moolenaar33570922005-01-25 22:26:29 +000019653 if (argvars[2].v_type != VAR_UNKNOWN)
19654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019655 int error = FALSE;
19656
19657 start_idx = get_tv_number_chk(&argvars[2], &error);
19658 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019659 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019660 if (start_idx >= 0)
19661 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 }
19663
19664 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19665 if (pos != NULL)
19666 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019667}
19668
19669/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019670 * "string()" function
19671 */
19672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019673f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019674{
19675 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019676 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019678 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019679 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19680 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019681 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019682 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019683 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684}
19685
19686/*
19687 * "strlen()" function
19688 */
19689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019690f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019692 rettv->vval.v_number = (varnumber_T)(STRLEN(
19693 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694}
19695
19696/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019697 * "strchars()" function
19698 */
19699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019700f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019701{
19702 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019703 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019704#ifdef FEAT_MBYTE
19705 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019706 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019707#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019708
19709 if (argvars[1].v_type != VAR_UNKNOWN)
19710 skipcc = get_tv_number_chk(&argvars[1], NULL);
19711 if (skipcc < 0 || skipcc > 1)
19712 EMSG(_(e_invarg));
19713 else
19714 {
19715#ifdef FEAT_MBYTE
19716 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19717 while (*s != NUL)
19718 {
19719 func_mb_ptr2char_adv(&s);
19720 ++len;
19721 }
19722 rettv->vval.v_number = len;
19723#else
19724 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19725#endif
19726 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019727}
19728
19729/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019730 * "strdisplaywidth()" function
19731 */
19732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019733f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019734{
19735 char_u *s = get_tv_string(&argvars[0]);
19736 int col = 0;
19737
19738 if (argvars[1].v_type != VAR_UNKNOWN)
19739 col = get_tv_number(&argvars[1]);
19740
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019741 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019742}
19743
19744/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019745 * "strwidth()" function
19746 */
19747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019748f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019749{
19750 char_u *s = get_tv_string(&argvars[0]);
19751
19752 rettv->vval.v_number = (varnumber_T)(
19753#ifdef FEAT_MBYTE
19754 mb_string2cells(s, -1)
19755#else
19756 STRLEN(s)
19757#endif
19758 );
19759}
19760
19761/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019762 * "strcharpart()" function
19763 */
19764 static void
19765f_strcharpart(typval_T *argvars, typval_T *rettv)
19766{
19767#ifdef FEAT_MBYTE
19768 char_u *p;
19769 int nchar;
19770 int nbyte = 0;
19771 int charlen;
19772 int len = 0;
19773 int slen;
19774 int error = FALSE;
19775
19776 p = get_tv_string(&argvars[0]);
19777 slen = (int)STRLEN(p);
19778
19779 nchar = get_tv_number_chk(&argvars[1], &error);
19780 if (!error)
19781 {
19782 if (nchar > 0)
19783 while (nchar > 0 && nbyte < slen)
19784 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019785 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019786 --nchar;
19787 }
19788 else
19789 nbyte = nchar;
19790 if (argvars[2].v_type != VAR_UNKNOWN)
19791 {
19792 charlen = get_tv_number(&argvars[2]);
19793 while (charlen > 0 && nbyte + len < slen)
19794 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019795 int off = nbyte + len;
19796
19797 if (off < 0)
19798 len += 1;
19799 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019800 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019801 --charlen;
19802 }
19803 }
19804 else
19805 len = slen - nbyte; /* default: all bytes that are available. */
19806 }
19807
19808 /*
19809 * Only return the overlap between the specified part and the actual
19810 * string.
19811 */
19812 if (nbyte < 0)
19813 {
19814 len += nbyte;
19815 nbyte = 0;
19816 }
19817 else if (nbyte > slen)
19818 nbyte = slen;
19819 if (len < 0)
19820 len = 0;
19821 else if (nbyte + len > slen)
19822 len = slen - nbyte;
19823
19824 rettv->v_type = VAR_STRING;
19825 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19826#else
19827 f_strpart(argvars, rettv);
19828#endif
19829}
19830
19831/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019832 * "strpart()" function
19833 */
19834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019835f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836{
19837 char_u *p;
19838 int n;
19839 int len;
19840 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019841 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019843 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019844 slen = (int)STRLEN(p);
19845
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019846 n = get_tv_number_chk(&argvars[1], &error);
19847 if (error)
19848 len = 0;
19849 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019850 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 else
19852 len = slen - n; /* default len: all bytes that are available. */
19853
19854 /*
19855 * Only return the overlap between the specified part and the actual
19856 * string.
19857 */
19858 if (n < 0)
19859 {
19860 len += n;
19861 n = 0;
19862 }
19863 else if (n > slen)
19864 n = slen;
19865 if (len < 0)
19866 len = 0;
19867 else if (n + len > slen)
19868 len = slen - n;
19869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019870 rettv->v_type = VAR_STRING;
19871 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872}
19873
19874/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019875 * "strridx()" function
19876 */
19877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019878f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019879{
19880 char_u buf[NUMBUFLEN];
19881 char_u *needle;
19882 char_u *haystack;
19883 char_u *rest;
19884 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019885 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019887 needle = get_tv_string_chk(&argvars[1]);
19888 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019889
19890 rettv->vval.v_number = -1;
19891 if (needle == NULL || haystack == NULL)
19892 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019893
19894 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019895 if (argvars[2].v_type != VAR_UNKNOWN)
19896 {
19897 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019898 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019899 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019900 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019901 }
19902 else
19903 end_idx = haystack_len;
19904
Bram Moolenaar0d660222005-01-07 21:51:51 +000019905 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019906 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019907 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019908 lastmatch = haystack + end_idx;
19909 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019910 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019911 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019912 for (rest = haystack; *rest != '\0'; ++rest)
19913 {
19914 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019915 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019916 break;
19917 lastmatch = rest;
19918 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019919 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019920
19921 if (lastmatch == NULL)
19922 rettv->vval.v_number = -1;
19923 else
19924 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19925}
19926
19927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 * "strtrans()" function
19929 */
19930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019931f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019933 rettv->v_type = VAR_STRING;
19934 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935}
19936
19937/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019938 * "submatch()" function
19939 */
19940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019941f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019942{
Bram Moolenaar41571762014-04-02 19:00:58 +020019943 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019944 int no;
19945 int retList = 0;
19946
19947 no = (int)get_tv_number_chk(&argvars[0], &error);
19948 if (error)
19949 return;
19950 error = FALSE;
19951 if (argvars[1].v_type != VAR_UNKNOWN)
19952 retList = get_tv_number_chk(&argvars[1], &error);
19953 if (error)
19954 return;
19955
19956 if (retList == 0)
19957 {
19958 rettv->v_type = VAR_STRING;
19959 rettv->vval.v_string = reg_submatch(no);
19960 }
19961 else
19962 {
19963 rettv->v_type = VAR_LIST;
19964 rettv->vval.v_list = reg_submatch_list(no);
19965 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019966}
19967
19968/*
19969 * "substitute()" function
19970 */
19971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019972f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019973{
19974 char_u patbuf[NUMBUFLEN];
19975 char_u subbuf[NUMBUFLEN];
19976 char_u flagsbuf[NUMBUFLEN];
19977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019978 char_u *str = get_tv_string_chk(&argvars[0]);
19979 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19980 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19981 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19982
Bram Moolenaar0d660222005-01-07 21:51:51 +000019983 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019984 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19985 rettv->vval.v_string = NULL;
19986 else
19987 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019988}
19989
19990/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019991 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019994f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
19996 int id = 0;
19997#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019998 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019999 long col;
20000 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020001 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020003 lnum = get_tv_lnum(argvars); /* -1 on type error */
20004 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20005 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020007 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020008 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020009 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010#endif
20011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020012 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013}
20014
20015/*
20016 * "synIDattr(id, what [, mode])" function
20017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020019f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020020{
20021 char_u *p = NULL;
20022#ifdef FEAT_SYN_HL
20023 int id;
20024 char_u *what;
20025 char_u *mode;
20026 char_u modebuf[NUMBUFLEN];
20027 int modec;
20028
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020029 id = get_tv_number(&argvars[0]);
20030 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020031 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020033 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020035 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 modec = 0; /* replace invalid with current */
20037 }
20038 else
20039 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020040#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020041 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 modec = 'g';
20043 else
20044#endif
20045 if (t_colors > 1)
20046 modec = 'c';
20047 else
20048 modec = 't';
20049 }
20050
20051
20052 switch (TOLOWER_ASC(what[0]))
20053 {
20054 case 'b':
20055 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20056 p = highlight_color(id, what, modec);
20057 else /* bold */
20058 p = highlight_has_attr(id, HL_BOLD, modec);
20059 break;
20060
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020061 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 p = highlight_color(id, what, modec);
20063 break;
20064
20065 case 'i':
20066 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20067 p = highlight_has_attr(id, HL_INVERSE, modec);
20068 else /* italic */
20069 p = highlight_has_attr(id, HL_ITALIC, modec);
20070 break;
20071
20072 case 'n': /* name */
20073 p = get_highlight_name(NULL, id - 1);
20074 break;
20075
20076 case 'r': /* reverse */
20077 p = highlight_has_attr(id, HL_INVERSE, modec);
20078 break;
20079
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020080 case 's':
20081 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20082 p = highlight_color(id, what, modec);
20083 else /* standout */
20084 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 break;
20086
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020087 case 'u':
20088 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20089 /* underline */
20090 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20091 else
20092 /* undercurl */
20093 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 break;
20095 }
20096
20097 if (p != NULL)
20098 p = vim_strsave(p);
20099#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020100 rettv->v_type = VAR_STRING;
20101 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102}
20103
20104/*
20105 * "synIDtrans(id)" function
20106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020108f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109{
20110 int id;
20111
20112#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020113 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114
20115 if (id > 0)
20116 id = syn_get_final_id(id);
20117 else
20118#endif
20119 id = 0;
20120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020121 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122}
20123
20124/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020125 * "synconcealed(lnum, col)" function
20126 */
20127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020128f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020129{
20130#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20131 long lnum;
20132 long col;
20133 int syntax_flags = 0;
20134 int cchar;
20135 int matchid = 0;
20136 char_u str[NUMBUFLEN];
20137#endif
20138
20139 rettv->v_type = VAR_LIST;
20140 rettv->vval.v_list = NULL;
20141
20142#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20143 lnum = get_tv_lnum(argvars); /* -1 on type error */
20144 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20145
20146 vim_memset(str, NUL, sizeof(str));
20147
20148 if (rettv_list_alloc(rettv) != FAIL)
20149 {
20150 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20151 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20152 && curwin->w_p_cole > 0)
20153 {
20154 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20155 syntax_flags = get_syntax_info(&matchid);
20156
20157 /* get the conceal character */
20158 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20159 {
20160 cchar = syn_get_sub_char();
20161 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20162 cchar = lcs_conceal;
20163 if (cchar != NUL)
20164 {
20165# ifdef FEAT_MBYTE
20166 if (has_mbyte)
20167 (*mb_char2bytes)(cchar, str);
20168 else
20169# endif
20170 str[0] = cchar;
20171 }
20172 }
20173 }
20174
20175 list_append_number(rettv->vval.v_list,
20176 (syntax_flags & HL_CONCEAL) != 0);
20177 /* -1 to auto-determine strlen */
20178 list_append_string(rettv->vval.v_list, str, -1);
20179 list_append_number(rettv->vval.v_list, matchid);
20180 }
20181#endif
20182}
20183
20184/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020185 * "synstack(lnum, col)" function
20186 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020188f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020189{
20190#ifdef FEAT_SYN_HL
20191 long lnum;
20192 long col;
20193 int i;
20194 int id;
20195#endif
20196
20197 rettv->v_type = VAR_LIST;
20198 rettv->vval.v_list = NULL;
20199
20200#ifdef FEAT_SYN_HL
20201 lnum = get_tv_lnum(argvars); /* -1 on type error */
20202 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20203
20204 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020205 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020206 && rettv_list_alloc(rettv) != FAIL)
20207 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020208 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020209 for (i = 0; ; ++i)
20210 {
20211 id = syn_get_stack_item(i);
20212 if (id < 0)
20213 break;
20214 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20215 break;
20216 }
20217 }
20218#endif
20219}
20220
Bram Moolenaar071d4272004-06-13 20:20:40 +000020221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020222get_cmd_output_as_rettv(
20223 typval_T *argvars,
20224 typval_T *rettv,
20225 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020227 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020229 char_u *infile = NULL;
20230 char_u buf[NUMBUFLEN];
20231 int err = FALSE;
20232 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020233 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020234 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020236 rettv->v_type = VAR_STRING;
20237 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020238 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020239 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020240
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020241 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020242 {
20243 /*
20244 * Write the string to a temp file, to be used for input of the shell
20245 * command.
20246 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020247 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020248 {
20249 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020250 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020251 }
20252
20253 fd = mch_fopen((char *)infile, WRITEBIN);
20254 if (fd == NULL)
20255 {
20256 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020257 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020258 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020259 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020260 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020261 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20262 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020263 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020264 else
20265 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020266 size_t len;
20267
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020268 p = get_tv_string_buf_chk(&argvars[1], buf);
20269 if (p == NULL)
20270 {
20271 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020272 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020273 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020274 len = STRLEN(p);
20275 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020276 err = TRUE;
20277 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020278 if (fclose(fd) != 0)
20279 err = TRUE;
20280 if (err)
20281 {
20282 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020283 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020284 }
20285 }
20286
Bram Moolenaar52a72462014-08-29 15:53:52 +020020287 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20288 * echoes typeahead, that messes up the display. */
20289 if (!msg_silent)
20290 flags += SHELL_COOKED;
20291
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020292 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020294 int len;
20295 listitem_T *li;
20296 char_u *s = NULL;
20297 char_u *start;
20298 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020300
Bram Moolenaar52a72462014-08-29 15:53:52 +020020301 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020302 if (res == NULL)
20303 goto errret;
20304
20305 list = list_alloc();
20306 if (list == NULL)
20307 goto errret;
20308
20309 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020311 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020312 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020313 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020314 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020315
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020316 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020317 if (s == NULL)
20318 goto errret;
20319
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020320 for (p = s; start < end; ++p, ++start)
20321 *p = *start == NUL ? NL : *start;
20322 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020323
20324 li = listitem_alloc();
20325 if (li == NULL)
20326 {
20327 vim_free(s);
20328 goto errret;
20329 }
20330 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020331 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020332 li->li_tv.vval.v_string = s;
20333 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020335
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020336 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020337 rettv->v_type = VAR_LIST;
20338 rettv->vval.v_list = list;
20339 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020341 else
20342 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020343 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020344#ifdef USE_CR
20345 /* translate <CR> into <NL> */
20346 if (res != NULL)
20347 {
20348 char_u *s;
20349
20350 for (s = res; *s; ++s)
20351 {
20352 if (*s == CAR)
20353 *s = NL;
20354 }
20355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356#else
20357# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020358 /* translate <CR><NL> into <NL> */
20359 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020361 char_u *s, *d;
20362
20363 d = res;
20364 for (s = res; *s; ++s)
20365 {
20366 if (s[0] == CAR && s[1] == NL)
20367 ++s;
20368 *d++ = *s;
20369 }
20370 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020372# endif
20373#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020374 rettv->vval.v_string = res;
20375 res = NULL;
20376 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020377
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020378errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020379 if (infile != NULL)
20380 {
20381 mch_remove(infile);
20382 vim_free(infile);
20383 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020384 if (res != NULL)
20385 vim_free(res);
20386 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020387 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020388}
20389
20390/*
20391 * "system()" function
20392 */
20393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020394f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020395{
20396 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20397}
20398
20399/*
20400 * "systemlist()" function
20401 */
20402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020403f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020404{
20405 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406}
20407
20408/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020409 * "tabpagebuflist()" function
20410 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020412f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020413{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020414#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020415 tabpage_T *tp;
20416 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020417
20418 if (argvars[0].v_type == VAR_UNKNOWN)
20419 wp = firstwin;
20420 else
20421 {
20422 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20423 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020424 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020425 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020426 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020427 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020428 for (; wp != NULL; wp = wp->w_next)
20429 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020430 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020431 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020432 }
20433#endif
20434}
20435
20436
20437/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020438 * "tabpagenr()" function
20439 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020441f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020442{
20443 int nr = 1;
20444#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020445 char_u *arg;
20446
20447 if (argvars[0].v_type != VAR_UNKNOWN)
20448 {
20449 arg = get_tv_string_chk(&argvars[0]);
20450 nr = 0;
20451 if (arg != NULL)
20452 {
20453 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020454 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020455 else
20456 EMSG2(_(e_invexpr2), arg);
20457 }
20458 }
20459 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020460 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020461#endif
20462 rettv->vval.v_number = nr;
20463}
20464
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020465
20466#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020467static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020468
20469/*
20470 * Common code for tabpagewinnr() and winnr().
20471 */
20472 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020473get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020474{
20475 win_T *twin;
20476 int nr = 1;
20477 win_T *wp;
20478 char_u *arg;
20479
20480 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20481 if (argvar->v_type != VAR_UNKNOWN)
20482 {
20483 arg = get_tv_string_chk(argvar);
20484 if (arg == NULL)
20485 nr = 0; /* type error; errmsg already given */
20486 else if (STRCMP(arg, "$") == 0)
20487 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20488 else if (STRCMP(arg, "#") == 0)
20489 {
20490 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20491 if (twin == NULL)
20492 nr = 0;
20493 }
20494 else
20495 {
20496 EMSG2(_(e_invexpr2), arg);
20497 nr = 0;
20498 }
20499 }
20500
20501 if (nr > 0)
20502 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20503 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020504 {
20505 if (wp == NULL)
20506 {
20507 /* didn't find it in this tabpage */
20508 nr = 0;
20509 break;
20510 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020511 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020512 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020513 return nr;
20514}
20515#endif
20516
20517/*
20518 * "tabpagewinnr()" function
20519 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020521f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020522{
20523 int nr = 1;
20524#ifdef FEAT_WINDOWS
20525 tabpage_T *tp;
20526
20527 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20528 if (tp == NULL)
20529 nr = 0;
20530 else
20531 nr = get_winnr(tp, &argvars[1]);
20532#endif
20533 rettv->vval.v_number = nr;
20534}
20535
20536
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020537/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020538 * "tagfiles()" function
20539 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020541f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020542{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020543 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020544 tagname_T tn;
20545 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020546
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020547 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020548 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020549 fname = alloc(MAXPATHL);
20550 if (fname == NULL)
20551 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020552
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020553 for (first = TRUE; ; first = FALSE)
20554 if (get_tagfname(&tn, first, fname) == FAIL
20555 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020556 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020557 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020558 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020559}
20560
20561/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020562 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020563 */
20564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020565f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020566{
20567 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020568
20569 tag_pattern = get_tv_string(&argvars[0]);
20570
20571 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020572 if (*tag_pattern == NUL)
20573 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020574
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020575 if (rettv_list_alloc(rettv) == OK)
20576 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020577}
20578
20579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020580 * "tempname()" function
20581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020583f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584{
20585 static int x = 'A';
20586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020587 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020588 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020589
20590 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20591 * names. Skip 'I' and 'O', they are used for shell redirection. */
20592 do
20593 {
20594 if (x == 'Z')
20595 x = '0';
20596 else if (x == '9')
20597 x = 'A';
20598 else
20599 {
20600#ifdef EBCDIC
20601 if (x == 'I')
20602 x = 'J';
20603 else if (x == 'R')
20604 x = 'S';
20605 else
20606#endif
20607 ++x;
20608 }
20609 } while (x == 'I' || x == 'O');
20610}
20611
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020612#ifdef FEAT_FLOAT
20613/*
20614 * "tan()" function
20615 */
20616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020617f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020618{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020619 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020620
20621 rettv->v_type = VAR_FLOAT;
20622 if (get_float_arg(argvars, &f) == OK)
20623 rettv->vval.v_float = tan(f);
20624 else
20625 rettv->vval.v_float = 0.0;
20626}
20627
20628/*
20629 * "tanh()" function
20630 */
20631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020633{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020634 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020635
20636 rettv->v_type = VAR_FLOAT;
20637 if (get_float_arg(argvars, &f) == OK)
20638 rettv->vval.v_float = tanh(f);
20639 else
20640 rettv->vval.v_float = 0.0;
20641}
20642#endif
20643
Bram Moolenaar574860b2016-05-24 17:33:34 +020020644/*
20645 * "test_garbagecollect_now()" function
20646 */
20647 static void
20648f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20649{
20650 /* This is dangerous, any Lists and Dicts used internally may be freed
20651 * while still in use. */
20652 garbage_collect(TRUE);
20653}
20654
20655#ifdef FEAT_JOB_CHANNEL
20656 static void
20657f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20658{
20659 rettv->v_type = VAR_CHANNEL;
20660 rettv->vval.v_channel = NULL;
20661}
20662#endif
20663
20664 static void
20665f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20666{
20667 rettv->v_type = VAR_DICT;
20668 rettv->vval.v_dict = NULL;
20669}
20670
20671#ifdef FEAT_JOB_CHANNEL
20672 static void
20673f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20674{
20675 rettv->v_type = VAR_JOB;
20676 rettv->vval.v_job = NULL;
20677}
20678#endif
20679
20680 static void
20681f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20682{
20683 rettv->v_type = VAR_LIST;
20684 rettv->vval.v_list = NULL;
20685}
20686
20687 static void
20688f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20689{
20690 rettv->v_type = VAR_PARTIAL;
20691 rettv->vval.v_partial = NULL;
20692}
20693
20694 static void
20695f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20696{
20697 rettv->v_type = VAR_STRING;
20698 rettv->vval.v_string = NULL;
20699}
20700
Bram Moolenaar975b5272016-03-15 23:10:59 +010020701#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20702/*
20703 * Get a callback from "arg". It can be a Funcref or a function name.
20704 * When "arg" is zero return an empty string.
20705 * Return NULL for an invalid argument.
20706 */
20707 char_u *
20708get_callback(typval_T *arg, partial_T **pp)
20709{
20710 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20711 {
20712 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020713 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020714 return (*pp)->pt_name;
20715 }
20716 *pp = NULL;
20717 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20718 return arg->vval.v_string;
20719 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20720 return (char_u *)"";
20721 EMSG(_("E921: Invalid callback argument"));
20722 return NULL;
20723}
20724#endif
20725
20726#ifdef FEAT_TIMERS
20727/*
20728 * "timer_start(time, callback [, options])" function
20729 */
20730 static void
20731f_timer_start(typval_T *argvars, typval_T *rettv)
20732{
20733 long msec = get_tv_number(&argvars[0]);
20734 timer_T *timer;
20735 int repeat = 0;
20736 char_u *callback;
20737 dict_T *dict;
20738
Bram Moolenaar38499922016-04-22 20:46:52 +020020739 if (check_secure())
20740 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020741 if (argvars[2].v_type != VAR_UNKNOWN)
20742 {
20743 if (argvars[2].v_type != VAR_DICT
20744 || (dict = argvars[2].vval.v_dict) == NULL)
20745 {
20746 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20747 return;
20748 }
20749 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20750 repeat = get_dict_number(dict, (char_u *)"repeat");
20751 }
20752
20753 timer = create_timer(msec, repeat);
20754 callback = get_callback(&argvars[1], &timer->tr_partial);
20755 if (callback == NULL)
20756 {
20757 stop_timer(timer);
20758 rettv->vval.v_number = -1;
20759 }
20760 else
20761 {
20762 timer->tr_callback = vim_strsave(callback);
20763 rettv->vval.v_number = timer->tr_id;
20764 }
20765}
20766
20767/*
20768 * "timer_stop(timer)" function
20769 */
20770 static void
20771f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20772{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020773 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020774
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020775 if (argvars[0].v_type != VAR_NUMBER)
20776 {
20777 EMSG(_(e_number_exp));
20778 return;
20779 }
20780 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020781 if (timer != NULL)
20782 stop_timer(timer);
20783}
20784#endif
20785
Bram Moolenaard52d9742005-08-21 22:20:28 +000020786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020787 * "tolower(string)" function
20788 */
20789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020790f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791{
20792 char_u *p;
20793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020794 p = vim_strsave(get_tv_string(&argvars[0]));
20795 rettv->v_type = VAR_STRING;
20796 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797
20798 if (p != NULL)
20799 while (*p != NUL)
20800 {
20801#ifdef FEAT_MBYTE
20802 int l;
20803
20804 if (enc_utf8)
20805 {
20806 int c, lc;
20807
20808 c = utf_ptr2char(p);
20809 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020810 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020811 /* TODO: reallocate string when byte count changes. */
20812 if (utf_char2len(lc) == l)
20813 utf_char2bytes(lc, p);
20814 p += l;
20815 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020816 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020817 p += l; /* skip multi-byte character */
20818 else
20819#endif
20820 {
20821 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20822 ++p;
20823 }
20824 }
20825}
20826
20827/*
20828 * "toupper(string)" function
20829 */
20830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020831f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020833 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020834 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835}
20836
20837/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020838 * "tr(string, fromstr, tostr)" function
20839 */
20840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020841f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020842{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020843 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020844 char_u *fromstr;
20845 char_u *tostr;
20846 char_u *p;
20847#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020848 int inlen;
20849 int fromlen;
20850 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020851 int idx;
20852 char_u *cpstr;
20853 int cplen;
20854 int first = TRUE;
20855#endif
20856 char_u buf[NUMBUFLEN];
20857 char_u buf2[NUMBUFLEN];
20858 garray_T ga;
20859
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020860 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020861 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20862 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020863
20864 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020865 rettv->v_type = VAR_STRING;
20866 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020867 if (fromstr == NULL || tostr == NULL)
20868 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020869 ga_init2(&ga, (int)sizeof(char), 80);
20870
20871#ifdef FEAT_MBYTE
20872 if (!has_mbyte)
20873#endif
20874 /* not multi-byte: fromstr and tostr must be the same length */
20875 if (STRLEN(fromstr) != STRLEN(tostr))
20876 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020877#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020878error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020879#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020880 EMSG2(_(e_invarg2), fromstr);
20881 ga_clear(&ga);
20882 return;
20883 }
20884
20885 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020886 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020887 {
20888#ifdef FEAT_MBYTE
20889 if (has_mbyte)
20890 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020891 inlen = (*mb_ptr2len)(in_str);
20892 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020893 cplen = inlen;
20894 idx = 0;
20895 for (p = fromstr; *p != NUL; p += fromlen)
20896 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020897 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020898 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020899 {
20900 for (p = tostr; *p != NUL; p += tolen)
20901 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020902 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020903 if (idx-- == 0)
20904 {
20905 cplen = tolen;
20906 cpstr = p;
20907 break;
20908 }
20909 }
20910 if (*p == NUL) /* tostr is shorter than fromstr */
20911 goto error;
20912 break;
20913 }
20914 ++idx;
20915 }
20916
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020917 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020918 {
20919 /* Check that fromstr and tostr have the same number of
20920 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020921 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020922 first = FALSE;
20923 for (p = tostr; *p != NUL; p += tolen)
20924 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020925 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020926 --idx;
20927 }
20928 if (idx != 0)
20929 goto error;
20930 }
20931
Bram Moolenaarcde88542015-08-11 19:14:00 +020020932 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020933 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020934 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020935
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020936 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020937 }
20938 else
20939#endif
20940 {
20941 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020942 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020943 if (p != NULL)
20944 ga_append(&ga, tostr[p - fromstr]);
20945 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020946 ga_append(&ga, *in_str);
20947 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020948 }
20949 }
20950
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020951 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020952 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020953 ga_append(&ga, NUL);
20954
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020955 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020956}
20957
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020958#ifdef FEAT_FLOAT
20959/*
20960 * "trunc({float})" function
20961 */
20962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020963f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020964{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020965 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020966
20967 rettv->v_type = VAR_FLOAT;
20968 if (get_float_arg(argvars, &f) == OK)
20969 /* trunc() is not in C90, use floor() or ceil() instead. */
20970 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20971 else
20972 rettv->vval.v_float = 0.0;
20973}
20974#endif
20975
Bram Moolenaar8299df92004-07-10 09:47:34 +000020976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 * "type(expr)" function
20978 */
20979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020980f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020982 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020983
20984 switch (argvars[0].v_type)
20985 {
20986 case VAR_NUMBER: n = 0; break;
20987 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020988 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020989 case VAR_FUNC: n = 2; break;
20990 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020991 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020992 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020993 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020994 if (argvars[0].vval.v_number == VVAL_FALSE
20995 || argvars[0].vval.v_number == VVAL_TRUE)
20996 n = 6;
20997 else
20998 n = 7;
20999 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021000 case VAR_JOB: n = 8; break;
21001 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021002 case VAR_UNKNOWN:
21003 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21004 n = -1;
21005 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021006 }
21007 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008}
21009
21010/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021011 * "undofile(name)" function
21012 */
21013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021014f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021015{
21016 rettv->v_type = VAR_STRING;
21017#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021018 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021019 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021020
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021021 if (*fname == NUL)
21022 {
21023 /* If there is no file name there will be no undo file. */
21024 rettv->vval.v_string = NULL;
21025 }
21026 else
21027 {
21028 char_u *ffname = FullName_save(fname, FALSE);
21029
21030 if (ffname != NULL)
21031 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21032 vim_free(ffname);
21033 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021034 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021035#else
21036 rettv->vval.v_string = NULL;
21037#endif
21038}
21039
21040/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021041 * "undotree()" function
21042 */
21043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021044f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021045{
21046 if (rettv_dict_alloc(rettv) == OK)
21047 {
21048 dict_T *dict = rettv->vval.v_dict;
21049 list_T *list;
21050
Bram Moolenaar730cde92010-06-27 05:18:54 +020021051 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021052 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021053 dict_add_nr_str(dict, "save_last",
21054 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021055 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21056 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021057 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021058
21059 list = list_alloc();
21060 if (list != NULL)
21061 {
21062 u_eval_tree(curbuf->b_u_oldhead, list);
21063 dict_add_list(dict, "entries", list);
21064 }
21065 }
21066}
21067
21068/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021069 * "values(dict)" function
21070 */
21071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021072f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021073{
21074 dict_list(argvars, rettv, 1);
21075}
21076
21077/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 * "virtcol(string)" function
21079 */
21080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021081f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082{
21083 colnr_T vcol = 0;
21084 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021085 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021087 fp = var2fpos(&argvars[0], FALSE, &fnum);
21088 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21089 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 {
21091 getvvcol(curwin, fp, NULL, NULL, &vcol);
21092 ++vcol;
21093 }
21094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021095 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096}
21097
21098/*
21099 * "visualmode()" function
21100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021102f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021104 char_u str[2];
21105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021107 str[0] = curbuf->b_visual_mode_eval;
21108 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021109 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110
21111 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021112 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021113 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114}
21115
21116/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021117 * "wildmenumode()" function
21118 */
21119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021120f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021121{
21122#ifdef FEAT_WILDMENU
21123 if (wild_menu_showing)
21124 rettv->vval.v_number = 1;
21125#endif
21126}
21127
21128/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 * "winbufnr(nr)" function
21130 */
21131 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021132f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133{
21134 win_T *wp;
21135
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021136 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021137 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021138 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021140 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021141}
21142
21143/*
21144 * "wincol()" function
21145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021147f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148{
21149 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021150 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151}
21152
21153/*
21154 * "winheight(nr)" function
21155 */
21156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021157f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021158{
21159 win_T *wp;
21160
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021161 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021163 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021165 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021166}
21167
21168/*
21169 * "winline()" function
21170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021172f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173{
21174 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021175 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176}
21177
21178/*
21179 * "winnr()" function
21180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021182f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183{
21184 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021185
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021187 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190}
21191
21192/*
21193 * "winrestcmd()" function
21194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021196f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197{
21198#ifdef FEAT_WINDOWS
21199 win_T *wp;
21200 int winnr = 1;
21201 garray_T ga;
21202 char_u buf[50];
21203
21204 ga_init2(&ga, (int)sizeof(char), 70);
21205 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21206 {
21207 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21208 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21210 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 ++winnr;
21212 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021213 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021215 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021218#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021219 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220}
21221
21222/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021223 * "winrestview()" function
21224 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021226f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021227{
21228 dict_T *dict;
21229
21230 if (argvars[0].v_type != VAR_DICT
21231 || (dict = argvars[0].vval.v_dict) == NULL)
21232 EMSG(_(e_invarg));
21233 else
21234 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021235 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21236 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21237 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21238 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021239#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021240 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21241 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021242#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021243 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21244 {
21245 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21246 curwin->w_set_curswant = FALSE;
21247 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021248
Bram Moolenaar82c25852014-05-28 16:47:16 +020021249 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21250 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021251#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021252 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21253 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021254#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021255 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21256 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21257 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21258 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021259
21260 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021261 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021262# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021263 win_new_width(curwin, W_WIDTH(curwin));
21264# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021265 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021266
Bram Moolenaarb851a962014-10-31 15:45:52 +010021267 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021268 curwin->w_topline = 1;
21269 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21270 curwin->w_topline = curbuf->b_ml.ml_line_count;
21271#ifdef FEAT_DIFF
21272 check_topfill(curwin, TRUE);
21273#endif
21274 }
21275}
21276
21277/*
21278 * "winsaveview()" function
21279 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021281f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021282{
21283 dict_T *dict;
21284
Bram Moolenaara800b422010-06-27 01:15:55 +020021285 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021286 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021287 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021288
21289 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21290 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21291#ifdef FEAT_VIRTUALEDIT
21292 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21293#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021294 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021295 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21296
21297 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21298#ifdef FEAT_DIFF
21299 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21300#endif
21301 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21302 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21303}
21304
21305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 * "winwidth(nr)" function
21307 */
21308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021309f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021310{
21311 win_T *wp;
21312
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021313 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021315 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021317#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021318 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021320 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321#endif
21322}
21323
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021325 * "wordcount()" function
21326 */
21327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021328f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021329{
21330 if (rettv_dict_alloc(rettv) == FAIL)
21331 return;
21332 cursor_pos_info(rettv->vval.v_dict);
21333}
21334
21335/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021336 * Write list of strings to file
21337 */
21338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021340{
21341 listitem_T *li;
21342 int c;
21343 int ret = OK;
21344 char_u *s;
21345
21346 for (li = list->lv_first; li != NULL; li = li->li_next)
21347 {
21348 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21349 {
21350 if (*s == '\n')
21351 c = putc(NUL, fd);
21352 else
21353 c = putc(*s, fd);
21354 if (c == EOF)
21355 {
21356 ret = FAIL;
21357 break;
21358 }
21359 }
21360 if (!binary || li->li_next != NULL)
21361 if (putc('\n', fd) == EOF)
21362 {
21363 ret = FAIL;
21364 break;
21365 }
21366 if (ret == FAIL)
21367 {
21368 EMSG(_(e_write));
21369 break;
21370 }
21371 }
21372 return ret;
21373}
21374
21375/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021376 * "writefile()" function
21377 */
21378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021379f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021380{
21381 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021382 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021383 char_u *fname;
21384 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021385 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021386
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021387 if (check_restricted() || check_secure())
21388 return;
21389
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021390 if (argvars[0].v_type != VAR_LIST)
21391 {
21392 EMSG2(_(e_listarg), "writefile()");
21393 return;
21394 }
21395 if (argvars[0].vval.v_list == NULL)
21396 return;
21397
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021398 if (argvars[2].v_type != VAR_UNKNOWN)
21399 {
21400 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21401 binary = TRUE;
21402 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21403 append = TRUE;
21404 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021405
21406 /* Always open the file in binary mode, library functions have a mind of
21407 * their own about CR-LF conversion. */
21408 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021409 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21410 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021411 {
21412 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21413 ret = -1;
21414 }
21415 else
21416 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021417 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21418 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021419 fclose(fd);
21420 }
21421
21422 rettv->vval.v_number = ret;
21423}
21424
21425/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021426 * "xor(expr, expr)" function
21427 */
21428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021429f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021430{
21431 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21432 ^ get_tv_number_chk(&argvars[1], NULL);
21433}
21434
21435
21436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021438 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 */
21440 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021441var2fpos(
21442 typval_T *varp,
21443 int dollar_lnum, /* TRUE when $ is last line */
21444 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021445{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021446 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021448 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449
Bram Moolenaara5525202006-03-02 22:52:09 +000021450 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021451 if (varp->v_type == VAR_LIST)
21452 {
21453 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021454 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021455 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021456 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021457
21458 l = varp->vval.v_list;
21459 if (l == NULL)
21460 return NULL;
21461
21462 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021463 pos.lnum = list_find_nr(l, 0L, &error);
21464 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021465 return NULL; /* invalid line number */
21466
21467 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021468 pos.col = list_find_nr(l, 1L, &error);
21469 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021470 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021471 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021472
21473 /* We accept "$" for the column number: last column. */
21474 li = list_find(l, 1L);
21475 if (li != NULL && li->li_tv.v_type == VAR_STRING
21476 && li->li_tv.vval.v_string != NULL
21477 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21478 pos.col = len + 1;
21479
Bram Moolenaara5525202006-03-02 22:52:09 +000021480 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021481 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021482 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021483 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021484
Bram Moolenaara5525202006-03-02 22:52:09 +000021485#ifdef FEAT_VIRTUALEDIT
21486 /* Get the virtual offset. Defaults to zero. */
21487 pos.coladd = list_find_nr(l, 2L, &error);
21488 if (error)
21489 pos.coladd = 0;
21490#endif
21491
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021492 return &pos;
21493 }
21494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021495 name = get_tv_string_chk(varp);
21496 if (name == NULL)
21497 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021498 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021500 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21501 {
21502 if (VIsual_active)
21503 return &VIsual;
21504 return &curwin->w_cursor;
21505 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021506 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021508 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21510 return NULL;
21511 return pp;
21512 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021513
21514#ifdef FEAT_VIRTUALEDIT
21515 pos.coladd = 0;
21516#endif
21517
Bram Moolenaar477933c2007-07-17 14:32:23 +000021518 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021519 {
21520 pos.col = 0;
21521 if (name[1] == '0') /* "w0": first visible line */
21522 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021523 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021524 pos.lnum = curwin->w_topline;
21525 return &pos;
21526 }
21527 else if (name[1] == '$') /* "w$": last visible line */
21528 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021529 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021530 pos.lnum = curwin->w_botline - 1;
21531 return &pos;
21532 }
21533 }
21534 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021536 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 {
21538 pos.lnum = curbuf->b_ml.ml_line_count;
21539 pos.col = 0;
21540 }
21541 else
21542 {
21543 pos.lnum = curwin->w_cursor.lnum;
21544 pos.col = (colnr_T)STRLEN(ml_get_curline());
21545 }
21546 return &pos;
21547 }
21548 return NULL;
21549}
21550
21551/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021552 * Convert list in "arg" into a position and optional file number.
21553 * When "fnump" is NULL there is no file number, only 3 items.
21554 * Note that the column is passed on as-is, the caller may want to decrement
21555 * it to use 1 for the first column.
21556 * Return FAIL when conversion is not possible, doesn't check the position for
21557 * validity.
21558 */
21559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021560list2fpos(
21561 typval_T *arg,
21562 pos_T *posp,
21563 int *fnump,
21564 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021565{
21566 list_T *l = arg->vval.v_list;
21567 long i = 0;
21568 long n;
21569
Bram Moolenaar493c1782014-05-28 14:34:46 +020021570 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21571 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021572 if (arg->v_type != VAR_LIST
21573 || l == NULL
21574 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021575 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021576 return FAIL;
21577
21578 if (fnump != NULL)
21579 {
21580 n = list_find_nr(l, i++, NULL); /* fnum */
21581 if (n < 0)
21582 return FAIL;
21583 if (n == 0)
21584 n = curbuf->b_fnum; /* current buffer */
21585 *fnump = n;
21586 }
21587
21588 n = list_find_nr(l, i++, NULL); /* lnum */
21589 if (n < 0)
21590 return FAIL;
21591 posp->lnum = n;
21592
21593 n = list_find_nr(l, i++, NULL); /* col */
21594 if (n < 0)
21595 return FAIL;
21596 posp->col = n;
21597
21598#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021599 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021600 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021601 posp->coladd = 0;
21602 else
21603 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021604#endif
21605
Bram Moolenaar493c1782014-05-28 14:34:46 +020021606 if (curswantp != NULL)
21607 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21608
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021609 return OK;
21610}
21611
21612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 * Get the length of an environment variable name.
21614 * Advance "arg" to the first character after the name.
21615 * Return 0 for error.
21616 */
21617 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021618get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021619{
21620 char_u *p;
21621 int len;
21622
21623 for (p = *arg; vim_isIDc(*p); ++p)
21624 ;
21625 if (p == *arg) /* no name found */
21626 return 0;
21627
21628 len = (int)(p - *arg);
21629 *arg = p;
21630 return len;
21631}
21632
21633/*
21634 * Get the length of the name of a function or internal variable.
21635 * "arg" is advanced to the first non-white character after the name.
21636 * Return 0 if something is wrong.
21637 */
21638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021639get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021640{
21641 char_u *p;
21642 int len;
21643
21644 /* Find the end of the name. */
21645 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021646 {
21647 if (*p == ':')
21648 {
21649 /* "s:" is start of "s:var", but "n:" is not and can be used in
21650 * slice "[n:]". Also "xx:" is not a namespace. */
21651 len = (int)(p - *arg);
21652 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21653 || len > 1)
21654 break;
21655 }
21656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021657 if (p == *arg) /* no name found */
21658 return 0;
21659
21660 len = (int)(p - *arg);
21661 *arg = skipwhite(p);
21662
21663 return len;
21664}
21665
21666/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021667 * Get the length of the name of a variable or function.
21668 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021670 * Return -1 if curly braces expansion failed.
21671 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 * If the name contains 'magic' {}'s, expand them and return the
21673 * expanded name in an allocated string via 'alias' - caller must free.
21674 */
21675 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021676get_name_len(
21677 char_u **arg,
21678 char_u **alias,
21679 int evaluate,
21680 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021681{
21682 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 char_u *p;
21684 char_u *expr_start;
21685 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686
21687 *alias = NULL; /* default to no alias */
21688
21689 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21690 && (*arg)[2] == (int)KE_SNR)
21691 {
21692 /* hard coded <SNR>, already translated */
21693 *arg += 3;
21694 return get_id_len(arg) + 3;
21695 }
21696 len = eval_fname_script(*arg);
21697 if (len > 0)
21698 {
21699 /* literal "<SID>", "s:" or "<SNR>" */
21700 *arg += len;
21701 }
21702
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021704 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021705 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021706 p = find_name_end(*arg, &expr_start, &expr_end,
21707 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021708 if (expr_start != NULL)
21709 {
21710 char_u *temp_string;
21711
21712 if (!evaluate)
21713 {
21714 len += (int)(p - *arg);
21715 *arg = skipwhite(p);
21716 return len;
21717 }
21718
21719 /*
21720 * Include any <SID> etc in the expanded string:
21721 * Thus the -len here.
21722 */
21723 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21724 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021725 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 *alias = temp_string;
21727 *arg = skipwhite(p);
21728 return (int)STRLEN(temp_string);
21729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730
21731 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021732 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 EMSG2(_(e_invexpr2), *arg);
21734
21735 return len;
21736}
21737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021738/*
21739 * Find the end of a variable or function name, taking care of magic braces.
21740 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21741 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021742 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021743 * Return a pointer to just after the name. Equal to "arg" if there is no
21744 * valid name.
21745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021747find_name_end(
21748 char_u *arg,
21749 char_u **expr_start,
21750 char_u **expr_end,
21751 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021753 int mb_nest = 0;
21754 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021755 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021756 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021758 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021760 *expr_start = NULL;
21761 *expr_end = NULL;
21762 }
21763
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021764 /* Quick check for valid starting character. */
21765 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21766 return arg;
21767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021768 for (p = arg; *p != NUL
21769 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021770 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021771 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021772 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021773 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021774 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021775 if (*p == '\'')
21776 {
21777 /* skip over 'string' to avoid counting [ and ] inside it. */
21778 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21779 ;
21780 if (*p == NUL)
21781 break;
21782 }
21783 else if (*p == '"')
21784 {
21785 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21786 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21787 if (*p == '\\' && p[1] != NUL)
21788 ++p;
21789 if (*p == NUL)
21790 break;
21791 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021792 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21793 {
21794 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021795 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021796 len = (int)(p - arg);
21797 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021798 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021799 break;
21800 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021802 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 if (*p == '[')
21805 ++br_nest;
21806 else if (*p == ']')
21807 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021809
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021810 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021812 if (*p == '{')
21813 {
21814 mb_nest++;
21815 if (expr_start != NULL && *expr_start == NULL)
21816 *expr_start = p;
21817 }
21818 else if (*p == '}')
21819 {
21820 mb_nest--;
21821 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21822 *expr_end = p;
21823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021825 }
21826
21827 return p;
21828}
21829
21830/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021831 * Expands out the 'magic' {}'s in a variable/function name.
21832 * Note that this can call itself recursively, to deal with
21833 * constructs like foo{bar}{baz}{bam}
21834 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21835 * "in_start" ^
21836 * "expr_start" ^
21837 * "expr_end" ^
21838 * "in_end" ^
21839 *
21840 * Returns a new allocated string, which the caller must free.
21841 * Returns NULL for failure.
21842 */
21843 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021844make_expanded_name(
21845 char_u *in_start,
21846 char_u *expr_start,
21847 char_u *expr_end,
21848 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021849{
21850 char_u c1;
21851 char_u *retval = NULL;
21852 char_u *temp_result;
21853 char_u *nextcmd = NULL;
21854
21855 if (expr_end == NULL || in_end == NULL)
21856 return NULL;
21857 *expr_start = NUL;
21858 *expr_end = NUL;
21859 c1 = *in_end;
21860 *in_end = NUL;
21861
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021862 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021863 if (temp_result != NULL && nextcmd == NULL)
21864 {
21865 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21866 + (in_end - expr_end) + 1));
21867 if (retval != NULL)
21868 {
21869 STRCPY(retval, in_start);
21870 STRCAT(retval, temp_result);
21871 STRCAT(retval, expr_end + 1);
21872 }
21873 }
21874 vim_free(temp_result);
21875
21876 *in_end = c1; /* put char back for error messages */
21877 *expr_start = '{';
21878 *expr_end = '}';
21879
21880 if (retval != NULL)
21881 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021882 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021883 if (expr_start != NULL)
21884 {
21885 /* Further expansion! */
21886 temp_result = make_expanded_name(retval, expr_start,
21887 expr_end, temp_result);
21888 vim_free(retval);
21889 retval = temp_result;
21890 }
21891 }
21892
21893 return retval;
21894}
21895
21896/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021897 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021898 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 */
21900 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021901eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021903 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21904}
21905
21906/*
21907 * Return TRUE if character "c" can be used as the first character in a
21908 * variable or function name (excluding '{' and '}').
21909 */
21910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021911eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021912{
21913 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021914}
21915
21916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021917 * Set number v: variable to "val".
21918 */
21919 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021920set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923}
21924
21925/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021926 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021927 */
21928 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021929get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021931 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932}
21933
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021934/*
21935 * Get string v: variable value. Uses a static buffer, can only be used once.
21936 */
21937 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021938get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021939{
21940 return get_tv_string(&vimvars[idx].vv_tv);
21941}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021942
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021944 * Get List v: variable value. Caller must take care of reference count when
21945 * needed.
21946 */
21947 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021948get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021949{
21950 return vimvars[idx].vv_list;
21951}
21952
21953/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021954 * Set v:char to character "c".
21955 */
21956 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021957set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021958{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021959 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021960
21961#ifdef FEAT_MBYTE
21962 if (has_mbyte)
21963 buf[(*mb_char2bytes)(c, buf)] = NUL;
21964 else
21965#endif
21966 {
21967 buf[0] = c;
21968 buf[1] = NUL;
21969 }
21970 set_vim_var_string(VV_CHAR, buf, -1);
21971}
21972
21973/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021974 * Set v:count to "count" and v:count1 to "count1".
21975 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 */
21977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021978set_vcount(
21979 long count,
21980 long count1,
21981 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021983 if (set_prevcount)
21984 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021985 vimvars[VV_COUNT].vv_nr = count;
21986 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987}
21988
21989/*
21990 * Set string v: variable to a copy of "val".
21991 */
21992 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021993set_vim_var_string(
21994 int idx,
21995 char_u *val,
21996 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997{
Bram Moolenaara542c682016-01-31 16:28:04 +010021998 clear_tv(&vimvars[idx].vv_di.di_tv);
21999 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022001 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022003 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022005 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022006}
22007
22008/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022009 * Set List v: variable to "val".
22010 */
22011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022012set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022013{
Bram Moolenaara542c682016-01-31 16:28:04 +010022014 clear_tv(&vimvars[idx].vv_di.di_tv);
22015 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022016 vimvars[idx].vv_list = val;
22017 if (val != NULL)
22018 ++val->lv_refcount;
22019}
22020
22021/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022022 * Set Dictionary v: variable to "val".
22023 */
22024 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022025set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022026{
22027 int todo;
22028 hashitem_T *hi;
22029
Bram Moolenaara542c682016-01-31 16:28:04 +010022030 clear_tv(&vimvars[idx].vv_di.di_tv);
22031 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022032 vimvars[idx].vv_dict = val;
22033 if (val != NULL)
22034 {
22035 ++val->dv_refcount;
22036
22037 /* Set readonly */
22038 todo = (int)val->dv_hashtab.ht_used;
22039 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22040 {
22041 if (HASHITEM_EMPTY(hi))
22042 continue;
22043 --todo;
22044 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22045 }
22046 }
22047}
22048
22049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 * Set v:register if needed.
22051 */
22052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022053set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054{
22055 char_u regname;
22056
22057 if (c == 0 || c == ' ')
22058 regname = '"';
22059 else
22060 regname = c;
22061 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022062 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 set_vim_var_string(VV_REG, &regname, 1);
22064}
22065
22066/*
22067 * Get or set v:exception. If "oldval" == NULL, return the current value.
22068 * Otherwise, restore the value to "oldval" and return NULL.
22069 * Must always be called in pairs to save and restore v:exception! Does not
22070 * take care of memory allocations.
22071 */
22072 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022073v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022074{
22075 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022076 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022077
Bram Moolenaare9a41262005-01-15 22:18:47 +000022078 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 return NULL;
22080}
22081
22082/*
22083 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22084 * Otherwise, restore the value to "oldval" and return NULL.
22085 * Must always be called in pairs to save and restore v:throwpoint! Does not
22086 * take care of memory allocations.
22087 */
22088 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022089v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022090{
22091 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022092 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093
Bram Moolenaare9a41262005-01-15 22:18:47 +000022094 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022095 return NULL;
22096}
22097
22098#if defined(FEAT_AUTOCMD) || defined(PROTO)
22099/*
22100 * Set v:cmdarg.
22101 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22102 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22103 * Must always be called in pairs!
22104 */
22105 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022106set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107{
22108 char_u *oldval;
22109 char_u *newval;
22110 unsigned len;
22111
Bram Moolenaare9a41262005-01-15 22:18:47 +000022112 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022113 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022115 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022116 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022117 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118 }
22119
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022120 if (eap->force_bin == FORCE_BIN)
22121 len = 6;
22122 else if (eap->force_bin == FORCE_NOBIN)
22123 len = 8;
22124 else
22125 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022126
22127 if (eap->read_edit)
22128 len += 7;
22129
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022130 if (eap->force_ff != 0)
22131 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22132# ifdef FEAT_MBYTE
22133 if (eap->force_enc != 0)
22134 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022135 if (eap->bad_char != 0)
22136 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022137# endif
22138
22139 newval = alloc(len + 1);
22140 if (newval == NULL)
22141 return NULL;
22142
22143 if (eap->force_bin == FORCE_BIN)
22144 sprintf((char *)newval, " ++bin");
22145 else if (eap->force_bin == FORCE_NOBIN)
22146 sprintf((char *)newval, " ++nobin");
22147 else
22148 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022149
22150 if (eap->read_edit)
22151 STRCAT(newval, " ++edit");
22152
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022153 if (eap->force_ff != 0)
22154 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22155 eap->cmd + eap->force_ff);
22156# ifdef FEAT_MBYTE
22157 if (eap->force_enc != 0)
22158 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22159 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022160 if (eap->bad_char == BAD_KEEP)
22161 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22162 else if (eap->bad_char == BAD_DROP)
22163 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22164 else if (eap->bad_char != 0)
22165 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022166# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022167 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022168 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169}
22170#endif
22171
22172/*
22173 * Get the value of internal variable "name".
22174 * Return OK or FAIL.
22175 */
22176 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022177get_var_tv(
22178 char_u *name,
22179 int len, /* length of "name" */
22180 typval_T *rettv, /* NULL when only checking existence */
22181 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22182 int verbose, /* may give error message */
22183 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184{
22185 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022186 typval_T *tv = NULL;
22187 typval_T atv;
22188 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022190
22191 /* truncate the name, so that we can use strcmp() */
22192 cc = name[len];
22193 name[len] = NUL;
22194
22195 /*
22196 * Check for "b:changedtick".
22197 */
22198 if (STRCMP(name, "b:changedtick") == 0)
22199 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022200 atv.v_type = VAR_NUMBER;
22201 atv.vval.v_number = curbuf->b_changedtick;
22202 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 }
22204
22205 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 * Check for user-defined variables.
22207 */
22208 else
22209 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022210 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022212 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022213 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022214 if (dip != NULL)
22215 *dip = v;
22216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022217 }
22218
Bram Moolenaare9a41262005-01-15 22:18:47 +000022219 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022221 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022222 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 ret = FAIL;
22224 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022225 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022226 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227
22228 name[len] = cc;
22229
22230 return ret;
22231}
22232
22233/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022234 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22235 * Also handle function call with Funcref variable: func(expr)
22236 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22237 */
22238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239handle_subscript(
22240 char_u **arg,
22241 typval_T *rettv,
22242 int evaluate, /* do more than finding the end */
22243 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022244{
22245 int ret = OK;
22246 dict_T *selfdict = NULL;
22247 char_u *s;
22248 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022249 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022250
22251 while (ret == OK
22252 && (**arg == '['
22253 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022254 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22255 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022256 && !vim_iswhite(*(*arg - 1)))
22257 {
22258 if (**arg == '(')
22259 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022260 partial_T *pt = NULL;
22261
Bram Moolenaard9fba312005-06-26 22:34:35 +000022262 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022263 if (evaluate)
22264 {
22265 functv = *rettv;
22266 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022267
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022268 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022269 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022270 {
22271 pt = functv.vval.v_partial;
22272 s = pt->pt_name;
22273 }
22274 else
22275 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022276 }
22277 else
22278 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022279 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022280 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022281 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022282
22283 /* Clear the funcref afterwards, so that deleting it while
22284 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022285 if (evaluate)
22286 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022287
22288 /* Stop the expression evaluation when immediately aborting on
22289 * error, or when an interrupt occurred or an exception was thrown
22290 * but not caught. */
22291 if (aborting())
22292 {
22293 if (ret == OK)
22294 clear_tv(rettv);
22295 ret = FAIL;
22296 }
22297 dict_unref(selfdict);
22298 selfdict = NULL;
22299 }
22300 else /* **arg == '[' || **arg == '.' */
22301 {
22302 dict_unref(selfdict);
22303 if (rettv->v_type == VAR_DICT)
22304 {
22305 selfdict = rettv->vval.v_dict;
22306 if (selfdict != NULL)
22307 ++selfdict->dv_refcount;
22308 }
22309 else
22310 selfdict = NULL;
22311 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22312 {
22313 clear_tv(rettv);
22314 ret = FAIL;
22315 }
22316 }
22317 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022318
Bram Moolenaar1d429612016-05-24 15:44:17 +020022319 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22320 * Don't do this when "Func" is already a partial that was bound
22321 * explicitly (pt_auto is FALSE). */
22322 if (selfdict != NULL
22323 && (rettv->v_type == VAR_FUNC
22324 || (rettv->v_type == VAR_PARTIAL
22325 && (rettv->vval.v_partial->pt_auto
22326 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022327 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022328 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22329 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022330 char_u *tofree = NULL;
22331 ufunc_T *fp;
22332 char_u fname_buf[FLEN_FIXED + 1];
22333 int error;
22334
22335 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022336 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022337 fp = find_func(fname);
22338 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022339
Bram Moolenaar65639032016-03-16 21:40:30 +010022340 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022341 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022342 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22343
22344 if (pt != NULL)
22345 {
22346 pt->pt_refcount = 1;
22347 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022348 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022349 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022350 if (rettv->v_type == VAR_FUNC)
22351 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022352 /* Just a function: Take over the function name and use
22353 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022354 pt->pt_name = rettv->vval.v_string;
22355 }
22356 else
22357 {
22358 partial_T *ret_pt = rettv->vval.v_partial;
22359 int i;
22360
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022361 /* Partial: copy the function name, use selfdict and copy
22362 * args. Can't take over name or args, the partial might
22363 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022364 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022365 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022366 if (ret_pt->pt_argc > 0)
22367 {
22368 pt->pt_argv = (typval_T *)alloc(
22369 sizeof(typval_T) * ret_pt->pt_argc);
22370 if (pt->pt_argv == NULL)
22371 /* out of memory: drop the arguments */
22372 pt->pt_argc = 0;
22373 else
22374 {
22375 pt->pt_argc = ret_pt->pt_argc;
22376 for (i = 0; i < pt->pt_argc; i++)
22377 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22378 }
22379 }
22380 partial_unref(ret_pt);
22381 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022382 rettv->v_type = VAR_PARTIAL;
22383 rettv->vval.v_partial = pt;
22384 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022385 }
22386 }
22387
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022388 dict_unref(selfdict);
22389 return ret;
22390}
22391
22392/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022393 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022394 * value).
22395 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022396 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022397alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022398{
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022400}
22401
22402/*
22403 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 * The string "s" must have been allocated, it is consumed.
22405 * Return NULL for out of memory, the variable otherwise.
22406 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022407 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022408alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022409{
Bram Moolenaar33570922005-01-25 22:26:29 +000022410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022412 rettv = alloc_tv();
22413 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022414 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022415 rettv->v_type = VAR_STRING;
22416 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022417 }
22418 else
22419 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022420 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421}
22422
22423/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022424 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022427free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428{
22429 if (varp != NULL)
22430 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022431 switch (varp->v_type)
22432 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022433 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022434 func_unref(varp->vval.v_string);
22435 /*FALLTHROUGH*/
22436 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022437 vim_free(varp->vval.v_string);
22438 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022439 case VAR_PARTIAL:
22440 partial_unref(varp->vval.v_partial);
22441 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022442 case VAR_LIST:
22443 list_unref(varp->vval.v_list);
22444 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022445 case VAR_DICT:
22446 dict_unref(varp->vval.v_dict);
22447 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022448 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022449#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022450 job_unref(varp->vval.v_job);
22451 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022452#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022453 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022454#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022455 channel_unref(varp->vval.v_channel);
22456 break;
22457#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022458 case VAR_NUMBER:
22459 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022460 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022461 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022462 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 vim_free(varp);
22465 }
22466}
22467
22468/*
22469 * Free the memory for a variable value and set the value to NULL or 0.
22470 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022472clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473{
22474 if (varp != NULL)
22475 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022476 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022478 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022479 func_unref(varp->vval.v_string);
22480 /*FALLTHROUGH*/
22481 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022482 vim_free(varp->vval.v_string);
22483 varp->vval.v_string = NULL;
22484 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022485 case VAR_PARTIAL:
22486 partial_unref(varp->vval.v_partial);
22487 varp->vval.v_partial = NULL;
22488 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022489 case VAR_LIST:
22490 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022491 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022492 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022493 case VAR_DICT:
22494 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022495 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022496 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022497 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022498 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022499 varp->vval.v_number = 0;
22500 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022501 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022502#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022503 varp->vval.v_float = 0.0;
22504 break;
22505#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022506 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022507#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022508 job_unref(varp->vval.v_job);
22509 varp->vval.v_job = NULL;
22510#endif
22511 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022512 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022513#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022514 channel_unref(varp->vval.v_channel);
22515 varp->vval.v_channel = NULL;
22516#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022517 case VAR_UNKNOWN:
22518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022520 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 }
22522}
22523
22524/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022525 * Set the value of a variable to NULL without freeing items.
22526 */
22527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022528init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022529{
22530 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022531 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022532}
22533
22534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 * Get the number value of a variable.
22536 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022537 * For incompatible types, return 0.
22538 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22539 * caller of incompatible types: it sets *denote to TRUE if "denote"
22540 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022542 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022543get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022545 int error = FALSE;
22546
22547 return get_tv_number_chk(varp, &error); /* return 0L on error */
22548}
22549
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022550 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022551get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022552{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022553 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022555 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022557 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022558 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022559 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022560#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022561 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022562 break;
22563#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022564 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022565 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022566 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022567 break;
22568 case VAR_STRING:
22569 if (varp->vval.v_string != NULL)
22570 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022571 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022572 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022573 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022574 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022575 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022576 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022577 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022578 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022579 case VAR_SPECIAL:
22580 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22581 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022582 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022583#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022584 EMSG(_("E910: Using a Job as a Number"));
22585 break;
22586#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022587 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022588#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022589 EMSG(_("E913: Using a Channel as a Number"));
22590 break;
22591#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022592 case VAR_UNKNOWN:
22593 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022594 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022595 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022596 if (denote == NULL) /* useful for values that must be unsigned */
22597 n = -1;
22598 else
22599 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022600 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601}
22602
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022603#ifdef FEAT_FLOAT
22604 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022605get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022606{
22607 switch (varp->v_type)
22608 {
22609 case VAR_NUMBER:
22610 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022611 case VAR_FLOAT:
22612 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022613 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022614 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022615 EMSG(_("E891: Using a Funcref as a Float"));
22616 break;
22617 case VAR_STRING:
22618 EMSG(_("E892: Using a String as a Float"));
22619 break;
22620 case VAR_LIST:
22621 EMSG(_("E893: Using a List as a Float"));
22622 break;
22623 case VAR_DICT:
22624 EMSG(_("E894: Using a Dictionary as a Float"));
22625 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022626 case VAR_SPECIAL:
22627 EMSG(_("E907: Using a special value as a Float"));
22628 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022629 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022630# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022631 EMSG(_("E911: Using a Job as a Float"));
22632 break;
22633# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022634 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022635# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022636 EMSG(_("E914: Using a Channel as a Float"));
22637 break;
22638# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022639 case VAR_UNKNOWN:
22640 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022641 break;
22642 }
22643 return 0;
22644}
22645#endif
22646
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022648 * Get the lnum from the first argument.
22649 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022650 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 */
22652 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022653get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654{
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022656 linenr_T lnum;
22657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022658 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 if (lnum == 0) /* no valid number, try using line() */
22660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022661 rettv.v_type = VAR_NUMBER;
22662 f_line(argvars, &rettv);
22663 lnum = rettv.vval.v_number;
22664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 }
22666 return lnum;
22667}
22668
22669/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022670 * Get the lnum from the first argument.
22671 * Also accepts "$", then "buf" is used.
22672 * Returns 0 on error.
22673 */
22674 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022675get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022676{
22677 if (argvars[0].v_type == VAR_STRING
22678 && argvars[0].vval.v_string != NULL
22679 && argvars[0].vval.v_string[0] == '$'
22680 && buf != NULL)
22681 return buf->b_ml.ml_line_count;
22682 return get_tv_number_chk(&argvars[0], NULL);
22683}
22684
22685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 * Get the string value of a variable.
22687 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022688 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22689 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 * If the String variable has never been set, return an empty string.
22691 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022692 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22693 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022695 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022696get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697{
22698 static char_u mybuf[NUMBUFLEN];
22699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022700 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701}
22702
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022703 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022704get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022706 char_u *res = get_tv_string_buf_chk(varp, buf);
22707
22708 return res != NULL ? res : (char_u *)"";
22709}
22710
Bram Moolenaar7d647822014-04-05 21:28:56 +020022711/*
22712 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22713 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022715get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022716{
22717 static char_u mybuf[NUMBUFLEN];
22718
22719 return get_tv_string_buf_chk(varp, mybuf);
22720}
22721
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022723get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022724{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022725 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022727 case VAR_NUMBER:
22728 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22729 return buf;
22730 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022731 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022732 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022733 break;
22734 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022735 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022736 break;
22737 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022738 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022739 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022740 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022741#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022742 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022743 break;
22744#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022745 case VAR_STRING:
22746 if (varp->vval.v_string != NULL)
22747 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022748 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022749 case VAR_SPECIAL:
22750 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22751 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022752 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022753#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022754 {
22755 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022756 char *status;
22757
22758 if (job == NULL)
22759 return (char_u *)"no process";
22760 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022761 : job->jv_status == JOB_ENDED ? "dead"
22762 : "run";
22763# ifdef UNIX
22764 vim_snprintf((char *)buf, NUMBUFLEN,
22765 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022766# elif defined(WIN32)
22767 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022768 "process %ld %s",
22769 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022770 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022771# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022772 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022773 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22774# endif
22775 return buf;
22776 }
22777#endif
22778 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022779 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022780#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022781 {
22782 channel_T *channel = varp->vval.v_channel;
22783 char *status = channel_status(channel);
22784
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022785 if (channel == NULL)
22786 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22787 else
22788 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022789 "channel %d %s", channel->ch_id, status);
22790 return buf;
22791 }
22792#endif
22793 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022794 case VAR_UNKNOWN:
22795 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022798 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022799}
22800
22801/*
22802 * Find variable "name" in the list of variables.
22803 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022804 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022805 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022806 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022808 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022809find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022812 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813
Bram Moolenaara7043832005-01-21 11:56:39 +000022814 ht = find_var_ht(name, &varname);
22815 if (htp != NULL)
22816 *htp = ht;
22817 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022819 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820}
22821
22822/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022823 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022824 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022826 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022827find_var_in_ht(
22828 hashtab_T *ht,
22829 int htname,
22830 char_u *varname,
22831 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022832{
Bram Moolenaar33570922005-01-25 22:26:29 +000022833 hashitem_T *hi;
22834
22835 if (*varname == NUL)
22836 {
22837 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022838 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022839 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022840 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022841 case 'g': return &globvars_var;
22842 case 'v': return &vimvars_var;
22843 case 'b': return &curbuf->b_bufvar;
22844 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022845#ifdef FEAT_WINDOWS
22846 case 't': return &curtab->tp_winvar;
22847#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022848 case 'l': return current_funccal == NULL
22849 ? NULL : &current_funccal->l_vars_var;
22850 case 'a': return current_funccal == NULL
22851 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022852 }
22853 return NULL;
22854 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022855
22856 hi = hash_find(ht, varname);
22857 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022858 {
22859 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022860 * worked find the variable again. Don't auto-load a script if it was
22861 * loaded already, otherwise it would be loaded every time when
22862 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022863 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022864 {
22865 /* Note: script_autoload() may make "hi" invalid. It must either
22866 * be obtained again or not used. */
22867 if (!script_autoload(varname, FALSE) || aborting())
22868 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022869 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022870 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022871 if (HASHITEM_EMPTY(hi))
22872 return NULL;
22873 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022875}
22876
22877/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022878 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022879 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022880 * Set "varname" to the start of name without ':'.
22881 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022883find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022884{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022885 hashitem_T *hi;
22886
Bram Moolenaar73627d02015-08-11 15:46:09 +020022887 if (name[0] == NUL)
22888 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889 if (name[1] != ':')
22890 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022891 /* The name must not start with a colon or #. */
22892 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 return NULL;
22894 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022895
22896 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022897 hi = hash_find(&compat_hashtab, name);
22898 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022899 return &compat_hashtab;
22900
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022902 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022903 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 }
22905 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022906 if (*name == 'g') /* global variable */
22907 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022908 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22909 */
22910 if (vim_strchr(name + 2, ':') != NULL
22911 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022912 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022913 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022914 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022915 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022916 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022917#ifdef FEAT_WINDOWS
22918 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022919 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022920#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022921 if (*name == 'v') /* v: variable */
22922 return &vimvarht;
22923 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022924 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022925 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022926 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 if (*name == 's' /* script variable */
22928 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22929 return &SCRIPT_VARS(current_SID);
22930 return NULL;
22931}
22932
22933/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022934 * Get function call environment based on bactrace debug level
22935 */
22936 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022937get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022938{
22939 int i;
22940 funccall_T *funccal;
22941 funccall_T *temp_funccal;
22942
22943 funccal = current_funccal;
22944 if (debug_backtrace_level > 0)
22945 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022946 for (i = 0; i < debug_backtrace_level; i++)
22947 {
22948 temp_funccal = funccal->caller;
22949 if (temp_funccal)
22950 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022951 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022952 /* backtrace level overflow. reset to max */
22953 debug_backtrace_level = i;
22954 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022955 }
22956 return funccal;
22957}
22958
22959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022961 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 * Returns NULL when it doesn't exist.
22963 */
22964 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022965get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022966{
Bram Moolenaar33570922005-01-25 22:26:29 +000022967 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022968
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022969 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 if (v == NULL)
22971 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022972 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973}
22974
22975/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 * sourcing this script and when executing functions defined in the script.
22978 */
22979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022980new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981{
Bram Moolenaara7043832005-01-21 11:56:39 +000022982 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022983 hashtab_T *ht;
22984 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022985
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22987 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022988 /* Re-allocating ga_data means that an ht_array pointing to
22989 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022990 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022991 for (i = 1; i <= ga_scripts.ga_len; ++i)
22992 {
22993 ht = &SCRIPT_VARS(i);
22994 if (ht->ht_mask == HT_INIT_SIZE - 1)
22995 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022996 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022998 }
22999
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000 while (ga_scripts.ga_len < id)
23001 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023002 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023003 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023004 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 }
23007 }
23008}
23009
23010/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23012 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023013 */
23014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023015init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016{
Bram Moolenaar33570922005-01-25 22:26:29 +000023017 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023018 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023019 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023020 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023021 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 dict_var->di_tv.vval.v_dict = dict;
23023 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023024 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23026 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027}
23028
23029/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023030 * Unreference a dictionary initialized by init_var_dict().
23031 */
23032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023033unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023034{
23035 /* Now the dict needs to be freed if no one else is using it, go back to
23036 * normal reference counting. */
23037 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23038 dict_unref(dict);
23039}
23040
23041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023042 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023043 * Frees all allocated variables and the value they contain.
23044 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 */
23046 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023047vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023048{
23049 vars_clear_ext(ht, TRUE);
23050}
23051
23052/*
23053 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23054 */
23055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023056vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057{
Bram Moolenaara7043832005-01-21 11:56:39 +000023058 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023059 hashitem_T *hi;
23060 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061
Bram Moolenaar33570922005-01-25 22:26:29 +000023062 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023063 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023064 for (hi = ht->ht_array; todo > 0; ++hi)
23065 {
23066 if (!HASHITEM_EMPTY(hi))
23067 {
23068 --todo;
23069
Bram Moolenaar33570922005-01-25 22:26:29 +000023070 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023071 * ht_array might change then. hash_clear() takes care of it
23072 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023073 v = HI2DI(hi);
23074 if (free_val)
23075 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023076 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023077 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023078 }
23079 }
23080 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023081 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082}
23083
Bram Moolenaara7043832005-01-21 11:56:39 +000023084/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023085 * Delete a variable from hashtab "ht" at item "hi".
23086 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023089delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090{
Bram Moolenaar33570922005-01-25 22:26:29 +000023091 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023092
23093 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023094 clear_tv(&di->di_tv);
23095 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023096}
23097
23098/*
23099 * List the value of one internal variable.
23100 */
23101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023102list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023104 char_u *tofree;
23105 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023106 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023107
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023108 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023109 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023110 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023111 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112}
23113
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023115list_one_var_a(
23116 char_u *prefix,
23117 char_u *name,
23118 int type,
23119 char_u *string,
23120 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121{
Bram Moolenaar31859182007-08-14 20:41:13 +000023122 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23123 msg_start();
23124 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 if (name != NULL) /* "a:" vars don't have a name stored */
23126 msg_puts(name);
23127 msg_putchar(' ');
23128 msg_advance(22);
23129 if (type == VAR_NUMBER)
23130 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023131 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023132 msg_putchar('*');
23133 else if (type == VAR_LIST)
23134 {
23135 msg_putchar('[');
23136 if (*string == '[')
23137 ++string;
23138 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023139 else if (type == VAR_DICT)
23140 {
23141 msg_putchar('{');
23142 if (*string == '{')
23143 ++string;
23144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 else
23146 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023147
Bram Moolenaar071d4272004-06-13 20:20:40 +000023148 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023149
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023150 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023151 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023152 if (*first)
23153 {
23154 msg_clr_eos();
23155 *first = FALSE;
23156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157}
23158
23159/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023160 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 * If the variable already exists, the value is updated.
23162 * Otherwise the variable is created.
23163 */
23164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023165set_var(
23166 char_u *name,
23167 typval_T *tv,
23168 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169{
Bram Moolenaar33570922005-01-25 22:26:29 +000023170 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023172 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023174 ht = find_var_ht(name, &varname);
23175 if (ht == NULL || *varname == NUL)
23176 {
23177 EMSG2(_(e_illvar), name);
23178 return;
23179 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023180 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023181
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023182 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23183 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023184 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023185
Bram Moolenaar33570922005-01-25 22:26:29 +000023186 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023188 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023189 if (var_check_ro(v->di_flags, name, FALSE)
23190 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023191 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023192
23193 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023194 * Handle setting internal v: variables separately where needed to
23195 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023196 */
23197 if (ht == &vimvarht)
23198 {
23199 if (v->di_tv.v_type == VAR_STRING)
23200 {
23201 vim_free(v->di_tv.vval.v_string);
23202 if (copy || tv->v_type != VAR_STRING)
23203 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23204 else
23205 {
23206 /* Take over the string to avoid an extra alloc/free. */
23207 v->di_tv.vval.v_string = tv->vval.v_string;
23208 tv->vval.v_string = NULL;
23209 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023210 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023212 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023213 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023214 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023215 if (STRCMP(varname, "searchforward") == 0)
23216 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023217#ifdef FEAT_SEARCH_EXTRA
23218 else if (STRCMP(varname, "hlsearch") == 0)
23219 {
23220 no_hlsearch = !v->di_tv.vval.v_number;
23221 redraw_all_later(SOME_VALID);
23222 }
23223#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023224 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023225 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023226 else if (v->di_tv.v_type != tv->v_type)
23227 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023228 }
23229
23230 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 }
23232 else /* add a new variable */
23233 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023234 /* Can't add "v:" variable. */
23235 if (ht == &vimvarht)
23236 {
23237 EMSG2(_(e_illvar), name);
23238 return;
23239 }
23240
Bram Moolenaar92124a32005-06-17 22:03:40 +000023241 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023242 if (!valid_varname(varname))
23243 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023244
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023245 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23246 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023247 if (v == NULL)
23248 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023249 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023252 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 return;
23254 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023255 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023257
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023258 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023259 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023260 else
23261 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023262 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023263 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023264 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023266}
23267
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023268/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023269 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023270 * Also give an error message.
23271 */
23272 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023273var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023274{
23275 if (flags & DI_FLAGS_RO)
23276 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023277 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023278 return TRUE;
23279 }
23280 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23281 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023282 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023283 return TRUE;
23284 }
23285 return FALSE;
23286}
23287
23288/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023289 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23290 * Also give an error message.
23291 */
23292 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023293var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023294{
23295 if (flags & DI_FLAGS_FIX)
23296 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023297 EMSG2(_("E795: Cannot delete variable %s"),
23298 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023299 return TRUE;
23300 }
23301 return FALSE;
23302}
23303
23304/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023305 * Check if a funcref is assigned to a valid variable name.
23306 * Return TRUE and give an error if not.
23307 */
23308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023309var_check_func_name(
23310 char_u *name, /* points to start of variable name */
23311 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023312{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023313 /* Allow for w: b: s: and t:. */
23314 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023315 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23316 ? name[2] : name[0]))
23317 {
23318 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23319 name);
23320 return TRUE;
23321 }
23322 /* Don't allow hiding a function. When "v" is not NULL we might be
23323 * assigning another function to the same var, the type is checked
23324 * below. */
23325 if (new_var && function_exists(name))
23326 {
23327 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23328 name);
23329 return TRUE;
23330 }
23331 return FALSE;
23332}
23333
23334/*
23335 * Check if a variable name is valid.
23336 * Return FALSE and give an error if not.
23337 */
23338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023339valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023340{
23341 char_u *p;
23342
23343 for (p = varname; *p != NUL; ++p)
23344 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23345 && *p != AUTOLOAD_CHAR)
23346 {
23347 EMSG2(_(e_illvar), varname);
23348 return FALSE;
23349 }
23350 return TRUE;
23351}
23352
23353/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023354 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023355 * Also give an error message, using "name" or _("name") when use_gettext is
23356 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023357 */
23358 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023359tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023360{
23361 if (lock & VAR_LOCKED)
23362 {
23363 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023364 name == NULL ? (char_u *)_("Unknown")
23365 : use_gettext ? (char_u *)_(name)
23366 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023367 return TRUE;
23368 }
23369 if (lock & VAR_FIXED)
23370 {
23371 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023372 name == NULL ? (char_u *)_("Unknown")
23373 : use_gettext ? (char_u *)_(name)
23374 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023375 return TRUE;
23376 }
23377 return FALSE;
23378}
23379
23380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023381 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023382 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023383 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023384 * It is OK for "from" and "to" to point to the same item. This is used to
23385 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023386 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023387 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023390 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023391 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023392 switch (from->v_type)
23393 {
23394 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023395 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023396 to->vval.v_number = from->vval.v_number;
23397 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023398 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023399#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023400 to->vval.v_float = from->vval.v_float;
23401 break;
23402#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023403 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023404#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023405 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023406 if (to->vval.v_job != NULL)
23407 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023408 break;
23409#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023410 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023411#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023412 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023413 if (to->vval.v_channel != NULL)
23414 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023415 break;
23416#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023417 case VAR_STRING:
23418 case VAR_FUNC:
23419 if (from->vval.v_string == NULL)
23420 to->vval.v_string = NULL;
23421 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023423 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023424 if (from->v_type == VAR_FUNC)
23425 func_ref(to->vval.v_string);
23426 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023427 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023428 case VAR_PARTIAL:
23429 if (from->vval.v_partial == NULL)
23430 to->vval.v_partial = NULL;
23431 else
23432 {
23433 to->vval.v_partial = from->vval.v_partial;
23434 ++to->vval.v_partial->pt_refcount;
23435 }
23436 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023437 case VAR_LIST:
23438 if (from->vval.v_list == NULL)
23439 to->vval.v_list = NULL;
23440 else
23441 {
23442 to->vval.v_list = from->vval.v_list;
23443 ++to->vval.v_list->lv_refcount;
23444 }
23445 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023446 case VAR_DICT:
23447 if (from->vval.v_dict == NULL)
23448 to->vval.v_dict = NULL;
23449 else
23450 {
23451 to->vval.v_dict = from->vval.v_dict;
23452 ++to->vval.v_dict->dv_refcount;
23453 }
23454 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023455 case VAR_UNKNOWN:
23456 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023457 break;
23458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459}
23460
23461/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023462 * Make a copy of an item.
23463 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023464 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23465 * reference to an already copied list/dict can be used.
23466 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023467 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023469item_copy(
23470 typval_T *from,
23471 typval_T *to,
23472 int deep,
23473 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023474{
23475 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023476 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023477
Bram Moolenaar33570922005-01-25 22:26:29 +000023478 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023479 {
23480 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023481 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023482 }
23483 ++recurse;
23484
23485 switch (from->v_type)
23486 {
23487 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023488 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023489 case VAR_STRING:
23490 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023491 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023492 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023493 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023494 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023495 copy_tv(from, to);
23496 break;
23497 case VAR_LIST:
23498 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023499 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023500 if (from->vval.v_list == NULL)
23501 to->vval.v_list = NULL;
23502 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23503 {
23504 /* use the copy made earlier */
23505 to->vval.v_list = from->vval.v_list->lv_copylist;
23506 ++to->vval.v_list->lv_refcount;
23507 }
23508 else
23509 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23510 if (to->vval.v_list == NULL)
23511 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023512 break;
23513 case VAR_DICT:
23514 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023515 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023516 if (from->vval.v_dict == NULL)
23517 to->vval.v_dict = NULL;
23518 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23519 {
23520 /* use the copy made earlier */
23521 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23522 ++to->vval.v_dict->dv_refcount;
23523 }
23524 else
23525 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23526 if (to->vval.v_dict == NULL)
23527 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023528 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023529 case VAR_UNKNOWN:
23530 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023531 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023532 }
23533 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023534 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023535}
23536
23537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 * ":echo expr1 ..." print each argument separated with a space, add a
23539 * newline at the end.
23540 * ":echon expr1 ..." print each argument plain.
23541 */
23542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023543ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023544{
23545 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023546 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023547 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 char_u *p;
23549 int needclr = TRUE;
23550 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023551 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552
23553 if (eap->skip)
23554 ++emsg_skip;
23555 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23556 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023557 /* If eval1() causes an error message the text from the command may
23558 * still need to be cleared. E.g., "echo 22,44". */
23559 need_clr_eos = needclr;
23560
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023562 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 {
23564 /*
23565 * Report the invalid expression unless the expression evaluation
23566 * has been cancelled due to an aborting error, an interrupt, or an
23567 * exception.
23568 */
23569 if (!aborting())
23570 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023571 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 break;
23573 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023574 need_clr_eos = FALSE;
23575
Bram Moolenaar071d4272004-06-13 20:20:40 +000023576 if (!eap->skip)
23577 {
23578 if (atstart)
23579 {
23580 atstart = FALSE;
23581 /* Call msg_start() after eval1(), evaluating the expression
23582 * may cause a message to appear. */
23583 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023584 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023585 /* Mark the saved text as finishing the line, so that what
23586 * follows is displayed on a new line when scrolling back
23587 * at the more prompt. */
23588 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 }
23592 else if (eap->cmdidx == CMD_echo)
23593 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023594 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023595 if (p != NULL)
23596 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023598 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023600 if (*p != TAB && needclr)
23601 {
23602 /* remove any text still there from the command */
23603 msg_clr_eos();
23604 needclr = FALSE;
23605 }
23606 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023607 }
23608 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023609 {
23610#ifdef FEAT_MBYTE
23611 if (has_mbyte)
23612 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023613 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023614
23615 (void)msg_outtrans_len_attr(p, i, echo_attr);
23616 p += i - 1;
23617 }
23618 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023619#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023620 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023623 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023625 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023626 arg = skipwhite(arg);
23627 }
23628 eap->nextcmd = check_nextcmd(arg);
23629
23630 if (eap->skip)
23631 --emsg_skip;
23632 else
23633 {
23634 /* remove text that may still be there from the command */
23635 if (needclr)
23636 msg_clr_eos();
23637 if (eap->cmdidx == CMD_echo)
23638 msg_end();
23639 }
23640}
23641
23642/*
23643 * ":echohl {name}".
23644 */
23645 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023646ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647{
23648 int id;
23649
23650 id = syn_name2id(eap->arg);
23651 if (id == 0)
23652 echo_attr = 0;
23653 else
23654 echo_attr = syn_id2attr(id);
23655}
23656
23657/*
23658 * ":execute expr1 ..." execute the result of an expression.
23659 * ":echomsg expr1 ..." Print a message
23660 * ":echoerr expr1 ..." Print an error
23661 * Each gets spaces around each argument and a newline at the end for
23662 * echo commands
23663 */
23664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023665ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023666{
23667 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023668 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 int ret = OK;
23670 char_u *p;
23671 garray_T ga;
23672 int len;
23673 int save_did_emsg;
23674
23675 ga_init2(&ga, 1, 80);
23676
23677 if (eap->skip)
23678 ++emsg_skip;
23679 while (*arg != NUL && *arg != '|' && *arg != '\n')
23680 {
23681 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023682 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 {
23684 /*
23685 * Report the invalid expression unless the expression evaluation
23686 * has been cancelled due to an aborting error, an interrupt, or an
23687 * exception.
23688 */
23689 if (!aborting())
23690 EMSG2(_(e_invexpr2), p);
23691 ret = FAIL;
23692 break;
23693 }
23694
23695 if (!eap->skip)
23696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023697 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 len = (int)STRLEN(p);
23699 if (ga_grow(&ga, len + 2) == FAIL)
23700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023701 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 ret = FAIL;
23703 break;
23704 }
23705 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023708 ga.ga_len += len;
23709 }
23710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023711 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 arg = skipwhite(arg);
23713 }
23714
23715 if (ret != FAIL && ga.ga_data != NULL)
23716 {
23717 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023718 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023720 out_flush();
23721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 else if (eap->cmdidx == CMD_echoerr)
23723 {
23724 /* We don't want to abort following commands, restore did_emsg. */
23725 save_did_emsg = did_emsg;
23726 EMSG((char_u *)ga.ga_data);
23727 if (!force_abort)
23728 did_emsg = save_did_emsg;
23729 }
23730 else if (eap->cmdidx == CMD_execute)
23731 do_cmdline((char_u *)ga.ga_data,
23732 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23733 }
23734
23735 ga_clear(&ga);
23736
23737 if (eap->skip)
23738 --emsg_skip;
23739
23740 eap->nextcmd = check_nextcmd(arg);
23741}
23742
23743/*
23744 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23745 * "arg" points to the "&" or '+' when called, to "option" when returning.
23746 * Returns NULL when no option name found. Otherwise pointer to the char
23747 * after the option name.
23748 */
23749 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023750find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751{
23752 char_u *p = *arg;
23753
23754 ++p;
23755 if (*p == 'g' && p[1] == ':')
23756 {
23757 *opt_flags = OPT_GLOBAL;
23758 p += 2;
23759 }
23760 else if (*p == 'l' && p[1] == ':')
23761 {
23762 *opt_flags = OPT_LOCAL;
23763 p += 2;
23764 }
23765 else
23766 *opt_flags = 0;
23767
23768 if (!ASCII_ISALPHA(*p))
23769 return NULL;
23770 *arg = p;
23771
23772 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23773 p += 4; /* termcap option */
23774 else
23775 while (ASCII_ISALPHA(*p))
23776 ++p;
23777 return p;
23778}
23779
23780/*
23781 * ":function"
23782 */
23783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023784ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785{
23786 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023787 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 int j;
23789 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023791 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 char_u *name = NULL;
23793 char_u *p;
23794 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023795 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023796 garray_T newargs;
23797 garray_T newlines;
23798 int varargs = FALSE;
23799 int mustend = FALSE;
23800 int flags = 0;
23801 ufunc_T *fp;
23802 int indent;
23803 int nesting;
23804 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023805 dictitem_T *v;
23806 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023807 static int func_nr = 0; /* number for nameless function */
23808 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023809 hashtab_T *ht;
23810 int todo;
23811 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023812 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813
23814 /*
23815 * ":function" without argument: list functions.
23816 */
23817 if (ends_excmd(*eap->arg))
23818 {
23819 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023820 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023821 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023822 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023823 {
23824 if (!HASHITEM_EMPTY(hi))
23825 {
23826 --todo;
23827 fp = HI2UF(hi);
23828 if (!isdigit(*fp->uf_name))
23829 list_func_head(fp, FALSE);
23830 }
23831 }
23832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833 eap->nextcmd = check_nextcmd(eap->arg);
23834 return;
23835 }
23836
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023837 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023838 * ":function /pat": list functions matching pattern.
23839 */
23840 if (*eap->arg == '/')
23841 {
23842 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23843 if (!eap->skip)
23844 {
23845 regmatch_T regmatch;
23846
23847 c = *p;
23848 *p = NUL;
23849 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23850 *p = c;
23851 if (regmatch.regprog != NULL)
23852 {
23853 regmatch.rm_ic = p_ic;
23854
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023855 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023856 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23857 {
23858 if (!HASHITEM_EMPTY(hi))
23859 {
23860 --todo;
23861 fp = HI2UF(hi);
23862 if (!isdigit(*fp->uf_name)
23863 && vim_regexec(&regmatch, fp->uf_name, 0))
23864 list_func_head(fp, FALSE);
23865 }
23866 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023867 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023868 }
23869 }
23870 if (*p == '/')
23871 ++p;
23872 eap->nextcmd = check_nextcmd(p);
23873 return;
23874 }
23875
23876 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 * Get the function name. There are these situations:
23878 * func normal function name
23879 * "name" == func, "fudi.fd_dict" == NULL
23880 * dict.func new dictionary entry
23881 * "name" == NULL, "fudi.fd_dict" set,
23882 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23883 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023884 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023885 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23886 * dict.func existing dict entry that's not a Funcref
23887 * "name" == NULL, "fudi.fd_dict" set,
23888 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023889 * s:func script-local function name
23890 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023893 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023894 paren = (vim_strchr(p, '(') != NULL);
23895 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 {
23897 /*
23898 * Return on an invalid expression in braces, unless the expression
23899 * evaluation has been cancelled due to an aborting error, an
23900 * interrupt, or an exception.
23901 */
23902 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023903 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023904 if (!eap->skip && fudi.fd_newkey != NULL)
23905 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023907 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 else
23910 eap->skip = TRUE;
23911 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023912
Bram Moolenaar071d4272004-06-13 20:20:40 +000023913 /* An error in a function call during evaluation of an expression in magic
23914 * braces should not cause the function not to be defined. */
23915 saved_did_emsg = did_emsg;
23916 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917
23918 /*
23919 * ":function func" with only function name: list function.
23920 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023921 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 {
23923 if (!ends_excmd(*skipwhite(p)))
23924 {
23925 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023926 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 }
23928 eap->nextcmd = check_nextcmd(p);
23929 if (eap->nextcmd != NULL)
23930 *p = NUL;
23931 if (!eap->skip && !got_int)
23932 {
23933 fp = find_func(name);
23934 if (fp != NULL)
23935 {
23936 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023937 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023939 if (FUNCLINE(fp, j) == NULL)
23940 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 msg_putchar('\n');
23942 msg_outnum((long)(j + 1));
23943 if (j < 9)
23944 msg_putchar(' ');
23945 if (j < 99)
23946 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023947 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023948 out_flush(); /* show a line at a time */
23949 ui_breakcheck();
23950 }
23951 if (!got_int)
23952 {
23953 msg_putchar('\n');
23954 msg_puts((char_u *)" endfunction");
23955 }
23956 }
23957 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023958 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023960 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 }
23962
23963 /*
23964 * ":function name(arg1, arg2)" Define function.
23965 */
23966 p = skipwhite(p);
23967 if (*p != '(')
23968 {
23969 if (!eap->skip)
23970 {
23971 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023972 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 }
23974 /* attempt to continue by skipping some text */
23975 if (vim_strchr(p, '(') != NULL)
23976 p = vim_strchr(p, '(');
23977 }
23978 p = skipwhite(p + 1);
23979
23980 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23981 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23982
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023983 if (!eap->skip)
23984 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023985 /* Check the name of the function. Unless it's a dictionary function
23986 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023987 if (name != NULL)
23988 arg = name;
23989 else
23990 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023991 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023992 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23993 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023994 {
23995 if (*arg == K_SPECIAL)
23996 j = 3;
23997 else
23998 j = 0;
23999 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24000 : eval_isnamec(arg[j])))
24001 ++j;
24002 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024003 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024004 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024005 /* Disallow using the g: dict. */
24006 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24007 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024008 }
24009
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 /*
24011 * Isolate the arguments: "arg1, arg2, ...)"
24012 */
24013 while (*p != ')')
24014 {
24015 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24016 {
24017 varargs = TRUE;
24018 p += 3;
24019 mustend = TRUE;
24020 }
24021 else
24022 {
24023 arg = p;
24024 while (ASCII_ISALNUM(*p) || *p == '_')
24025 ++p;
24026 if (arg == p || isdigit(*arg)
24027 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24028 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24029 {
24030 if (!eap->skip)
24031 EMSG2(_("E125: Illegal argument: %s"), arg);
24032 break;
24033 }
24034 if (ga_grow(&newargs, 1) == FAIL)
24035 goto erret;
24036 c = *p;
24037 *p = NUL;
24038 arg = vim_strsave(arg);
24039 if (arg == NULL)
24040 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024041
24042 /* Check for duplicate argument name. */
24043 for (i = 0; i < newargs.ga_len; ++i)
24044 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24045 {
24046 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024047 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024048 goto erret;
24049 }
24050
Bram Moolenaar071d4272004-06-13 20:20:40 +000024051 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24052 *p = c;
24053 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024054 if (*p == ',')
24055 ++p;
24056 else
24057 mustend = TRUE;
24058 }
24059 p = skipwhite(p);
24060 if (mustend && *p != ')')
24061 {
24062 if (!eap->skip)
24063 EMSG2(_(e_invarg2), eap->arg);
24064 break;
24065 }
24066 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024067 if (*p != ')')
24068 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 ++p; /* skip the ')' */
24070
Bram Moolenaare9a41262005-01-15 22:18:47 +000024071 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024072 for (;;)
24073 {
24074 p = skipwhite(p);
24075 if (STRNCMP(p, "range", 5) == 0)
24076 {
24077 flags |= FC_RANGE;
24078 p += 5;
24079 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024080 else if (STRNCMP(p, "dict", 4) == 0)
24081 {
24082 flags |= FC_DICT;
24083 p += 4;
24084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024085 else if (STRNCMP(p, "abort", 5) == 0)
24086 {
24087 flags |= FC_ABORT;
24088 p += 5;
24089 }
24090 else
24091 break;
24092 }
24093
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024094 /* When there is a line break use what follows for the function body.
24095 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24096 if (*p == '\n')
24097 line_arg = p + 1;
24098 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024099 EMSG(_(e_trailing));
24100
24101 /*
24102 * Read the body of the function, until ":endfunction" is found.
24103 */
24104 if (KeyTyped)
24105 {
24106 /* Check if the function already exists, don't let the user type the
24107 * whole function before telling him it doesn't work! For a script we
24108 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024109 if (!eap->skip && !eap->forceit)
24110 {
24111 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24112 EMSG(_(e_funcdict));
24113 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024114 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024116
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024117 if (!eap->skip && did_emsg)
24118 goto erret;
24119
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 msg_putchar('\n'); /* don't overwrite the function name */
24121 cmdline_row = msg_row;
24122 }
24123
24124 indent = 2;
24125 nesting = 0;
24126 for (;;)
24127 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024128 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024129 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024130 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024131 saved_wait_return = FALSE;
24132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024134 sourcing_lnum_off = sourcing_lnum;
24135
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024136 if (line_arg != NULL)
24137 {
24138 /* Use eap->arg, split up in parts by line breaks. */
24139 theline = line_arg;
24140 p = vim_strchr(theline, '\n');
24141 if (p == NULL)
24142 line_arg += STRLEN(line_arg);
24143 else
24144 {
24145 *p = NUL;
24146 line_arg = p + 1;
24147 }
24148 }
24149 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 theline = getcmdline(':', 0L, indent);
24151 else
24152 theline = eap->getline(':', eap->cookie, indent);
24153 if (KeyTyped)
24154 lines_left = Rows - 1;
24155 if (theline == NULL)
24156 {
24157 EMSG(_("E126: Missing :endfunction"));
24158 goto erret;
24159 }
24160
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024161 /* Detect line continuation: sourcing_lnum increased more than one. */
24162 if (sourcing_lnum > sourcing_lnum_off + 1)
24163 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24164 else
24165 sourcing_lnum_off = 0;
24166
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 if (skip_until != NULL)
24168 {
24169 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24170 * don't check for ":endfunc". */
24171 if (STRCMP(theline, skip_until) == 0)
24172 {
24173 vim_free(skip_until);
24174 skip_until = NULL;
24175 }
24176 }
24177 else
24178 {
24179 /* skip ':' and blanks*/
24180 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24181 ;
24182
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024183 /* Check for "endfunction". */
24184 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024185 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024186 if (line_arg == NULL)
24187 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 break;
24189 }
24190
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024191 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024192 * at "end". */
24193 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24194 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024195 else if (STRNCMP(p, "if", 2) == 0
24196 || STRNCMP(p, "wh", 2) == 0
24197 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024198 || STRNCMP(p, "try", 3) == 0)
24199 indent += 2;
24200
24201 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024202 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024203 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024204 if (*p == '!')
24205 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024207 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024208 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024209 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024210 ++nesting;
24211 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 }
24213 }
24214
24215 /* Check for ":append" or ":insert". */
24216 p = skip_range(p, NULL);
24217 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24218 || (p[0] == 'i'
24219 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24220 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24221 skip_until = vim_strsave((char_u *)".");
24222
24223 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24224 arg = skipwhite(skiptowhite(p));
24225 if (arg[0] == '<' && arg[1] =='<'
24226 && ((p[0] == 'p' && p[1] == 'y'
24227 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24228 || (p[0] == 'p' && p[1] == 'e'
24229 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24230 || (p[0] == 't' && p[1] == 'c'
24231 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024232 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24233 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24235 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024236 || (p[0] == 'm' && p[1] == 'z'
24237 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 ))
24239 {
24240 /* ":python <<" continues until a dot, like ":append" */
24241 p = skipwhite(arg + 2);
24242 if (*p == NUL)
24243 skip_until = vim_strsave((char_u *)".");
24244 else
24245 skip_until = vim_strsave(p);
24246 }
24247 }
24248
24249 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024250 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024251 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024252 if (line_arg == NULL)
24253 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024254 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024255 }
24256
24257 /* Copy the line to newly allocated memory. get_one_sourceline()
24258 * allocates 250 bytes per line, this saves 80% on average. The cost
24259 * is an extra alloc/free. */
24260 p = vim_strsave(theline);
24261 if (p != NULL)
24262 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024263 if (line_arg == NULL)
24264 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024265 theline = p;
24266 }
24267
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024268 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24269
24270 /* Add NULL lines for continuation lines, so that the line count is
24271 * equal to the index in the growarray. */
24272 while (sourcing_lnum_off-- > 0)
24273 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024274
24275 /* Check for end of eap->arg. */
24276 if (line_arg != NULL && *line_arg == NUL)
24277 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024278 }
24279
24280 /* Don't define the function when skipping commands or when an error was
24281 * detected. */
24282 if (eap->skip || did_emsg)
24283 goto erret;
24284
24285 /*
24286 * If there are no errors, add the function
24287 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024288 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024289 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024290 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024291 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024292 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024293 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024294 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024295 goto erret;
24296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024297
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024298 fp = find_func(name);
24299 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024300 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024301 if (!eap->forceit)
24302 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024303 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024304 goto erret;
24305 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024306 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024307 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024308 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024309 name);
24310 goto erret;
24311 }
24312 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024313 ga_clear_strings(&(fp->uf_args));
24314 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024315 vim_free(name);
24316 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024318 }
24319 else
24320 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024321 char numbuf[20];
24322
24323 fp = NULL;
24324 if (fudi.fd_newkey == NULL && !eap->forceit)
24325 {
24326 EMSG(_(e_funcdict));
24327 goto erret;
24328 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024329 if (fudi.fd_di == NULL)
24330 {
24331 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024332 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024333 goto erret;
24334 }
24335 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024336 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024337 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024338
24339 /* Give the function a sequential number. Can only be used with a
24340 * Funcref! */
24341 vim_free(name);
24342 sprintf(numbuf, "%d", ++func_nr);
24343 name = vim_strsave((char_u *)numbuf);
24344 if (name == NULL)
24345 goto erret;
24346 }
24347
24348 if (fp == NULL)
24349 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024350 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024351 {
24352 int slen, plen;
24353 char_u *scriptname;
24354
24355 /* Check that the autoload name matches the script name. */
24356 j = FAIL;
24357 if (sourcing_name != NULL)
24358 {
24359 scriptname = autoload_name(name);
24360 if (scriptname != NULL)
24361 {
24362 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024363 plen = (int)STRLEN(p);
24364 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024365 if (slen > plen && fnamecmp(p,
24366 sourcing_name + slen - plen) == 0)
24367 j = OK;
24368 vim_free(scriptname);
24369 }
24370 }
24371 if (j == FAIL)
24372 {
24373 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24374 goto erret;
24375 }
24376 }
24377
24378 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024379 if (fp == NULL)
24380 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024381
24382 if (fudi.fd_dict != NULL)
24383 {
24384 if (fudi.fd_di == NULL)
24385 {
24386 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024387 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024388 if (fudi.fd_di == NULL)
24389 {
24390 vim_free(fp);
24391 goto erret;
24392 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024393 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24394 {
24395 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024396 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024397 goto erret;
24398 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024399 }
24400 else
24401 /* overwrite existing dict entry */
24402 clear_tv(&fudi.fd_di->di_tv);
24403 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024404 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024405 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024406 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024407
24408 /* behave like "dict" was used */
24409 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024410 }
24411
Bram Moolenaar071d4272004-06-13 20:20:40 +000024412 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024413 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024414 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24415 {
24416 vim_free(fp);
24417 goto erret;
24418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024419 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024420 fp->uf_args = newargs;
24421 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024422#ifdef FEAT_PROFILE
24423 fp->uf_tml_count = NULL;
24424 fp->uf_tml_total = NULL;
24425 fp->uf_tml_self = NULL;
24426 fp->uf_profiling = FALSE;
24427 if (prof_def_func())
24428 func_do_profile(fp);
24429#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024430 fp->uf_varargs = varargs;
24431 fp->uf_flags = flags;
24432 fp->uf_calls = 0;
24433 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024434 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024435
24436erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437 ga_clear_strings(&newargs);
24438 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024439ret_free:
24440 vim_free(skip_until);
24441 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024442 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024443 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024444 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445}
24446
24447/*
24448 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024449 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024450 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024451 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024452 * TFN_INT: internal function name OK
24453 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024454 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024455 * Advances "pp" to just after the function name (if no error).
24456 */
24457 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024458trans_function_name(
24459 char_u **pp,
24460 int skip, /* only find the end, don't evaluate */
24461 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024462 funcdict_T *fdp, /* return: info about dictionary used */
24463 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024464{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024465 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 char_u *start;
24467 char_u *end;
24468 int lead;
24469 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024470 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024471 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024472
24473 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024474 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024475 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024476
24477 /* Check for hard coded <SNR>: already translated function ID (from a user
24478 * command). */
24479 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24480 && (*pp)[2] == (int)KE_SNR)
24481 {
24482 *pp += 3;
24483 len = get_id_len(pp) + 3;
24484 return vim_strnsave(start, len);
24485 }
24486
24487 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24488 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024490 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024492
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024493 /* Note that TFN_ flags use the same values as GLV_ flags. */
24494 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024495 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024496 if (end == start)
24497 {
24498 if (!skip)
24499 EMSG(_("E129: Function name required"));
24500 goto theend;
24501 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024502 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024503 {
24504 /*
24505 * Report an invalid expression in braces, unless the expression
24506 * evaluation has been cancelled due to an aborting error, an
24507 * interrupt, or an exception.
24508 */
24509 if (!aborting())
24510 {
24511 if (end != NULL)
24512 EMSG2(_(e_invarg2), start);
24513 }
24514 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024515 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024516 goto theend;
24517 }
24518
24519 if (lv.ll_tv != NULL)
24520 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024521 if (fdp != NULL)
24522 {
24523 fdp->fd_dict = lv.ll_dict;
24524 fdp->fd_newkey = lv.ll_newkey;
24525 lv.ll_newkey = NULL;
24526 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024527 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024528 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24529 {
24530 name = vim_strsave(lv.ll_tv->vval.v_string);
24531 *pp = end;
24532 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024533 else if (lv.ll_tv->v_type == VAR_PARTIAL
24534 && lv.ll_tv->vval.v_partial != NULL)
24535 {
24536 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24537 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024538 if (partial != NULL)
24539 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024540 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024541 else
24542 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024543 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24544 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024545 EMSG(_(e_funcref));
24546 else
24547 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024548 name = NULL;
24549 }
24550 goto theend;
24551 }
24552
24553 if (lv.ll_name == NULL)
24554 {
24555 /* Error found, but continue after the function name. */
24556 *pp = end;
24557 goto theend;
24558 }
24559
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024560 /* Check if the name is a Funcref. If so, use the value. */
24561 if (lv.ll_exp_name != NULL)
24562 {
24563 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024564 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024565 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024566 if (name == lv.ll_exp_name)
24567 name = NULL;
24568 }
24569 else
24570 {
24571 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024572 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024573 if (name == *pp)
24574 name = NULL;
24575 }
24576 if (name != NULL)
24577 {
24578 name = vim_strsave(name);
24579 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024580 if (STRNCMP(name, "<SNR>", 5) == 0)
24581 {
24582 /* Change "<SNR>" to the byte sequence. */
24583 name[0] = K_SPECIAL;
24584 name[1] = KS_EXTRA;
24585 name[2] = (int)KE_SNR;
24586 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24587 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024588 goto theend;
24589 }
24590
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024591 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024592 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024593 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024594 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24595 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24596 {
24597 /* When there was "s:" already or the name expanded to get a
24598 * leading "s:" then remove it. */
24599 lv.ll_name += 2;
24600 len -= 2;
24601 lead = 2;
24602 }
24603 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024604 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024605 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024606 /* skip over "s:" and "g:" */
24607 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024608 lv.ll_name += 2;
24609 len = (int)(end - lv.ll_name);
24610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024611
24612 /*
24613 * Copy the function name to allocated memory.
24614 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24615 * Accept <SNR>123_name() outside a script.
24616 */
24617 if (skip)
24618 lead = 0; /* do nothing */
24619 else if (lead > 0)
24620 {
24621 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024622 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24623 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024624 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024625 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024626 if (current_SID <= 0)
24627 {
24628 EMSG(_(e_usingsid));
24629 goto theend;
24630 }
24631 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24632 lead += (int)STRLEN(sid_buf);
24633 }
24634 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024635 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024636 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024637 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024638 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024639 goto theend;
24640 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024641 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024642 {
24643 char_u *cp = vim_strchr(lv.ll_name, ':');
24644
24645 if (cp != NULL && cp < end)
24646 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024647 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024648 goto theend;
24649 }
24650 }
24651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024652 name = alloc((unsigned)(len + lead + 1));
24653 if (name != NULL)
24654 {
24655 if (lead > 0)
24656 {
24657 name[0] = K_SPECIAL;
24658 name[1] = KS_EXTRA;
24659 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024660 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024661 STRCPY(name + 3, sid_buf);
24662 }
24663 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024664 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024665 }
24666 *pp = end;
24667
24668theend:
24669 clear_lval(&lv);
24670 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024671}
24672
24673/*
24674 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24675 * Return 2 if "p" starts with "s:".
24676 * Return 0 otherwise.
24677 */
24678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024679eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024681 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24682 * the standard library function. */
24683 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24684 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685 return 5;
24686 if (p[0] == 's' && p[1] == ':')
24687 return 2;
24688 return 0;
24689}
24690
24691/*
24692 * Return TRUE if "p" starts with "<SID>" or "s:".
24693 * Only works if eval_fname_script() returned non-zero for "p"!
24694 */
24695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024696eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697{
24698 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24699}
24700
24701/*
24702 * List the head of the function: "name(arg1, arg2)".
24703 */
24704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024705list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024706{
24707 int j;
24708
24709 msg_start();
24710 if (indent)
24711 MSG_PUTS(" ");
24712 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024713 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 {
24715 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024716 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717 }
24718 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024719 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024721 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024722 {
24723 if (j)
24724 MSG_PUTS(", ");
24725 msg_puts(FUNCARG(fp, j));
24726 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024728 {
24729 if (j)
24730 MSG_PUTS(", ");
24731 MSG_PUTS("...");
24732 }
24733 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024734 if (fp->uf_flags & FC_ABORT)
24735 MSG_PUTS(" abort");
24736 if (fp->uf_flags & FC_RANGE)
24737 MSG_PUTS(" range");
24738 if (fp->uf_flags & FC_DICT)
24739 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024740 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024741 if (p_verbose > 0)
24742 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024743}
24744
24745/*
24746 * Find a function by name, return pointer to it in ufuncs.
24747 * Return NULL for unknown function.
24748 */
24749 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024750find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024752 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024753
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024754 hi = hash_find(&func_hashtab, name);
24755 if (!HASHITEM_EMPTY(hi))
24756 return HI2UF(hi);
24757 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024758}
24759
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024760#if defined(EXITFREE) || defined(PROTO)
24761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024762free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024763{
24764 hashitem_T *hi;
24765
24766 /* Need to start all over every time, because func_free() may change the
24767 * hash table. */
24768 while (func_hashtab.ht_used > 0)
24769 for (hi = func_hashtab.ht_array; ; ++hi)
24770 if (!HASHITEM_EMPTY(hi))
24771 {
24772 func_free(HI2UF(hi));
24773 break;
24774 }
24775}
24776#endif
24777
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024778 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024779translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024780{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024781 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024782 return find_internal_func(name) >= 0;
24783 return find_func(name) != NULL;
24784}
24785
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024786/*
24787 * Return TRUE if a function "name" exists.
24788 */
24789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024790function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024791{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024792 char_u *nm = name;
24793 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024794 int n = FALSE;
24795
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024796 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024797 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024798 nm = skipwhite(nm);
24799
24800 /* Only accept "funcname", "funcname ", "funcname (..." and
24801 * "funcname(...", not "funcname!...". */
24802 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024803 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024804 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024805 return n;
24806}
24807
Bram Moolenaara1544c02013-05-30 12:35:52 +020024808 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024809get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024810{
24811 char_u *nm = name;
24812 char_u *p;
24813
Bram Moolenaar65639032016-03-16 21:40:30 +010024814 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024815
24816 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024817 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024818 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024819
Bram Moolenaara1544c02013-05-30 12:35:52 +020024820 vim_free(p);
24821 return NULL;
24822}
24823
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024824/*
24825 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024826 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24827 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024828 */
24829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024830builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024831{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024832 char_u *p;
24833
24834 if (!ASCII_ISLOWER(name[0]))
24835 return FALSE;
24836 p = vim_strchr(name, AUTOLOAD_CHAR);
24837 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024838}
24839
Bram Moolenaar05159a02005-02-26 23:04:13 +000024840#if defined(FEAT_PROFILE) || defined(PROTO)
24841/*
24842 * Start profiling function "fp".
24843 */
24844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024846{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024847 int len = fp->uf_lines.ga_len;
24848
24849 if (len == 0)
24850 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024851 fp->uf_tm_count = 0;
24852 profile_zero(&fp->uf_tm_self);
24853 profile_zero(&fp->uf_tm_total);
24854 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024855 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024856 if (fp->uf_tml_total == NULL)
24857 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024858 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024859 if (fp->uf_tml_self == NULL)
24860 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024861 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024862 fp->uf_tml_idx = -1;
24863 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24864 || fp->uf_tml_self == NULL)
24865 return; /* out of memory */
24866
24867 fp->uf_profiling = TRUE;
24868}
24869
24870/*
24871 * Dump the profiling results for all functions in file "fd".
24872 */
24873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024874func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024875{
24876 hashitem_T *hi;
24877 int todo;
24878 ufunc_T *fp;
24879 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024880 ufunc_T **sorttab;
24881 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024882
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024883 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024884 if (todo == 0)
24885 return; /* nothing to dump */
24886
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024887 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024888
Bram Moolenaar05159a02005-02-26 23:04:13 +000024889 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24890 {
24891 if (!HASHITEM_EMPTY(hi))
24892 {
24893 --todo;
24894 fp = HI2UF(hi);
24895 if (fp->uf_profiling)
24896 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024897 if (sorttab != NULL)
24898 sorttab[st_len++] = fp;
24899
Bram Moolenaar05159a02005-02-26 23:04:13 +000024900 if (fp->uf_name[0] == K_SPECIAL)
24901 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24902 else
24903 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24904 if (fp->uf_tm_count == 1)
24905 fprintf(fd, "Called 1 time\n");
24906 else
24907 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24908 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24909 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24910 fprintf(fd, "\n");
24911 fprintf(fd, "count total (s) self (s)\n");
24912
24913 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24914 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024915 if (FUNCLINE(fp, i) == NULL)
24916 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024917 prof_func_line(fd, fp->uf_tml_count[i],
24918 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024919 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24920 }
24921 fprintf(fd, "\n");
24922 }
24923 }
24924 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024925
24926 if (sorttab != NULL && st_len > 0)
24927 {
24928 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24929 prof_total_cmp);
24930 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24931 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24932 prof_self_cmp);
24933 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24934 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024935
24936 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024937}
Bram Moolenaar73830342005-02-28 22:48:19 +000024938
24939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024940prof_sort_list(
24941 FILE *fd,
24942 ufunc_T **sorttab,
24943 int st_len,
24944 char *title,
24945 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024946{
24947 int i;
24948 ufunc_T *fp;
24949
24950 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24951 fprintf(fd, "count total (s) self (s) function\n");
24952 for (i = 0; i < 20 && i < st_len; ++i)
24953 {
24954 fp = sorttab[i];
24955 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24956 prefer_self);
24957 if (fp->uf_name[0] == K_SPECIAL)
24958 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24959 else
24960 fprintf(fd, " %s()\n", fp->uf_name);
24961 }
24962 fprintf(fd, "\n");
24963}
24964
24965/*
24966 * Print the count and times for one function or function line.
24967 */
24968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024969prof_func_line(
24970 FILE *fd,
24971 int count,
24972 proftime_T *total,
24973 proftime_T *self,
24974 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024975{
24976 if (count > 0)
24977 {
24978 fprintf(fd, "%5d ", count);
24979 if (prefer_self && profile_equal(total, self))
24980 fprintf(fd, " ");
24981 else
24982 fprintf(fd, "%s ", profile_msg(total));
24983 if (!prefer_self && profile_equal(total, self))
24984 fprintf(fd, " ");
24985 else
24986 fprintf(fd, "%s ", profile_msg(self));
24987 }
24988 else
24989 fprintf(fd, " ");
24990}
24991
24992/*
24993 * Compare function for total time sorting.
24994 */
24995 static int
24996#ifdef __BORLANDC__
24997_RTLENTRYF
24998#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024999prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025000{
25001 ufunc_T *p1, *p2;
25002
25003 p1 = *(ufunc_T **)s1;
25004 p2 = *(ufunc_T **)s2;
25005 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25006}
25007
25008/*
25009 * Compare function for self time sorting.
25010 */
25011 static int
25012#ifdef __BORLANDC__
25013_RTLENTRYF
25014#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025015prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025016{
25017 ufunc_T *p1, *p2;
25018
25019 p1 = *(ufunc_T **)s1;
25020 p2 = *(ufunc_T **)s2;
25021 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25022}
25023
Bram Moolenaar05159a02005-02-26 23:04:13 +000025024#endif
25025
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025026/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025027 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025028 * Return TRUE if a package was loaded.
25029 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025030 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025031script_autoload(
25032 char_u *name,
25033 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025034{
25035 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025036 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025037 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025038 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025039
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025040 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025041 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025042 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025043 return FALSE;
25044
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025045 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025046
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025047 /* Find the name in the list of previously loaded package names. Skip
25048 * "autoload/", it's always the same. */
25049 for (i = 0; i < ga_loaded.ga_len; ++i)
25050 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25051 break;
25052 if (!reload && i < ga_loaded.ga_len)
25053 ret = FALSE; /* was loaded already */
25054 else
25055 {
25056 /* Remember the name if it wasn't loaded already. */
25057 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25058 {
25059 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25060 tofree = NULL;
25061 }
25062
25063 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025064 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025065 ret = TRUE;
25066 }
25067
25068 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025069 return ret;
25070}
25071
25072/*
25073 * Return the autoload script name for a function or variable name.
25074 * Returns NULL when out of memory.
25075 */
25076 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025077autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025078{
25079 char_u *p;
25080 char_u *scriptname;
25081
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025082 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025083 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25084 if (scriptname == NULL)
25085 return FALSE;
25086 STRCPY(scriptname, "autoload/");
25087 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025088 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025089 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025090 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025091 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025092 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025093}
25094
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25096
25097/*
25098 * Function given to ExpandGeneric() to obtain the list of user defined
25099 * function names.
25100 */
25101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025102get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025103{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025104 static long_u done;
25105 static hashitem_T *hi;
25106 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025107
25108 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025110 done = 0;
25111 hi = func_hashtab.ht_array;
25112 }
25113 if (done < func_hashtab.ht_used)
25114 {
25115 if (done++ > 0)
25116 ++hi;
25117 while (HASHITEM_EMPTY(hi))
25118 ++hi;
25119 fp = HI2UF(hi);
25120
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025121 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025122 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025123
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025124 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25125 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126
25127 cat_func_name(IObuff, fp);
25128 if (xp->xp_context != EXPAND_USER_FUNC)
25129 {
25130 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025131 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132 STRCAT(IObuff, ")");
25133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 return IObuff;
25135 }
25136 return NULL;
25137}
25138
25139#endif /* FEAT_CMDL_COMPL */
25140
25141/*
25142 * Copy the function name of "fp" to buffer "buf".
25143 * "buf" must be able to hold the function name plus three bytes.
25144 * Takes care of script-local function names.
25145 */
25146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025147cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025148{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025149 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025150 {
25151 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025152 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025153 }
25154 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025155 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156}
25157
25158/*
25159 * ":delfunction {name}"
25160 */
25161 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025162ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025163{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025164 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025165 char_u *p;
25166 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025167 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168
25169 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025170 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025171 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025172 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025173 {
25174 if (fudi.fd_dict != NULL && !eap->skip)
25175 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025176 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025178 if (!ends_excmd(*skipwhite(p)))
25179 {
25180 vim_free(name);
25181 EMSG(_(e_trailing));
25182 return;
25183 }
25184 eap->nextcmd = check_nextcmd(p);
25185 if (eap->nextcmd != NULL)
25186 *p = NUL;
25187
25188 if (!eap->skip)
25189 fp = find_func(name);
25190 vim_free(name);
25191
25192 if (!eap->skip)
25193 {
25194 if (fp == NULL)
25195 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025196 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 return;
25198 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025199 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 {
25201 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25202 return;
25203 }
25204
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025205 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025207 /* Delete the dict item that refers to the function, it will
25208 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025209 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025211 else
25212 func_free(fp);
25213 }
25214}
25215
25216/*
25217 * Free a function and remove it from the list of functions.
25218 */
25219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025220func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025221{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025222 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025223
25224 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025225 ga_clear_strings(&(fp->uf_args));
25226 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025227#ifdef FEAT_PROFILE
25228 vim_free(fp->uf_tml_count);
25229 vim_free(fp->uf_tml_total);
25230 vim_free(fp->uf_tml_self);
25231#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025232
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025233 /* remove the function from the function hashtable */
25234 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25235 if (HASHITEM_EMPTY(hi))
25236 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025237 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025238 hash_remove(&func_hashtab, hi);
25239
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025240 vim_free(fp);
25241}
25242
25243/*
25244 * Unreference a Function: decrement the reference count and free it when it
25245 * becomes zero. Only for numbered functions.
25246 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025248func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025249{
25250 ufunc_T *fp;
25251
25252 if (name != NULL && isdigit(*name))
25253 {
25254 fp = find_func(name);
25255 if (fp == NULL)
25256 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025257 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025258 {
25259 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025260 * when "uf_calls" becomes zero. */
25261 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025262 func_free(fp);
25263 }
25264 }
25265}
25266
25267/*
25268 * Count a reference to a Function.
25269 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025271func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025272{
25273 ufunc_T *fp;
25274
25275 if (name != NULL && isdigit(*name))
25276 {
25277 fp = find_func(name);
25278 if (fp == NULL)
25279 EMSG2(_(e_intern2), "func_ref()");
25280 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025281 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025282 }
25283}
25284
25285/*
25286 * Call a user function.
25287 */
25288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025289call_user_func(
25290 ufunc_T *fp, /* pointer to function */
25291 int argcount, /* nr of args */
25292 typval_T *argvars, /* arguments */
25293 typval_T *rettv, /* return value */
25294 linenr_T firstline, /* first line of range */
25295 linenr_T lastline, /* last line of range */
25296 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025297{
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 char_u *save_sourcing_name;
25299 linenr_T save_sourcing_lnum;
25300 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025301 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025302 int save_did_emsg;
25303 static int depth = 0;
25304 dictitem_T *v;
25305 int fixvar_idx = 0; /* index in fixvar[] */
25306 int i;
25307 int ai;
25308 char_u numbuf[NUMBUFLEN];
25309 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025310 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025311#ifdef FEAT_PROFILE
25312 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025313 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315
25316 /* If depth of calling is getting too high, don't execute the function */
25317 if (depth >= p_mfd)
25318 {
25319 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025320 rettv->v_type = VAR_NUMBER;
25321 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 return;
25323 }
25324 ++depth;
25325
25326 line_breakcheck(); /* check for CTRL-C hit */
25327
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025328 fc = (funccall_T *)alloc(sizeof(funccall_T));
25329 fc->caller = current_funccal;
25330 current_funccal = fc;
25331 fc->func = fp;
25332 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025333 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025334 fc->linenr = 0;
25335 fc->returned = FALSE;
25336 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025338 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25339 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340
Bram Moolenaar33570922005-01-25 22:26:29 +000025341 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025342 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025343 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25344 * each argument variable and saves a lot of time.
25345 */
25346 /*
25347 * Init l: variables.
25348 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025349 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025350 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025351 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025352 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25353 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025354 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025355 name = v->di_key;
25356 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025357 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025358 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025359 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025360 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025361 v->di_tv.vval.v_dict = selfdict;
25362 ++selfdict->dv_refcount;
25363 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025364
Bram Moolenaar33570922005-01-25 22:26:29 +000025365 /*
25366 * Init a: variables.
25367 * Set a:0 to "argcount".
25368 * Set a:000 to a list with room for the "..." arguments.
25369 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025370 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025371 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025372 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025373 /* Use "name" to avoid a warning from some compiler that checks the
25374 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025375 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025376 name = v->di_key;
25377 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025378 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025379 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025380 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025381 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025382 v->di_tv.vval.v_list = &fc->l_varlist;
25383 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25384 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25385 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025386
25387 /*
25388 * Set a:firstline to "firstline" and a:lastline to "lastline".
25389 * Set a:name to named arguments.
25390 * Set a:N to the "..." arguments.
25391 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025392 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025393 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025394 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025395 (varnumber_T)lastline);
25396 for (i = 0; i < argcount; ++i)
25397 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025398 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025399 if (ai < 0)
25400 /* named argument a:name */
25401 name = FUNCARG(fp, i);
25402 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025403 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025404 /* "..." argument a:1, a:2, etc. */
25405 sprintf((char *)numbuf, "%d", ai + 1);
25406 name = numbuf;
25407 }
25408 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25409 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025410 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025411 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25412 }
25413 else
25414 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025415 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25416 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025417 if (v == NULL)
25418 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025419 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025420 }
25421 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025422 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025423
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025424 /* Note: the values are copied directly to avoid alloc/free.
25425 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025426 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025427 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025428
25429 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25430 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025431 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25432 fc->l_listitems[ai].li_tv = argvars[i];
25433 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025434 }
25435 }
25436
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437 /* Don't redraw while executing the function. */
25438 ++RedrawingDisabled;
25439 save_sourcing_name = sourcing_name;
25440 save_sourcing_lnum = sourcing_lnum;
25441 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025442 /* need space for function name + ("function " + 3) or "[number]" */
25443 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25444 + STRLEN(fp->uf_name) + 20;
25445 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025446 if (sourcing_name != NULL)
25447 {
25448 if (save_sourcing_name != NULL
25449 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025450 sprintf((char *)sourcing_name, "%s[%d]..",
25451 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452 else
25453 STRCPY(sourcing_name, "function ");
25454 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25455
25456 if (p_verbose >= 12)
25457 {
25458 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025459 verbose_enter_scroll();
25460
Bram Moolenaar555b2802005-05-19 21:08:39 +000025461 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025462 if (p_verbose >= 14)
25463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025465 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025466 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025467 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468
25469 msg_puts((char_u *)"(");
25470 for (i = 0; i < argcount; ++i)
25471 {
25472 if (i > 0)
25473 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025474 if (argvars[i].v_type == VAR_NUMBER)
25475 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 else
25477 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025478 /* Do not want errors such as E724 here. */
25479 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025480 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025481 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025482 if (s != NULL)
25483 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025484 if (vim_strsize(s) > MSG_BUF_CLEN)
25485 {
25486 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25487 s = buf;
25488 }
25489 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025490 vim_free(tofree);
25491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 }
25493 }
25494 msg_puts((char_u *)")");
25495 }
25496 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025497
25498 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 --no_wait_return;
25500 }
25501 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025502#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025503 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025504 {
25505 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25506 func_do_profile(fp);
25507 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025508 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025509 {
25510 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025511 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025512 profile_zero(&fp->uf_tm_children);
25513 }
25514 script_prof_save(&wait_start);
25515 }
25516#endif
25517
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025519 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 save_did_emsg = did_emsg;
25521 did_emsg = FALSE;
25522
25523 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025524 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25526
25527 --RedrawingDisabled;
25528
25529 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025530 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025532 clear_tv(rettv);
25533 rettv->v_type = VAR_NUMBER;
25534 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 }
25536
Bram Moolenaar05159a02005-02-26 23:04:13 +000025537#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025538 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025539 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025540 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025541 profile_end(&call_start);
25542 profile_sub_wait(&wait_start, &call_start);
25543 profile_add(&fp->uf_tm_total, &call_start);
25544 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025545 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025546 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025547 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25548 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025549 }
25550 }
25551#endif
25552
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553 /* when being verbose, mention the return value */
25554 if (p_verbose >= 12)
25555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025556 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025557 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025560 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025561 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025562 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025563 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025564 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025566 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025567 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025568 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025569 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025570
Bram Moolenaar555b2802005-05-19 21:08:39 +000025571 /* The value may be very long. Skip the middle part, so that we
25572 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025573 * truncate it at the end. Don't want errors such as E724 here. */
25574 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025575 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025576 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025577 if (s != NULL)
25578 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025579 if (vim_strsize(s) > MSG_BUF_CLEN)
25580 {
25581 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25582 s = buf;
25583 }
25584 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025585 vim_free(tofree);
25586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 }
25588 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025589
25590 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 --no_wait_return;
25592 }
25593
25594 vim_free(sourcing_name);
25595 sourcing_name = save_sourcing_name;
25596 sourcing_lnum = save_sourcing_lnum;
25597 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025598#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025599 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025600 script_prof_restore(&wait_start);
25601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602
25603 if (p_verbose >= 12 && sourcing_name != NULL)
25604 {
25605 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025606 verbose_enter_scroll();
25607
Bram Moolenaar555b2802005-05-19 21:08:39 +000025608 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025610
25611 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 --no_wait_return;
25613 }
25614
25615 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025616 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025618
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025619 /* If the a:000 list and the l: and a: dicts are not referenced we can
25620 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025621 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25622 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25623 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25624 {
25625 free_funccal(fc, FALSE);
25626 }
25627 else
25628 {
25629 hashitem_T *hi;
25630 listitem_T *li;
25631 int todo;
25632
25633 /* "fc" is still in use. This can happen when returning "a:000" or
25634 * assigning "l:" to a global variable.
25635 * Link "fc" in the list for garbage collection later. */
25636 fc->caller = previous_funccal;
25637 previous_funccal = fc;
25638
25639 /* Make a copy of the a: variables, since we didn't do that above. */
25640 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25641 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25642 {
25643 if (!HASHITEM_EMPTY(hi))
25644 {
25645 --todo;
25646 v = HI2DI(hi);
25647 copy_tv(&v->di_tv, &v->di_tv);
25648 }
25649 }
25650
25651 /* Make a copy of the a:000 items, since we didn't do that above. */
25652 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25653 copy_tv(&li->li_tv, &li->li_tv);
25654 }
25655}
25656
25657/*
25658 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025659 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025660 */
25661 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025662can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025663{
25664 return (fc->l_varlist.lv_copyID != copyID
25665 && fc->l_vars.dv_copyID != copyID
25666 && fc->l_avars.dv_copyID != copyID);
25667}
25668
25669/*
25670 * Free "fc" and what it contains.
25671 */
25672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025673free_funccal(
25674 funccall_T *fc,
25675 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025676{
25677 listitem_T *li;
25678
25679 /* The a: variables typevals may not have been allocated, only free the
25680 * allocated variables. */
25681 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25682
25683 /* free all l: variables */
25684 vars_clear(&fc->l_vars.dv_hashtab);
25685
25686 /* Free the a:000 variables if they were allocated. */
25687 if (free_val)
25688 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25689 clear_tv(&li->li_tv);
25690
25691 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692}
25693
25694/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025695 * Add a number variable "name" to dict "dp" with value "nr".
25696 */
25697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025698add_nr_var(
25699 dict_T *dp,
25700 dictitem_T *v,
25701 char *name,
25702 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025703{
25704 STRCPY(v->di_key, name);
25705 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25706 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25707 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025708 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025709 v->di_tv.vval.v_number = nr;
25710}
25711
25712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713 * ":return [expr]"
25714 */
25715 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025716ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025717{
25718 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025719 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720 int returning = FALSE;
25721
25722 if (current_funccal == NULL)
25723 {
25724 EMSG(_("E133: :return not inside a function"));
25725 return;
25726 }
25727
25728 if (eap->skip)
25729 ++emsg_skip;
25730
25731 eap->nextcmd = NULL;
25732 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025733 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 {
25735 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025736 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025737 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025738 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025739 }
25740 /* It's safer to return also on error. */
25741 else if (!eap->skip)
25742 {
25743 /*
25744 * Return unless the expression evaluation has been cancelled due to an
25745 * aborting error, an interrupt, or an exception.
25746 */
25747 if (!aborting())
25748 returning = do_return(eap, FALSE, TRUE, NULL);
25749 }
25750
25751 /* When skipping or the return gets pending, advance to the next command
25752 * in this line (!returning). Otherwise, ignore the rest of the line.
25753 * Following lines will be ignored by get_func_line(). */
25754 if (returning)
25755 eap->nextcmd = NULL;
25756 else if (eap->nextcmd == NULL) /* no argument */
25757 eap->nextcmd = check_nextcmd(arg);
25758
25759 if (eap->skip)
25760 --emsg_skip;
25761}
25762
25763/*
25764 * Return from a function. Possibly makes the return pending. Also called
25765 * for a pending return at the ":endtry" or after returning from an extra
25766 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025767 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025768 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 * FALSE when the return gets pending.
25770 */
25771 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025772do_return(
25773 exarg_T *eap,
25774 int reanimate,
25775 int is_cmd,
25776 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777{
25778 int idx;
25779 struct condstack *cstack = eap->cstack;
25780
25781 if (reanimate)
25782 /* Undo the return. */
25783 current_funccal->returned = FALSE;
25784
25785 /*
25786 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25787 * not in its finally clause (which then is to be executed next) is found.
25788 * In this case, make the ":return" pending for execution at the ":endtry".
25789 * Otherwise, return normally.
25790 */
25791 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25792 if (idx >= 0)
25793 {
25794 cstack->cs_pending[idx] = CSTP_RETURN;
25795
25796 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025797 /* A pending return again gets pending. "rettv" points to an
25798 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025799 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025800 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801 else
25802 {
25803 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025804 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025806 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025808 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 {
25810 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025811 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025812 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813 else
25814 EMSG(_(e_outofmem));
25815 }
25816 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025817 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
25819 if (reanimate)
25820 {
25821 /* The pending return value could be overwritten by a ":return"
25822 * without argument in a finally clause; reset the default
25823 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025824 current_funccal->rettv->v_type = VAR_NUMBER;
25825 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826 }
25827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025828 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 }
25830 else
25831 {
25832 current_funccal->returned = TRUE;
25833
25834 /* If the return is carried out now, store the return value. For
25835 * a return immediately after reanimation, the value is already
25836 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025837 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025839 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025840 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025842 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025843 }
25844 }
25845
25846 return idx < 0;
25847}
25848
25849/*
25850 * Free the variable with a pending return value.
25851 */
25852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025853discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854{
Bram Moolenaar33570922005-01-25 22:26:29 +000025855 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856}
25857
25858/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025859 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025860 * is an allocated string. Used by report_pending() for verbose messages.
25861 */
25862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025863get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025865 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025866 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025867 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025869 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025870 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025871 if (s == NULL)
25872 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025873
25874 STRCPY(IObuff, ":return ");
25875 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25876 if (STRLEN(s) + 8 >= IOSIZE)
25877 STRCPY(IObuff + IOSIZE - 4, "...");
25878 vim_free(tofree);
25879 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880}
25881
25882/*
25883 * Get next function line.
25884 * Called by do_cmdline() to get the next line.
25885 * Returns allocated string, or NULL for end of function.
25886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025888get_func_line(
25889 int c UNUSED,
25890 void *cookie,
25891 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892{
Bram Moolenaar33570922005-01-25 22:26:29 +000025893 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025894 ufunc_T *fp = fcp->func;
25895 char_u *retval;
25896 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897
25898 /* If breakpoints have been added/deleted need to check for it. */
25899 if (fcp->dbg_tick != debug_tick)
25900 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025901 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902 sourcing_lnum);
25903 fcp->dbg_tick = debug_tick;
25904 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025905#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025906 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025907 func_line_end(cookie);
25908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909
Bram Moolenaar05159a02005-02-26 23:04:13 +000025910 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025911 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25912 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 retval = NULL;
25914 else
25915 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025916 /* Skip NULL lines (continuation lines). */
25917 while (fcp->linenr < gap->ga_len
25918 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25919 ++fcp->linenr;
25920 if (fcp->linenr >= gap->ga_len)
25921 retval = NULL;
25922 else
25923 {
25924 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25925 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025926#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025927 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025928 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025929#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025931 }
25932
25933 /* Did we encounter a breakpoint? */
25934 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25935 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025936 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025938 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 sourcing_lnum);
25940 fcp->dbg_tick = debug_tick;
25941 }
25942
25943 return retval;
25944}
25945
Bram Moolenaar05159a02005-02-26 23:04:13 +000025946#if defined(FEAT_PROFILE) || defined(PROTO)
25947/*
25948 * Called when starting to read a function line.
25949 * "sourcing_lnum" must be correct!
25950 * When skipping lines it may not actually be executed, but we won't find out
25951 * until later and we need to store the time now.
25952 */
25953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025954func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025955{
25956 funccall_T *fcp = (funccall_T *)cookie;
25957 ufunc_T *fp = fcp->func;
25958
25959 if (fp->uf_profiling && sourcing_lnum >= 1
25960 && sourcing_lnum <= fp->uf_lines.ga_len)
25961 {
25962 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025963 /* Skip continuation lines. */
25964 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25965 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025966 fp->uf_tml_execed = FALSE;
25967 profile_start(&fp->uf_tml_start);
25968 profile_zero(&fp->uf_tml_children);
25969 profile_get_wait(&fp->uf_tml_wait);
25970 }
25971}
25972
25973/*
25974 * Called when actually executing a function line.
25975 */
25976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025977func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025978{
25979 funccall_T *fcp = (funccall_T *)cookie;
25980 ufunc_T *fp = fcp->func;
25981
25982 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25983 fp->uf_tml_execed = TRUE;
25984}
25985
25986/*
25987 * Called when done with a function line.
25988 */
25989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025990func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025991{
25992 funccall_T *fcp = (funccall_T *)cookie;
25993 ufunc_T *fp = fcp->func;
25994
25995 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25996 {
25997 if (fp->uf_tml_execed)
25998 {
25999 ++fp->uf_tml_count[fp->uf_tml_idx];
26000 profile_end(&fp->uf_tml_start);
26001 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026002 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026003 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26004 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026005 }
26006 fp->uf_tml_idx = -1;
26007 }
26008}
26009#endif
26010
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011/*
26012 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026013 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 */
26015 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026016func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026017{
Bram Moolenaar33570922005-01-25 22:26:29 +000026018 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026019
26020 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26021 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026022 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 || fcp->returned);
26024}
26025
26026/*
26027 * return TRUE if cookie indicates a function which "abort"s on errors.
26028 */
26029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026030func_has_abort(
26031 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026033 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034}
26035
26036#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26037typedef enum
26038{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026039 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26040 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26041 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042} var_flavour_T;
26043
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026044static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026045
26046 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026047var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048{
26049 char_u *p = varname;
26050
26051 if (ASCII_ISUPPER(*p))
26052 {
26053 while (*(++p))
26054 if (ASCII_ISLOWER(*p))
26055 return VAR_FLAVOUR_SESSION;
26056 return VAR_FLAVOUR_VIMINFO;
26057 }
26058 else
26059 return VAR_FLAVOUR_DEFAULT;
26060}
26061#endif
26062
26063#if defined(FEAT_VIMINFO) || defined(PROTO)
26064/*
26065 * Restore global vars that start with a capital from the viminfo file
26066 */
26067 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026068read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069{
26070 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026071 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026072 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026073 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074
26075 if (!writing && (find_viminfo_parameter('!') != NULL))
26076 {
26077 tab = vim_strchr(virp->vir_line + 1, '\t');
26078 if (tab != NULL)
26079 {
26080 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026081 switch (*tab)
26082 {
26083 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026084#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026085 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026086#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026087 case 'D': type = VAR_DICT; break;
26088 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026089 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091
26092 tab = vim_strchr(tab, '\t');
26093 if (tab != NULL)
26094 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026095 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026096 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026097 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026099#ifdef FEAT_FLOAT
26100 else if (type == VAR_FLOAT)
26101 (void)string2float(tab + 1, &tv.vval.v_float);
26102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026103 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026104 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026105 if (type == VAR_DICT || type == VAR_LIST)
26106 {
26107 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26108
26109 if (etv == NULL)
26110 /* Failed to parse back the dict or list, use it as a
26111 * string. */
26112 tv.v_type = VAR_STRING;
26113 else
26114 {
26115 vim_free(tv.vval.v_string);
26116 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026117 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026118 }
26119 }
26120
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026121 /* when in a function use global variables */
26122 save_funccal = current_funccal;
26123 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026124 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026125 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026126
26127 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026128 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026129 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26130 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 }
26132 }
26133 }
26134
26135 return viminfo_readline(virp);
26136}
26137
26138/*
26139 * Write global vars that start with a capital to the viminfo file
26140 */
26141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026142write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026143{
Bram Moolenaar33570922005-01-25 22:26:29 +000026144 hashitem_T *hi;
26145 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026146 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026147 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026148 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026149 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026150 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026151
26152 if (find_viminfo_parameter('!') == NULL)
26153 return;
26154
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026155 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026156
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026157 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026158 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026159 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026160 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026162 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026163 this_var = HI2DI(hi);
26164 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026165 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026166 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026167 {
26168 case VAR_STRING: s = "STR"; break;
26169 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026170 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026171 case VAR_DICT: s = "DIC"; break;
26172 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026173 case VAR_SPECIAL: s = "XPL"; break;
26174
26175 case VAR_UNKNOWN:
26176 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026177 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026178 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026179 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026180 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026181 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026182 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026183 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026184 if (p != NULL)
26185 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026186 vim_free(tofree);
26187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188 }
26189 }
26190}
26191#endif
26192
26193#if defined(FEAT_SESSION) || defined(PROTO)
26194 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026195store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026196{
Bram Moolenaar33570922005-01-25 22:26:29 +000026197 hashitem_T *hi;
26198 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026199 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 char_u *p, *t;
26201
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026202 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026203 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026205 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026207 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026208 this_var = HI2DI(hi);
26209 if ((this_var->di_tv.v_type == VAR_NUMBER
26210 || this_var->di_tv.v_type == VAR_STRING)
26211 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026212 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026213 /* Escape special characters with a backslash. Turn a LF and
26214 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026215 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026216 (char_u *)"\\\"\n\r");
26217 if (p == NULL) /* out of memory */
26218 break;
26219 for (t = p; *t != NUL; ++t)
26220 if (*t == '\n')
26221 *t = 'n';
26222 else if (*t == '\r')
26223 *t = 'r';
26224 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026225 this_var->di_key,
26226 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26227 : ' ',
26228 p,
26229 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26230 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026231 || put_eol(fd) == FAIL)
26232 {
26233 vim_free(p);
26234 return FAIL;
26235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026236 vim_free(p);
26237 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026238#ifdef FEAT_FLOAT
26239 else if (this_var->di_tv.v_type == VAR_FLOAT
26240 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26241 {
26242 float_T f = this_var->di_tv.vval.v_float;
26243 int sign = ' ';
26244
26245 if (f < 0)
26246 {
26247 f = -f;
26248 sign = '-';
26249 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026250 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026251 this_var->di_key, sign, f) < 0)
26252 || put_eol(fd) == FAIL)
26253 return FAIL;
26254 }
26255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026256 }
26257 }
26258 return OK;
26259}
26260#endif
26261
Bram Moolenaar661b1822005-07-28 22:36:45 +000026262/*
26263 * Display script name where an item was last set.
26264 * Should only be invoked when 'verbose' is non-zero.
26265 */
26266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026267last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026268{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026269 char_u *p;
26270
Bram Moolenaar661b1822005-07-28 22:36:45 +000026271 if (scriptID != 0)
26272 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026273 p = home_replace_save(NULL, get_scriptname(scriptID));
26274 if (p != NULL)
26275 {
26276 verbose_enter();
26277 MSG_PUTS(_("\n\tLast set from "));
26278 MSG_PUTS(p);
26279 vim_free(p);
26280 verbose_leave();
26281 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026282 }
26283}
26284
Bram Moolenaard812df62008-11-09 12:46:09 +000026285/*
26286 * List v:oldfiles in a nice way.
26287 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026289ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026290{
26291 list_T *l = vimvars[VV_OLDFILES].vv_list;
26292 listitem_T *li;
26293 int nr = 0;
26294
26295 if (l == NULL)
26296 msg((char_u *)_("No old files"));
26297 else
26298 {
26299 msg_start();
26300 msg_scroll = TRUE;
26301 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26302 {
26303 msg_outnum((long)++nr);
26304 MSG_PUTS(": ");
26305 msg_outtrans(get_tv_string(&li->li_tv));
26306 msg_putchar('\n');
26307 out_flush(); /* output one line at a time */
26308 ui_breakcheck();
26309 }
26310 /* Assume "got_int" was set to truncate the listing. */
26311 got_int = FALSE;
26312
26313#ifdef FEAT_BROWSE_CMD
26314 if (cmdmod.browse)
26315 {
26316 quit_more = FALSE;
26317 nr = prompt_for_number(FALSE);
26318 msg_starthere();
26319 if (nr > 0)
26320 {
26321 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26322 (long)nr);
26323
26324 if (p != NULL)
26325 {
26326 p = expand_env_save(p);
26327 eap->arg = p;
26328 eap->cmdidx = CMD_edit;
26329 cmdmod.browse = FALSE;
26330 do_exedit(eap, NULL);
26331 vim_free(p);
26332 }
26333 }
26334 }
26335#endif
26336 }
26337}
26338
Bram Moolenaar53744302015-07-17 17:38:22 +020026339/* reset v:option_new, v:option_old and v:option_type */
26340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026341reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026342{
26343 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26344 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26345 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26346}
26347
26348
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349#endif /* FEAT_EVAL */
26350
Bram Moolenaar071d4272004-06-13 20:20:40 +000026351
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026352#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353
26354#ifdef WIN3264
26355/*
26356 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26357 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026358static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26359static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26360static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026361
26362/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 * Get the short path (8.3) for the filename in "fnamep".
26364 * Only works for a valid file name.
26365 * When the path gets longer "fnamep" is changed and the allocated buffer
26366 * is put in "bufp".
26367 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26368 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 */
26370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026371get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026372{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026373 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026374 char_u *newbuf;
26375
26376 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026377 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378 if (l > len - 1)
26379 {
26380 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026381 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 newbuf = vim_strnsave(*fnamep, l);
26383 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026384 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026385
26386 vim_free(*bufp);
26387 *fnamep = *bufp = newbuf;
26388
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026389 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026390 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026391 }
26392
26393 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026394 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026395}
26396
26397/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026398 * Get the short path (8.3) for the filename in "fname". The converted
26399 * path is returned in "bufp".
26400 *
26401 * Some of the directories specified in "fname" may not exist. This function
26402 * will shorten the existing directories at the beginning of the path and then
26403 * append the remaining non-existing path.
26404 *
26405 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026406 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026407 * bufp - Pointer to an allocated buffer for the filename.
26408 * fnamelen - Length of the filename pointed to by fname
26409 *
26410 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411 */
26412 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026413shortpath_for_invalid_fname(
26414 char_u **fname,
26415 char_u **bufp,
26416 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026417{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026418 char_u *short_fname, *save_fname, *pbuf_unused;
26419 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026421 int old_len, len;
26422 int new_len, sfx_len;
26423 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026424
26425 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026426 old_len = *fnamelen;
26427 save_fname = vim_strnsave(*fname, old_len);
26428 pbuf_unused = NULL;
26429 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026430
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026431 endp = save_fname + old_len - 1; /* Find the end of the copy */
26432 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026433
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026434 /*
26435 * Try shortening the supplied path till it succeeds by removing one
26436 * directory at a time from the tail of the path.
26437 */
26438 len = 0;
26439 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026440 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026441 /* go back one path-separator */
26442 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26443 --endp;
26444 if (endp <= save_fname)
26445 break; /* processed the complete path */
26446
26447 /*
26448 * Replace the path separator with a NUL and try to shorten the
26449 * resulting path.
26450 */
26451 ch = *endp;
26452 *endp = 0;
26453 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026454 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026455 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26456 {
26457 retval = FAIL;
26458 goto theend;
26459 }
26460 *endp = ch; /* preserve the string */
26461
26462 if (len > 0)
26463 break; /* successfully shortened the path */
26464
26465 /* failed to shorten the path. Skip the path separator */
26466 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026467 }
26468
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026469 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026470 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026471 /*
26472 * Succeeded in shortening the path. Now concatenate the shortened
26473 * path with the remaining path at the tail.
26474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026475
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026476 /* Compute the length of the new path. */
26477 sfx_len = (int)(save_endp - endp) + 1;
26478 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026480 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026481 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026482 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026483 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026484 /* There is not enough space in the currently allocated string,
26485 * copy it to a buffer big enough. */
26486 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026487 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026488 {
26489 retval = FAIL;
26490 goto theend;
26491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026492 }
26493 else
26494 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026495 /* Transfer short_fname to the main buffer (it's big enough),
26496 * unless get_short_pathname() did its work in-place. */
26497 *fname = *bufp = save_fname;
26498 if (short_fname != save_fname)
26499 vim_strncpy(save_fname, short_fname, len);
26500 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026501 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026502
26503 /* concat the not-shortened part of the path */
26504 vim_strncpy(*fname + len, endp, sfx_len);
26505 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026506 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026507
26508theend:
26509 vim_free(pbuf_unused);
26510 vim_free(save_fname);
26511
26512 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513}
26514
26515/*
26516 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026517 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518 */
26519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026520shortpath_for_partial(
26521 char_u **fnamep,
26522 char_u **bufp,
26523 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524{
26525 int sepcount, len, tflen;
26526 char_u *p;
26527 char_u *pbuf, *tfname;
26528 int hasTilde;
26529
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026530 /* Count up the path separators from the RHS.. so we know which part
26531 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026533 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534 if (vim_ispathsep(*p))
26535 ++sepcount;
26536
26537 /* Need full path first (use expand_env() to remove a "~/") */
26538 hasTilde = (**fnamep == '~');
26539 if (hasTilde)
26540 pbuf = tfname = expand_env_save(*fnamep);
26541 else
26542 pbuf = tfname = FullName_save(*fnamep, FALSE);
26543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026544 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026546 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26547 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026548
26549 if (len == 0)
26550 {
26551 /* Don't have a valid filename, so shorten the rest of the
26552 * path if we can. This CAN give us invalid 8.3 filenames, but
26553 * there's not a lot of point in guessing what it might be.
26554 */
26555 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026558 }
26559
26560 /* Count the paths backward to find the beginning of the desired string. */
26561 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026562 {
26563#ifdef FEAT_MBYTE
26564 if (has_mbyte)
26565 p -= mb_head_off(tfname, p);
26566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 if (vim_ispathsep(*p))
26568 {
26569 if (sepcount == 0 || (hasTilde && sepcount == 1))
26570 break;
26571 else
26572 sepcount --;
26573 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575 if (hasTilde)
26576 {
26577 --p;
26578 if (p >= tfname)
26579 *p = '~';
26580 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 }
26583 else
26584 ++p;
26585
26586 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26587 vim_free(*bufp);
26588 *fnamelen = (int)STRLEN(p);
26589 *bufp = pbuf;
26590 *fnamep = p;
26591
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026592 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026593}
26594#endif /* WIN3264 */
26595
26596/*
26597 * Adjust a filename, according to a string of modifiers.
26598 * *fnamep must be NUL terminated when called. When returning, the length is
26599 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026600 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601 * When there is an error, *fnamep is set to NULL.
26602 */
26603 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026604modify_fname(
26605 char_u *src, /* string with modifiers */
26606 int *usedlen, /* characters after src that are used */
26607 char_u **fnamep, /* file name so far */
26608 char_u **bufp, /* buffer for allocated file name or NULL */
26609 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026610{
26611 int valid = 0;
26612 char_u *tail;
26613 char_u *s, *p, *pbuf;
26614 char_u dirname[MAXPATHL];
26615 int c;
26616 int has_fullname = 0;
26617#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026618 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026619 int has_shortname = 0;
26620#endif
26621
26622repeat:
26623 /* ":p" - full path/file_name */
26624 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26625 {
26626 has_fullname = 1;
26627
26628 valid |= VALID_PATH;
26629 *usedlen += 2;
26630
26631 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26632 if ((*fnamep)[0] == '~'
26633#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26634 && ((*fnamep)[1] == '/'
26635# ifdef BACKSLASH_IN_FILENAME
26636 || (*fnamep)[1] == '\\'
26637# endif
26638 || (*fnamep)[1] == NUL)
26639
26640#endif
26641 )
26642 {
26643 *fnamep = expand_env_save(*fnamep);
26644 vim_free(*bufp); /* free any allocated file name */
26645 *bufp = *fnamep;
26646 if (*fnamep == NULL)
26647 return -1;
26648 }
26649
26650 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026651 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026652 {
26653 if (vim_ispathsep(*p)
26654 && p[1] == '.'
26655 && (p[2] == NUL
26656 || vim_ispathsep(p[2])
26657 || (p[2] == '.'
26658 && (p[3] == NUL || vim_ispathsep(p[3])))))
26659 break;
26660 }
26661
26662 /* FullName_save() is slow, don't use it when not needed. */
26663 if (*p != NUL || !vim_isAbsName(*fnamep))
26664 {
26665 *fnamep = FullName_save(*fnamep, *p != NUL);
26666 vim_free(*bufp); /* free any allocated file name */
26667 *bufp = *fnamep;
26668 if (*fnamep == NULL)
26669 return -1;
26670 }
26671
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026672#ifdef WIN3264
26673# if _WIN32_WINNT >= 0x0500
26674 if (vim_strchr(*fnamep, '~') != NULL)
26675 {
26676 /* Expand 8.3 filename to full path. Needed to make sure the same
26677 * file does not have two different names.
26678 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26679 p = alloc(_MAX_PATH + 1);
26680 if (p != NULL)
26681 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026682 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026683 {
26684 vim_free(*bufp);
26685 *bufp = *fnamep = p;
26686 }
26687 else
26688 vim_free(p);
26689 }
26690 }
26691# endif
26692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026693 /* Append a path separator to a directory. */
26694 if (mch_isdir(*fnamep))
26695 {
26696 /* Make room for one or two extra characters. */
26697 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26698 vim_free(*bufp); /* free any allocated file name */
26699 *bufp = *fnamep;
26700 if (*fnamep == NULL)
26701 return -1;
26702 add_pathsep(*fnamep);
26703 }
26704 }
26705
26706 /* ":." - path relative to the current directory */
26707 /* ":~" - path relative to the home directory */
26708 /* ":8" - shortname path - postponed till after */
26709 while (src[*usedlen] == ':'
26710 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26711 {
26712 *usedlen += 2;
26713 if (c == '8')
26714 {
26715#ifdef WIN3264
26716 has_shortname = 1; /* Postpone this. */
26717#endif
26718 continue;
26719 }
26720 pbuf = NULL;
26721 /* Need full path first (use expand_env() to remove a "~/") */
26722 if (!has_fullname)
26723 {
26724 if (c == '.' && **fnamep == '~')
26725 p = pbuf = expand_env_save(*fnamep);
26726 else
26727 p = pbuf = FullName_save(*fnamep, FALSE);
26728 }
26729 else
26730 p = *fnamep;
26731
26732 has_fullname = 0;
26733
26734 if (p != NULL)
26735 {
26736 if (c == '.')
26737 {
26738 mch_dirname(dirname, MAXPATHL);
26739 s = shorten_fname(p, dirname);
26740 if (s != NULL)
26741 {
26742 *fnamep = s;
26743 if (pbuf != NULL)
26744 {
26745 vim_free(*bufp); /* free any allocated file name */
26746 *bufp = pbuf;
26747 pbuf = NULL;
26748 }
26749 }
26750 }
26751 else
26752 {
26753 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26754 /* Only replace it when it starts with '~' */
26755 if (*dirname == '~')
26756 {
26757 s = vim_strsave(dirname);
26758 if (s != NULL)
26759 {
26760 *fnamep = s;
26761 vim_free(*bufp);
26762 *bufp = s;
26763 }
26764 }
26765 }
26766 vim_free(pbuf);
26767 }
26768 }
26769
26770 tail = gettail(*fnamep);
26771 *fnamelen = (int)STRLEN(*fnamep);
26772
26773 /* ":h" - head, remove "/file_name", can be repeated */
26774 /* Don't remove the first "/" or "c:\" */
26775 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26776 {
26777 valid |= VALID_HEAD;
26778 *usedlen += 2;
26779 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026780 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026781 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026782 *fnamelen = (int)(tail - *fnamep);
26783#ifdef VMS
26784 if (*fnamelen > 0)
26785 *fnamelen += 1; /* the path separator is part of the path */
26786#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026787 if (*fnamelen == 0)
26788 {
26789 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26790 p = vim_strsave((char_u *)".");
26791 if (p == NULL)
26792 return -1;
26793 vim_free(*bufp);
26794 *bufp = *fnamep = tail = p;
26795 *fnamelen = 1;
26796 }
26797 else
26798 {
26799 while (tail > s && !after_pathsep(s, tail))
26800 mb_ptr_back(*fnamep, tail);
26801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026802 }
26803
26804 /* ":8" - shortname */
26805 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26806 {
26807 *usedlen += 2;
26808#ifdef WIN3264
26809 has_shortname = 1;
26810#endif
26811 }
26812
26813#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026814 /*
26815 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026816 */
26817 if (has_shortname)
26818 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026819 /* Copy the string if it is shortened by :h and when it wasn't copied
26820 * yet, because we are going to change it in place. Avoids changing
26821 * the buffer name for "%:8". */
26822 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026823 {
26824 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026825 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026826 return -1;
26827 vim_free(*bufp);
26828 *bufp = *fnamep = p;
26829 }
26830
26831 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026832 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026833 if (!has_fullname && !vim_isAbsName(*fnamep))
26834 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026835 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026836 return -1;
26837 }
26838 else
26839 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026840 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026841
Bram Moolenaardc935552011-08-17 15:23:23 +020026842 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026844 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026845 return -1;
26846
26847 if (l == 0)
26848 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026849 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026850 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026851 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026852 return -1;
26853 }
26854 *fnamelen = l;
26855 }
26856 }
26857#endif /* WIN3264 */
26858
26859 /* ":t" - tail, just the basename */
26860 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26861 {
26862 *usedlen += 2;
26863 *fnamelen -= (int)(tail - *fnamep);
26864 *fnamep = tail;
26865 }
26866
26867 /* ":e" - extension, can be repeated */
26868 /* ":r" - root, without extension, can be repeated */
26869 while (src[*usedlen] == ':'
26870 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26871 {
26872 /* find a '.' in the tail:
26873 * - for second :e: before the current fname
26874 * - otherwise: The last '.'
26875 */
26876 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26877 s = *fnamep - 2;
26878 else
26879 s = *fnamep + *fnamelen - 1;
26880 for ( ; s > tail; --s)
26881 if (s[0] == '.')
26882 break;
26883 if (src[*usedlen + 1] == 'e') /* :e */
26884 {
26885 if (s > tail)
26886 {
26887 *fnamelen += (int)(*fnamep - (s + 1));
26888 *fnamep = s + 1;
26889#ifdef VMS
26890 /* cut version from the extension */
26891 s = *fnamep + *fnamelen - 1;
26892 for ( ; s > *fnamep; --s)
26893 if (s[0] == ';')
26894 break;
26895 if (s > *fnamep)
26896 *fnamelen = s - *fnamep;
26897#endif
26898 }
26899 else if (*fnamep <= tail)
26900 *fnamelen = 0;
26901 }
26902 else /* :r */
26903 {
26904 if (s > tail) /* remove one extension */
26905 *fnamelen = (int)(s - *fnamep);
26906 }
26907 *usedlen += 2;
26908 }
26909
26910 /* ":s?pat?foo?" - substitute */
26911 /* ":gs?pat?foo?" - global substitute */
26912 if (src[*usedlen] == ':'
26913 && (src[*usedlen + 1] == 's'
26914 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26915 {
26916 char_u *str;
26917 char_u *pat;
26918 char_u *sub;
26919 int sep;
26920 char_u *flags;
26921 int didit = FALSE;
26922
26923 flags = (char_u *)"";
26924 s = src + *usedlen + 2;
26925 if (src[*usedlen + 1] == 'g')
26926 {
26927 flags = (char_u *)"g";
26928 ++s;
26929 }
26930
26931 sep = *s++;
26932 if (sep)
26933 {
26934 /* find end of pattern */
26935 p = vim_strchr(s, sep);
26936 if (p != NULL)
26937 {
26938 pat = vim_strnsave(s, (int)(p - s));
26939 if (pat != NULL)
26940 {
26941 s = p + 1;
26942 /* find end of substitution */
26943 p = vim_strchr(s, sep);
26944 if (p != NULL)
26945 {
26946 sub = vim_strnsave(s, (int)(p - s));
26947 str = vim_strnsave(*fnamep, *fnamelen);
26948 if (sub != NULL && str != NULL)
26949 {
26950 *usedlen = (int)(p + 1 - src);
26951 s = do_string_sub(str, pat, sub, flags);
26952 if (s != NULL)
26953 {
26954 *fnamep = s;
26955 *fnamelen = (int)STRLEN(s);
26956 vim_free(*bufp);
26957 *bufp = s;
26958 didit = TRUE;
26959 }
26960 }
26961 vim_free(sub);
26962 vim_free(str);
26963 }
26964 vim_free(pat);
26965 }
26966 }
26967 /* after using ":s", repeat all the modifiers */
26968 if (didit)
26969 goto repeat;
26970 }
26971 }
26972
Bram Moolenaar26df0922014-02-23 23:39:13 +010026973 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26974 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026975 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026976 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026977 if (c != NUL)
26978 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026979 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026980 if (c != NUL)
26981 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026982 if (p == NULL)
26983 return -1;
26984 vim_free(*bufp);
26985 *bufp = *fnamep = p;
26986 *fnamelen = (int)STRLEN(p);
26987 *usedlen += 2;
26988 }
26989
Bram Moolenaar071d4272004-06-13 20:20:40 +000026990 return valid;
26991}
26992
26993/*
26994 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26995 * "flags" can be "g" to do a global substitute.
26996 * Returns an allocated string, NULL for error.
26997 */
26998 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026999do_string_sub(
27000 char_u *str,
27001 char_u *pat,
27002 char_u *sub,
27003 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027004{
27005 int sublen;
27006 regmatch_T regmatch;
27007 int i;
27008 int do_all;
27009 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027010 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027011 garray_T ga;
27012 char_u *ret;
27013 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027014 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027015
27016 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27017 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027018 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027019
27020 ga_init2(&ga, 1, 200);
27021
27022 do_all = (flags[0] == 'g');
27023
27024 regmatch.rm_ic = p_ic;
27025 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27026 if (regmatch.regprog != NULL)
27027 {
27028 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027029 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027030 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27031 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027032 /* Skip empty match except for first match. */
27033 if (regmatch.startp[0] == regmatch.endp[0])
27034 {
27035 if (zero_width == regmatch.startp[0])
27036 {
27037 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027038 i = MB_PTR2LEN(tail);
27039 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27040 (size_t)i);
27041 ga.ga_len += i;
27042 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027043 continue;
27044 }
27045 zero_width = regmatch.startp[0];
27046 }
27047
Bram Moolenaar071d4272004-06-13 20:20:40 +000027048 /*
27049 * Get some space for a temporary buffer to do the substitution
27050 * into. It will contain:
27051 * - The text up to where the match is.
27052 * - The substituted text.
27053 * - The text after the match.
27054 */
27055 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027056 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027057 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27058 {
27059 ga_clear(&ga);
27060 break;
27061 }
27062
27063 /* copy the text up to where the match is */
27064 i = (int)(regmatch.startp[0] - tail);
27065 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27066 /* add the substituted text */
27067 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27068 + ga.ga_len + i, TRUE, TRUE, FALSE);
27069 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027070 tail = regmatch.endp[0];
27071 if (*tail == NUL)
27072 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027073 if (!do_all)
27074 break;
27075 }
27076
27077 if (ga.ga_data != NULL)
27078 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27079
Bram Moolenaar473de612013-06-08 18:19:48 +020027080 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027081 }
27082
27083 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27084 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027085 if (p_cpo == empty_option)
27086 p_cpo = save_cpo;
27087 else
27088 /* Darn, evaluating {sub} expression changed the value. */
27089 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027090
27091 return ret;
27092}
27093
27094#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */