blob: 41f98d2fa1f7764b3d95825d6a3e8f5c74b6a5f4 [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 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012426 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012427 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012428 partial_T *pt;
12429 partial_T fref_pt;
12430
12431 if (argvars[0].v_type == VAR_PARTIAL)
12432 pt = argvars[0].vval.v_partial;
12433 else
12434 {
12435 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12436 fref_pt.pt_name = argvars[0].vval.v_string;
12437 pt = &fref_pt;
12438 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012439
12440 if (pt != NULL)
12441 {
12442 char_u *what = get_tv_string(&argvars[1]);
12443
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012444 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012445 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012446 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012447 if (pt->pt_name == NULL)
12448 rettv->vval.v_string = NULL;
12449 else
12450 rettv->vval.v_string = vim_strsave(pt->pt_name);
12451 }
12452 else if (STRCMP(what, "dict") == 0)
12453 {
12454 rettv->v_type = VAR_DICT;
12455 rettv->vval.v_dict = pt->pt_dict;
12456 if (pt->pt_dict != NULL)
12457 ++pt->pt_dict->dv_refcount;
12458 }
12459 else if (STRCMP(what, "args") == 0)
12460 {
12461 rettv->v_type = VAR_LIST;
12462 if (rettv_list_alloc(rettv) == OK)
12463 {
12464 int i;
12465
12466 for (i = 0; i < pt->pt_argc; ++i)
12467 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12468 }
12469 }
12470 else
12471 EMSG2(_(e_invarg2), what);
12472 return;
12473 }
12474 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012475 else
12476 EMSG2(_(e_listdictarg), "get()");
12477
12478 if (tv == NULL)
12479 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012480 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012481 copy_tv(&argvars[2], rettv);
12482 }
12483 else
12484 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485}
12486
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012487static 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 +000012488
12489/*
12490 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012491 * Return a range (from start to end) of lines in rettv from the specified
12492 * buffer.
12493 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012494 */
12495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012496get_buffer_lines(
12497 buf_T *buf,
12498 linenr_T start,
12499 linenr_T end,
12500 int retlist,
12501 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012502{
12503 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012504
Bram Moolenaar959a1432013-12-14 12:17:38 +010012505 rettv->v_type = VAR_STRING;
12506 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012507 if (retlist && rettv_list_alloc(rettv) == FAIL)
12508 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012509
12510 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12511 return;
12512
12513 if (!retlist)
12514 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012515 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12516 p = ml_get_buf(buf, start, FALSE);
12517 else
12518 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012519 rettv->vval.v_string = vim_strsave(p);
12520 }
12521 else
12522 {
12523 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012524 return;
12525
12526 if (start < 1)
12527 start = 1;
12528 if (end > buf->b_ml.ml_line_count)
12529 end = buf->b_ml.ml_line_count;
12530 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012531 if (list_append_string(rettv->vval.v_list,
12532 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012533 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012534 }
12535}
12536
12537/*
12538 * "getbufline()" function
12539 */
12540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012541f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012542{
12543 linenr_T lnum;
12544 linenr_T end;
12545 buf_T *buf;
12546
12547 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12548 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012549 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012550 --emsg_off;
12551
Bram Moolenaar661b1822005-07-28 22:36:45 +000012552 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012553 if (argvars[2].v_type == VAR_UNKNOWN)
12554 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012555 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012556 end = get_tv_lnum_buf(&argvars[2], buf);
12557
Bram Moolenaar342337a2005-07-21 21:11:17 +000012558 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012559}
12560
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561/*
12562 * "getbufvar()" function
12563 */
12564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012565f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566{
12567 buf_T *buf;
12568 buf_T *save_curbuf;
12569 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012570 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012571 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012572
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012573 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12574 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012575 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012576 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012577
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012578 rettv->v_type = VAR_STRING;
12579 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012580
12581 if (buf != NULL && varname != NULL)
12582 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012583 /* set curbuf to be our buf, temporarily */
12584 save_curbuf = curbuf;
12585 curbuf = buf;
12586
Bram Moolenaar0d660222005-01-07 21:51:51 +000012587 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012588 {
12589 if (get_option_tv(&varname, rettv, TRUE) == OK)
12590 done = TRUE;
12591 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012592 else if (STRCMP(varname, "changedtick") == 0)
12593 {
12594 rettv->v_type = VAR_NUMBER;
12595 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012596 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012597 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012598 else
12599 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012600 /* Look up the variable. */
12601 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12602 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12603 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012605 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012606 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012607 done = TRUE;
12608 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012609 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012610
12611 /* restore previous notion of curbuf */
12612 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012613 }
12614
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012615 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12616 /* use the default value */
12617 copy_tv(&argvars[2], rettv);
12618
Bram Moolenaar0d660222005-01-07 21:51:51 +000012619 --emsg_off;
12620}
12621
12622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 * "getchar()" function
12624 */
12625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012626f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012627{
12628 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012630
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012631 /* Position the cursor. Needed after a message that ends in a space. */
12632 windgoto(msg_row, msg_col);
12633
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634 ++no_mapping;
12635 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012636 for (;;)
12637 {
12638 if (argvars[0].v_type == VAR_UNKNOWN)
12639 /* getchar(): blocking wait. */
12640 n = safe_vgetc();
12641 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12642 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012643 n = vpeekc_any();
12644 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012645 /* illegal argument or getchar(0) and no char avail: return zero */
12646 n = 0;
12647 else
12648 /* getchar(0) and char avail: return char */
12649 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012650
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012651 if (n == K_IGNORE)
12652 continue;
12653 break;
12654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655 --no_mapping;
12656 --allow_keys;
12657
Bram Moolenaar219b8702006-11-01 14:32:36 +000012658 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12659 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12660 vimvars[VV_MOUSE_COL].vv_nr = 0;
12661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012662 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 if (IS_SPECIAL(n) || mod_mask != 0)
12664 {
12665 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12666 int i = 0;
12667
12668 /* Turn a special key into three bytes, plus modifier. */
12669 if (mod_mask != 0)
12670 {
12671 temp[i++] = K_SPECIAL;
12672 temp[i++] = KS_MODIFIER;
12673 temp[i++] = mod_mask;
12674 }
12675 if (IS_SPECIAL(n))
12676 {
12677 temp[i++] = K_SPECIAL;
12678 temp[i++] = K_SECOND(n);
12679 temp[i++] = K_THIRD(n);
12680 }
12681#ifdef FEAT_MBYTE
12682 else if (has_mbyte)
12683 i += (*mb_char2bytes)(n, temp + i);
12684#endif
12685 else
12686 temp[i++] = n;
12687 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 rettv->v_type = VAR_STRING;
12689 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012690
12691#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012692 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012693 {
12694 int row = mouse_row;
12695 int col = mouse_col;
12696 win_T *win;
12697 linenr_T lnum;
12698# ifdef FEAT_WINDOWS
12699 win_T *wp;
12700# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012701 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012702
12703 if (row >= 0 && col >= 0)
12704 {
12705 /* Find the window at the mouse coordinates and compute the
12706 * text position. */
12707 win = mouse_find_win(&row, &col);
12708 (void)mouse_comp_pos(win, &row, &col, &lnum);
12709# ifdef FEAT_WINDOWS
12710 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012711 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012712# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012713 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012714 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12715 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12716 }
12717 }
12718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 }
12720}
12721
12722/*
12723 * "getcharmod()" function
12724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012726f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012727{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729}
12730
12731/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012732 * "getcharsearch()" function
12733 */
12734 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012735f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012736{
12737 if (rettv_dict_alloc(rettv) != FAIL)
12738 {
12739 dict_T *dict = rettv->vval.v_dict;
12740
12741 dict_add_nr_str(dict, "char", 0L, last_csearch());
12742 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12743 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12744 }
12745}
12746
12747/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748 * "getcmdline()" function
12749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012751f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->v_type = VAR_STRING;
12754 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755}
12756
12757/*
12758 * "getcmdpos()" function
12759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012761f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764}
12765
12766/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012767 * "getcmdtype()" function
12768 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012770f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012771{
12772 rettv->v_type = VAR_STRING;
12773 rettv->vval.v_string = alloc(2);
12774 if (rettv->vval.v_string != NULL)
12775 {
12776 rettv->vval.v_string[0] = get_cmdline_type();
12777 rettv->vval.v_string[1] = NUL;
12778 }
12779}
12780
12781/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012782 * "getcmdwintype()" function
12783 */
12784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012785f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012786{
12787 rettv->v_type = VAR_STRING;
12788 rettv->vval.v_string = NULL;
12789#ifdef FEAT_CMDWIN
12790 rettv->vval.v_string = alloc(2);
12791 if (rettv->vval.v_string != NULL)
12792 {
12793 rettv->vval.v_string[0] = cmdwin_type;
12794 rettv->vval.v_string[1] = NUL;
12795 }
12796#endif
12797}
12798
12799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 * "getcwd()" function
12801 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012803f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012805 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012806 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012809 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012810
12811 wp = find_tabwin(&argvars[0], &argvars[1]);
12812 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012813 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012814 if (wp->w_localdir != NULL)
12815 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12816 else if(globaldir != NULL)
12817 rettv->vval.v_string = vim_strsave(globaldir);
12818 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012819 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012820 cwd = alloc(MAXPATHL);
12821 if (cwd != NULL)
12822 {
12823 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12824 rettv->vval.v_string = vim_strsave(cwd);
12825 vim_free(cwd);
12826 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012827 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012828#ifdef BACKSLASH_IN_FILENAME
12829 if (rettv->vval.v_string != NULL)
12830 slash_adjust(rettv->vval.v_string);
12831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832 }
12833}
12834
12835/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012836 * "getfontname()" function
12837 */
12838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012839f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012840{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012841 rettv->v_type = VAR_STRING;
12842 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012843#ifdef FEAT_GUI
12844 if (gui.in_use)
12845 {
12846 GuiFont font;
12847 char_u *name = NULL;
12848
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012849 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012850 {
12851 /* Get the "Normal" font. Either the name saved by
12852 * hl_set_font_name() or from the font ID. */
12853 font = gui.norm_font;
12854 name = hl_get_font_name();
12855 }
12856 else
12857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012858 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012859 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12860 return;
12861 font = gui_mch_get_font(name, FALSE);
12862 if (font == NOFONT)
12863 return; /* Invalid font name, return empty string. */
12864 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012865 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012866 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012867 gui_mch_free_font(font);
12868 }
12869#endif
12870}
12871
12872/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012873 * "getfperm({fname})" function
12874 */
12875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012876f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012877{
12878 char_u *fname;
12879 struct stat st;
12880 char_u *perm = NULL;
12881 char_u flags[] = "rwx";
12882 int i;
12883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012886 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887 if (mch_stat((char *)fname, &st) >= 0)
12888 {
12889 perm = vim_strsave((char_u *)"---------");
12890 if (perm != NULL)
12891 {
12892 for (i = 0; i < 9; i++)
12893 {
12894 if (st.st_mode & (1 << (8 - i)))
12895 perm[i] = flags[i % 3];
12896 }
12897 }
12898 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012900}
12901
12902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 * "getfsize({fname})" function
12904 */
12905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012906f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907{
12908 char_u *fname;
12909 struct stat st;
12910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914
12915 if (mch_stat((char *)fname, &st) >= 0)
12916 {
12917 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012919 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012922
12923 /* non-perfect check for overflow */
12924 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12925 rettv->vval.v_number = -2;
12926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 }
12928 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012929 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012930}
12931
12932/*
12933 * "getftime({fname})" function
12934 */
12935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012936f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937{
12938 char_u *fname;
12939 struct stat st;
12940
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012941 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942
12943 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012944 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947}
12948
12949/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012950 * "getftype({fname})" function
12951 */
12952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012953f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012954{
12955 char_u *fname;
12956 struct stat st;
12957 char_u *type = NULL;
12958 char *t;
12959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012960 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012963 if (mch_lstat((char *)fname, &st) >= 0)
12964 {
12965#ifdef S_ISREG
12966 if (S_ISREG(st.st_mode))
12967 t = "file";
12968 else if (S_ISDIR(st.st_mode))
12969 t = "dir";
12970# ifdef S_ISLNK
12971 else if (S_ISLNK(st.st_mode))
12972 t = "link";
12973# endif
12974# ifdef S_ISBLK
12975 else if (S_ISBLK(st.st_mode))
12976 t = "bdev";
12977# endif
12978# ifdef S_ISCHR
12979 else if (S_ISCHR(st.st_mode))
12980 t = "cdev";
12981# endif
12982# ifdef S_ISFIFO
12983 else if (S_ISFIFO(st.st_mode))
12984 t = "fifo";
12985# endif
12986# ifdef S_ISSOCK
12987 else if (S_ISSOCK(st.st_mode))
12988 t = "fifo";
12989# endif
12990 else
12991 t = "other";
12992#else
12993# ifdef S_IFMT
12994 switch (st.st_mode & S_IFMT)
12995 {
12996 case S_IFREG: t = "file"; break;
12997 case S_IFDIR: t = "dir"; break;
12998# ifdef S_IFLNK
12999 case S_IFLNK: t = "link"; break;
13000# endif
13001# ifdef S_IFBLK
13002 case S_IFBLK: t = "bdev"; break;
13003# endif
13004# ifdef S_IFCHR
13005 case S_IFCHR: t = "cdev"; break;
13006# endif
13007# ifdef S_IFIFO
13008 case S_IFIFO: t = "fifo"; break;
13009# endif
13010# ifdef S_IFSOCK
13011 case S_IFSOCK: t = "socket"; break;
13012# endif
13013 default: t = "other";
13014 }
13015# else
13016 if (mch_isdir(fname))
13017 t = "dir";
13018 else
13019 t = "file";
13020# endif
13021#endif
13022 type = vim_strsave((char_u *)t);
13023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013025}
13026
13027/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013028 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013032{
13033 linenr_T lnum;
13034 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013035 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013036
13037 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013038 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013039 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013040 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013041 retlist = FALSE;
13042 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013043 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013044 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013045 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013046 retlist = TRUE;
13047 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013048
Bram Moolenaar342337a2005-07-21 21:11:17 +000013049 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013050}
13051
13052/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013053 * "getmatches()" function
13054 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013056f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013057{
13058#ifdef FEAT_SEARCH_EXTRA
13059 dict_T *dict;
13060 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013061 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013062
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013063 if (rettv_list_alloc(rettv) == OK)
13064 {
13065 while (cur != NULL)
13066 {
13067 dict = dict_alloc();
13068 if (dict == NULL)
13069 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013070 if (cur->match.regprog == NULL)
13071 {
13072 /* match added with matchaddpos() */
13073 for (i = 0; i < MAXPOSMATCH; ++i)
13074 {
13075 llpos_T *llpos;
13076 char buf[6];
13077 list_T *l;
13078
13079 llpos = &cur->pos.pos[i];
13080 if (llpos->lnum == 0)
13081 break;
13082 l = list_alloc();
13083 if (l == NULL)
13084 break;
13085 list_append_number(l, (varnumber_T)llpos->lnum);
13086 if (llpos->col > 0)
13087 {
13088 list_append_number(l, (varnumber_T)llpos->col);
13089 list_append_number(l, (varnumber_T)llpos->len);
13090 }
13091 sprintf(buf, "pos%d", i + 1);
13092 dict_add_list(dict, buf, l);
13093 }
13094 }
13095 else
13096 {
13097 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13098 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013099 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013100 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13101 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013102# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013103 if (cur->conceal_char)
13104 {
13105 char_u buf[MB_MAXBYTES + 1];
13106
13107 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13108 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13109 }
13110# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013111 list_append_dict(rettv->vval.v_list, dict);
13112 cur = cur->next;
13113 }
13114 }
13115#endif
13116}
13117
13118/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013119 * "getpid()" function
13120 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013121 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013122f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013123{
13124 rettv->vval.v_number = mch_get_pid();
13125}
13126
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013128getpos_both(
13129 typval_T *argvars,
13130 typval_T *rettv,
13131 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013132{
Bram Moolenaara5525202006-03-02 22:52:09 +000013133 pos_T *fp;
13134 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013135 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013136
13137 if (rettv_list_alloc(rettv) == OK)
13138 {
13139 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013140 if (getcurpos)
13141 fp = &curwin->w_cursor;
13142 else
13143 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013144 if (fnum != -1)
13145 list_append_number(l, (varnumber_T)fnum);
13146 else
13147 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013148 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13149 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013150 list_append_number(l, (fp != NULL)
13151 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013152 : (varnumber_T)0);
13153 list_append_number(l,
13154#ifdef FEAT_VIRTUALEDIT
13155 (fp != NULL) ? (varnumber_T)fp->coladd :
13156#endif
13157 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013158 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013159 {
13160 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013161 list_append_number(l, curwin->w_curswant == MAXCOL ?
13162 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013163 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013164 }
13165 else
13166 rettv->vval.v_number = FALSE;
13167}
13168
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013169
13170/*
13171 * "getcurpos()" function
13172 */
13173 static void
13174f_getcurpos(typval_T *argvars, typval_T *rettv)
13175{
13176 getpos_both(argvars, rettv, TRUE);
13177}
13178
13179/*
13180 * "getpos(string)" function
13181 */
13182 static void
13183f_getpos(typval_T *argvars, typval_T *rettv)
13184{
13185 getpos_both(argvars, rettv, FALSE);
13186}
13187
Bram Moolenaara5525202006-03-02 22:52:09 +000013188/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013189 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013190 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013192f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013193{
13194#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013195 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013196#endif
13197
Bram Moolenaar2641f772005-03-25 21:58:17 +000013198#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013199 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013200 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013201 wp = NULL;
13202 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13203 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013204 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013205 if (wp == NULL)
13206 return;
13207 }
13208
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013209 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013210 }
13211#endif
13212}
13213
13214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215 * "getreg()" function
13216 */
13217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013218f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013219{
13220 char_u *strregname;
13221 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013222 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013223 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013226 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013228 strregname = get_tv_string_chk(&argvars[0]);
13229 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013230 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013233 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13234 return_list = get_tv_number_chk(&argvars[2], &error);
13235 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013237 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013238 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013239
13240 if (error)
13241 return;
13242
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 regname = (strregname == NULL ? '"' : *strregname);
13244 if (regname == 0)
13245 regname = '"';
13246
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013247 if (return_list)
13248 {
13249 rettv->v_type = VAR_LIST;
13250 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13251 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013252 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013253 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013254 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013255 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013256 }
13257 else
13258 {
13259 rettv->v_type = VAR_STRING;
13260 rettv->vval.v_string = get_reg_contents(regname,
13261 arg2 ? GREG_EXPR_SRC : 0);
13262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263}
13264
13265/*
13266 * "getregtype()" function
13267 */
13268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013269f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270{
13271 char_u *strregname;
13272 int regname;
13273 char_u buf[NUMBUFLEN + 2];
13274 long reglen = 0;
13275
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013276 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013277 {
13278 strregname = get_tv_string_chk(&argvars[0]);
13279 if (strregname == NULL) /* type error; errmsg already given */
13280 {
13281 rettv->v_type = VAR_STRING;
13282 rettv->vval.v_string = NULL;
13283 return;
13284 }
13285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 else
13287 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013288 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289
13290 regname = (strregname == NULL ? '"' : *strregname);
13291 if (regname == 0)
13292 regname = '"';
13293
13294 buf[0] = NUL;
13295 buf[1] = NUL;
13296 switch (get_reg_type(regname, &reglen))
13297 {
13298 case MLINE: buf[0] = 'V'; break;
13299 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013300 case MBLOCK:
13301 buf[0] = Ctrl_V;
13302 sprintf((char *)buf + 1, "%ld", reglen + 1);
13303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305 rettv->v_type = VAR_STRING;
13306 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013307}
13308
13309/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013310 * "gettabvar()" function
13311 */
13312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013313f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013314{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013315 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013316 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013317 dictitem_T *v;
13318 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013319 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013320
13321 rettv->v_type = VAR_STRING;
13322 rettv->vval.v_string = NULL;
13323
13324 varname = get_tv_string_chk(&argvars[1]);
13325 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13326 if (tp != NULL && varname != NULL)
13327 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013328 /* Set tp to be our tabpage, temporarily. Also set the window to the
13329 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013330 if (switch_win(&oldcurwin, &oldtabpage,
13331 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013332 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013333 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013334 /* look up the variable */
13335 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13336 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13337 if (v != NULL)
13338 {
13339 copy_tv(&v->di_tv, rettv);
13340 done = TRUE;
13341 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013342 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013343
13344 /* restore previous notion of curwin */
13345 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013346 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013347
13348 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013349 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013350}
13351
13352/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013353 * "gettabwinvar()" function
13354 */
13355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013356f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013357{
13358 getwinvar(argvars, rettv, 1);
13359}
13360
13361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 * "getwinposx()" function
13363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013365f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013367 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013368#ifdef FEAT_GUI
13369 if (gui.in_use)
13370 {
13371 int x, y;
13372
13373 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 }
13376#endif
13377}
13378
13379/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013380 * "win_findbuf()" function
13381 */
13382 static void
13383f_win_findbuf(typval_T *argvars, typval_T *rettv)
13384{
13385 if (rettv_list_alloc(rettv) != FAIL)
13386 win_findbuf(argvars, rettv->vval.v_list);
13387}
13388
13389/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013390 * "win_getid()" function
13391 */
13392 static void
13393f_win_getid(typval_T *argvars, typval_T *rettv)
13394{
13395 rettv->vval.v_number = win_getid(argvars);
13396}
13397
13398/*
13399 * "win_gotoid()" function
13400 */
13401 static void
13402f_win_gotoid(typval_T *argvars, typval_T *rettv)
13403{
13404 rettv->vval.v_number = win_gotoid(argvars);
13405}
13406
13407/*
13408 * "win_id2tabwin()" function
13409 */
13410 static void
13411f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13412{
13413 if (rettv_list_alloc(rettv) != FAIL)
13414 win_id2tabwin(argvars, rettv->vval.v_list);
13415}
13416
13417/*
13418 * "win_id2win()" function
13419 */
13420 static void
13421f_win_id2win(typval_T *argvars, typval_T *rettv)
13422{
13423 rettv->vval.v_number = win_id2win(argvars);
13424}
13425
13426/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427 * "getwinposy()" function
13428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013430f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013431{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013432 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013433#ifdef FEAT_GUI
13434 if (gui.in_use)
13435 {
13436 int x, y;
13437
13438 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 }
13441#endif
13442}
13443
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013444/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013445 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013446 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013447 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013448find_win_by_nr(
13449 typval_T *vp,
13450 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013451{
13452#ifdef FEAT_WINDOWS
13453 win_T *wp;
13454#endif
13455 int nr;
13456
13457 nr = get_tv_number_chk(vp, NULL);
13458
13459#ifdef FEAT_WINDOWS
13460 if (nr < 0)
13461 return NULL;
13462 if (nr == 0)
13463 return curwin;
13464
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013465 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13466 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013467 if (--nr <= 0)
13468 break;
13469 return wp;
13470#else
13471 if (nr == 0 || nr == 1)
13472 return curwin;
13473 return NULL;
13474#endif
13475}
13476
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013478 * Find window specified by "wvp" in tabpage "tvp".
13479 */
13480 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013481find_tabwin(
13482 typval_T *wvp, /* VAR_UNKNOWN for current window */
13483 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013484{
13485 win_T *wp = NULL;
13486 tabpage_T *tp = NULL;
13487 long n;
13488
13489 if (wvp->v_type != VAR_UNKNOWN)
13490 {
13491 if (tvp->v_type != VAR_UNKNOWN)
13492 {
13493 n = get_tv_number(tvp);
13494 if (n >= 0)
13495 tp = find_tabpage(n);
13496 }
13497 else
13498 tp = curtab;
13499
13500 if (tp != NULL)
13501 wp = find_win_by_nr(wvp, tp);
13502 }
13503 else
13504 wp = curwin;
13505
13506 return wp;
13507}
13508
13509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 * "getwinvar()" function
13511 */
13512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013513f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013515 getwinvar(argvars, rettv, 0);
13516}
13517
13518/*
13519 * getwinvar() and gettabwinvar()
13520 */
13521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013522getwinvar(
13523 typval_T *argvars,
13524 typval_T *rettv,
13525 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013526{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013527 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013528 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013529 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013530 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013531 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013532#ifdef FEAT_WINDOWS
13533 win_T *oldcurwin;
13534 tabpage_T *oldtabpage;
13535 int need_switch_win;
13536#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013538#ifdef FEAT_WINDOWS
13539 if (off == 1)
13540 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13541 else
13542 tp = curtab;
13543#endif
13544 win = find_win_by_nr(&argvars[off], tp);
13545 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013546 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013547
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013548 rettv->v_type = VAR_STRING;
13549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013550
13551 if (win != NULL && varname != NULL)
13552 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013553#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013554 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013555 * otherwise the window is not valid. Only do this when needed,
13556 * autocommands get blocked. */
13557 need_switch_win = !(tp == curtab && win == curwin);
13558 if (!need_switch_win
13559 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13560#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013561 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013562 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013563 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013564 if (get_option_tv(&varname, rettv, 1) == OK)
13565 done = TRUE;
13566 }
13567 else
13568 {
13569 /* Look up the variable. */
13570 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13571 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13572 varname, FALSE);
13573 if (v != NULL)
13574 {
13575 copy_tv(&v->di_tv, rettv);
13576 done = TRUE;
13577 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013579 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013580
Bram Moolenaarba117c22015-09-29 16:53:22 +020013581#ifdef FEAT_WINDOWS
13582 if (need_switch_win)
13583 /* restore previous notion of curwin */
13584 restore_win(oldcurwin, oldtabpage, TRUE);
13585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 }
13587
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013588 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13589 /* use the default return value */
13590 copy_tv(&argvars[off + 2], rettv);
13591
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592 --emsg_off;
13593}
13594
13595/*
13596 * "glob()" function
13597 */
13598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013599f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013600{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013601 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013603 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013605 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013606 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013608 if (argvars[1].v_type != VAR_UNKNOWN)
13609 {
13610 if (get_tv_number_chk(&argvars[1], &error))
13611 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013612 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013613 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013614 if (get_tv_number_chk(&argvars[2], &error))
13615 {
13616 rettv->v_type = VAR_LIST;
13617 rettv->vval.v_list = NULL;
13618 }
13619 if (argvars[3].v_type != VAR_UNKNOWN
13620 && get_tv_number_chk(&argvars[3], &error))
13621 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013622 }
13623 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013624 if (!error)
13625 {
13626 ExpandInit(&xpc);
13627 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013628 if (p_wic)
13629 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013630 if (rettv->v_type == VAR_STRING)
13631 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013632 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013633 else if (rettv_list_alloc(rettv) != FAIL)
13634 {
13635 int i;
13636
13637 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13638 NULL, options, WILD_ALL_KEEP);
13639 for (i = 0; i < xpc.xp_numfiles; i++)
13640 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13641
13642 ExpandCleanup(&xpc);
13643 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013644 }
13645 else
13646 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647}
13648
13649/*
13650 * "globpath()" function
13651 */
13652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013653f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013655 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013658 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013659 garray_T ga;
13660 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013662 /* When the optional second argument is non-zero, don't remove matches
13663 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013664 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013665 if (argvars[2].v_type != VAR_UNKNOWN)
13666 {
13667 if (get_tv_number_chk(&argvars[2], &error))
13668 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013669 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013670 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013671 if (get_tv_number_chk(&argvars[3], &error))
13672 {
13673 rettv->v_type = VAR_LIST;
13674 rettv->vval.v_list = NULL;
13675 }
13676 if (argvars[4].v_type != VAR_UNKNOWN
13677 && get_tv_number_chk(&argvars[4], &error))
13678 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013679 }
13680 }
13681 if (file != NULL && !error)
13682 {
13683 ga_init2(&ga, (int)sizeof(char_u *), 10);
13684 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13685 if (rettv->v_type == VAR_STRING)
13686 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13687 else if (rettv_list_alloc(rettv) != FAIL)
13688 for (i = 0; i < ga.ga_len; ++i)
13689 list_append_string(rettv->vval.v_list,
13690 ((char_u **)(ga.ga_data))[i], -1);
13691 ga_clear_strings(&ga);
13692 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013693 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695}
13696
13697/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013698 * "glob2regpat()" function
13699 */
13700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013701f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013702{
13703 char_u *pat = get_tv_string_chk(&argvars[0]);
13704
13705 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013706 rettv->vval.v_string = (pat == NULL)
13707 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013708}
13709
13710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 * "has()" function
13712 */
13713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013714f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715{
13716 int i;
13717 char_u *name;
13718 int n = FALSE;
13719 static char *(has_list[]) =
13720 {
13721#ifdef AMIGA
13722 "amiga",
13723# ifdef FEAT_ARP
13724 "arp",
13725# endif
13726#endif
13727#ifdef __BEOS__
13728 "beos",
13729#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013730#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 "mac",
13732#endif
13733#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013734 "macunix", /* built with 'darwin' enabled */
13735#endif
13736#if defined(__APPLE__) && __APPLE__ == 1
13737 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739#ifdef __QNX__
13740 "qnx",
13741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742#ifdef UNIX
13743 "unix",
13744#endif
13745#ifdef VMS
13746 "vms",
13747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013748#ifdef WIN32
13749 "win32",
13750#endif
13751#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13752 "win32unix",
13753#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013754#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755 "win64",
13756#endif
13757#ifdef EBCDIC
13758 "ebcdic",
13759#endif
13760#ifndef CASE_INSENSITIVE_FILENAME
13761 "fname_case",
13762#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013763#ifdef HAVE_ACL
13764 "acl",
13765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766#ifdef FEAT_ARABIC
13767 "arabic",
13768#endif
13769#ifdef FEAT_AUTOCMD
13770 "autocmd",
13771#endif
13772#ifdef FEAT_BEVAL
13773 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013774# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13775 "balloon_multiline",
13776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777#endif
13778#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13779 "builtin_terms",
13780# ifdef ALL_BUILTIN_TCAPS
13781 "all_builtin_terms",
13782# endif
13783#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013784#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13785 || defined(FEAT_GUI_W32) \
13786 || defined(FEAT_GUI_MOTIF))
13787 "browsefilter",
13788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789#ifdef FEAT_BYTEOFF
13790 "byte_offset",
13791#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013793 "channel",
13794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795#ifdef FEAT_CINDENT
13796 "cindent",
13797#endif
13798#ifdef FEAT_CLIENTSERVER
13799 "clientserver",
13800#endif
13801#ifdef FEAT_CLIPBOARD
13802 "clipboard",
13803#endif
13804#ifdef FEAT_CMDL_COMPL
13805 "cmdline_compl",
13806#endif
13807#ifdef FEAT_CMDHIST
13808 "cmdline_hist",
13809#endif
13810#ifdef FEAT_COMMENTS
13811 "comments",
13812#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013813#ifdef FEAT_CONCEAL
13814 "conceal",
13815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816#ifdef FEAT_CRYPT
13817 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013818 "crypt-blowfish",
13819 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820#endif
13821#ifdef FEAT_CSCOPE
13822 "cscope",
13823#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013824#ifdef FEAT_CURSORBIND
13825 "cursorbind",
13826#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013827#ifdef CURSOR_SHAPE
13828 "cursorshape",
13829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830#ifdef DEBUG
13831 "debug",
13832#endif
13833#ifdef FEAT_CON_DIALOG
13834 "dialog_con",
13835#endif
13836#ifdef FEAT_GUI_DIALOG
13837 "dialog_gui",
13838#endif
13839#ifdef FEAT_DIFF
13840 "diff",
13841#endif
13842#ifdef FEAT_DIGRAPHS
13843 "digraphs",
13844#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013845#ifdef FEAT_DIRECTX
13846 "directx",
13847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013848#ifdef FEAT_DND
13849 "dnd",
13850#endif
13851#ifdef FEAT_EMACS_TAGS
13852 "emacs_tags",
13853#endif
13854 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013855 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856#ifdef FEAT_SEARCH_EXTRA
13857 "extra_search",
13858#endif
13859#ifdef FEAT_FKMAP
13860 "farsi",
13861#endif
13862#ifdef FEAT_SEARCHPATH
13863 "file_in_path",
13864#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013865#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013866 "filterpipe",
13867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013868#ifdef FEAT_FIND_ID
13869 "find_in_path",
13870#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013871#ifdef FEAT_FLOAT
13872 "float",
13873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874#ifdef FEAT_FOLDING
13875 "folding",
13876#endif
13877#ifdef FEAT_FOOTER
13878 "footer",
13879#endif
13880#if !defined(USE_SYSTEM) && defined(UNIX)
13881 "fork",
13882#endif
13883#ifdef FEAT_GETTEXT
13884 "gettext",
13885#endif
13886#ifdef FEAT_GUI
13887 "gui",
13888#endif
13889#ifdef FEAT_GUI_ATHENA
13890# ifdef FEAT_GUI_NEXTAW
13891 "gui_neXtaw",
13892# else
13893 "gui_athena",
13894# endif
13895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896#ifdef FEAT_GUI_GTK
13897 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013898# ifdef USE_GTK3
13899 "gui_gtk3",
13900# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013904#ifdef FEAT_GUI_GNOME
13905 "gui_gnome",
13906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907#ifdef FEAT_GUI_MAC
13908 "gui_mac",
13909#endif
13910#ifdef FEAT_GUI_MOTIF
13911 "gui_motif",
13912#endif
13913#ifdef FEAT_GUI_PHOTON
13914 "gui_photon",
13915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916#ifdef FEAT_GUI_W32
13917 "gui_win32",
13918#endif
13919#ifdef FEAT_HANGULIN
13920 "hangul_input",
13921#endif
13922#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13923 "iconv",
13924#endif
13925#ifdef FEAT_INS_EXPAND
13926 "insert_expand",
13927#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013928#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013929 "job",
13930#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931#ifdef FEAT_JUMPLIST
13932 "jumplist",
13933#endif
13934#ifdef FEAT_KEYMAP
13935 "keymap",
13936#endif
13937#ifdef FEAT_LANGMAP
13938 "langmap",
13939#endif
13940#ifdef FEAT_LIBCALL
13941 "libcall",
13942#endif
13943#ifdef FEAT_LINEBREAK
13944 "linebreak",
13945#endif
13946#ifdef FEAT_LISP
13947 "lispindent",
13948#endif
13949#ifdef FEAT_LISTCMDS
13950 "listcmds",
13951#endif
13952#ifdef FEAT_LOCALMAP
13953 "localmap",
13954#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013955#ifdef FEAT_LUA
13956# ifndef DYNAMIC_LUA
13957 "lua",
13958# endif
13959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960#ifdef FEAT_MENU
13961 "menu",
13962#endif
13963#ifdef FEAT_SESSION
13964 "mksession",
13965#endif
13966#ifdef FEAT_MODIFY_FNAME
13967 "modify_fname",
13968#endif
13969#ifdef FEAT_MOUSE
13970 "mouse",
13971#endif
13972#ifdef FEAT_MOUSESHAPE
13973 "mouseshape",
13974#endif
13975#if defined(UNIX) || defined(VMS)
13976# ifdef FEAT_MOUSE_DEC
13977 "mouse_dec",
13978# endif
13979# ifdef FEAT_MOUSE_GPM
13980 "mouse_gpm",
13981# endif
13982# ifdef FEAT_MOUSE_JSB
13983 "mouse_jsbterm",
13984# endif
13985# ifdef FEAT_MOUSE_NET
13986 "mouse_netterm",
13987# endif
13988# ifdef FEAT_MOUSE_PTERM
13989 "mouse_pterm",
13990# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013991# ifdef FEAT_MOUSE_SGR
13992 "mouse_sgr",
13993# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013994# ifdef FEAT_SYSMOUSE
13995 "mouse_sysmouse",
13996# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013997# ifdef FEAT_MOUSE_URXVT
13998 "mouse_urxvt",
13999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000# ifdef FEAT_MOUSE_XTERM
14001 "mouse_xterm",
14002# endif
14003#endif
14004#ifdef FEAT_MBYTE
14005 "multi_byte",
14006#endif
14007#ifdef FEAT_MBYTE_IME
14008 "multi_byte_ime",
14009#endif
14010#ifdef FEAT_MULTI_LANG
14011 "multi_lang",
14012#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014013#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014014#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014015 "mzscheme",
14016#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#ifdef FEAT_OLE
14019 "ole",
14020#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014021 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022#ifdef FEAT_PATH_EXTRA
14023 "path_extra",
14024#endif
14025#ifdef FEAT_PERL
14026#ifndef DYNAMIC_PERL
14027 "perl",
14028#endif
14029#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014030#ifdef FEAT_PERSISTENT_UNDO
14031 "persistent_undo",
14032#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033#ifdef FEAT_PYTHON
14034#ifndef DYNAMIC_PYTHON
14035 "python",
14036#endif
14037#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014038#ifdef FEAT_PYTHON3
14039#ifndef DYNAMIC_PYTHON3
14040 "python3",
14041#endif
14042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043#ifdef FEAT_POSTSCRIPT
14044 "postscript",
14045#endif
14046#ifdef FEAT_PRINTER
14047 "printer",
14048#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014049#ifdef FEAT_PROFILE
14050 "profile",
14051#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014052#ifdef FEAT_RELTIME
14053 "reltime",
14054#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055#ifdef FEAT_QUICKFIX
14056 "quickfix",
14057#endif
14058#ifdef FEAT_RIGHTLEFT
14059 "rightleft",
14060#endif
14061#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14062 "ruby",
14063#endif
14064#ifdef FEAT_SCROLLBIND
14065 "scrollbind",
14066#endif
14067#ifdef FEAT_CMDL_INFO
14068 "showcmd",
14069 "cmdline_info",
14070#endif
14071#ifdef FEAT_SIGNS
14072 "signs",
14073#endif
14074#ifdef FEAT_SMARTINDENT
14075 "smartindent",
14076#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014077#ifdef STARTUPTIME
14078 "startuptime",
14079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080#ifdef FEAT_STL_OPT
14081 "statusline",
14082#endif
14083#ifdef FEAT_SUN_WORKSHOP
14084 "sun_workshop",
14085#endif
14086#ifdef FEAT_NETBEANS_INTG
14087 "netbeans_intg",
14088#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014089#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014090 "spell",
14091#endif
14092#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 "syntax",
14094#endif
14095#if defined(USE_SYSTEM) || !defined(UNIX)
14096 "system",
14097#endif
14098#ifdef FEAT_TAG_BINS
14099 "tag_binary",
14100#endif
14101#ifdef FEAT_TAG_OLDSTATIC
14102 "tag_old_static",
14103#endif
14104#ifdef FEAT_TAG_ANYWHITE
14105 "tag_any_white",
14106#endif
14107#ifdef FEAT_TCL
14108# ifndef DYNAMIC_TCL
14109 "tcl",
14110# endif
14111#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014112#ifdef FEAT_TERMGUICOLORS
14113 "termguicolors",
14114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014115#ifdef TERMINFO
14116 "terminfo",
14117#endif
14118#ifdef FEAT_TERMRESPONSE
14119 "termresponse",
14120#endif
14121#ifdef FEAT_TEXTOBJ
14122 "textobjects",
14123#endif
14124#ifdef HAVE_TGETENT
14125 "tgetent",
14126#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014127#ifdef FEAT_TIMERS
14128 "timers",
14129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130#ifdef FEAT_TITLE
14131 "title",
14132#endif
14133#ifdef FEAT_TOOLBAR
14134 "toolbar",
14135#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014136#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14137 "unnamedplus",
14138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139#ifdef FEAT_USR_CMDS
14140 "user-commands", /* was accidentally included in 5.4 */
14141 "user_commands",
14142#endif
14143#ifdef FEAT_VIMINFO
14144 "viminfo",
14145#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014146#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014147 "vertsplit",
14148#endif
14149#ifdef FEAT_VIRTUALEDIT
14150 "virtualedit",
14151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153#ifdef FEAT_VISUALEXTRA
14154 "visualextra",
14155#endif
14156#ifdef FEAT_VREPLACE
14157 "vreplace",
14158#endif
14159#ifdef FEAT_WILDIGN
14160 "wildignore",
14161#endif
14162#ifdef FEAT_WILDMENU
14163 "wildmenu",
14164#endif
14165#ifdef FEAT_WINDOWS
14166 "windows",
14167#endif
14168#ifdef FEAT_WAK
14169 "winaltkeys",
14170#endif
14171#ifdef FEAT_WRITEBACKUP
14172 "writebackup",
14173#endif
14174#ifdef FEAT_XIM
14175 "xim",
14176#endif
14177#ifdef FEAT_XFONTSET
14178 "xfontset",
14179#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014180#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014181 "xpm",
14182 "xpm_w32", /* for backward compatibility */
14183#else
14184# if defined(HAVE_XPM)
14185 "xpm",
14186# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188#ifdef USE_XSMP
14189 "xsmp",
14190#endif
14191#ifdef USE_XSMP_INTERACT
14192 "xsmp_interact",
14193#endif
14194#ifdef FEAT_XCLIPBOARD
14195 "xterm_clipboard",
14196#endif
14197#ifdef FEAT_XTERM_SAVE
14198 "xterm_save",
14199#endif
14200#if defined(UNIX) && defined(FEAT_X11)
14201 "X11",
14202#endif
14203 NULL
14204 };
14205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014207 for (i = 0; has_list[i] != NULL; ++i)
14208 if (STRICMP(name, has_list[i]) == 0)
14209 {
14210 n = TRUE;
14211 break;
14212 }
14213
14214 if (n == FALSE)
14215 {
14216 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014217 {
14218 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014219 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014220 && vim_isdigit(name[6])
14221 && vim_isdigit(name[8])
14222 && vim_isdigit(name[10]))
14223 {
14224 int major = atoi((char *)name + 6);
14225 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014226
14227 /* Expect "patch-9.9.01234". */
14228 n = (major < VIM_VERSION_MAJOR
14229 || (major == VIM_VERSION_MAJOR
14230 && (minor < VIM_VERSION_MINOR
14231 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014232 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014233 }
14234 else
14235 n = has_patch(atoi((char *)name + 5));
14236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 else if (STRICMP(name, "vim_starting") == 0)
14238 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014239#ifdef FEAT_MBYTE
14240 else if (STRICMP(name, "multi_byte_encoding") == 0)
14241 n = has_mbyte;
14242#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014243#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14244 else if (STRICMP(name, "balloon_multiline") == 0)
14245 n = multiline_balloon_available();
14246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247#ifdef DYNAMIC_TCL
14248 else if (STRICMP(name, "tcl") == 0)
14249 n = tcl_enabled(FALSE);
14250#endif
14251#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14252 else if (STRICMP(name, "iconv") == 0)
14253 n = iconv_enabled(FALSE);
14254#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014255#ifdef DYNAMIC_LUA
14256 else if (STRICMP(name, "lua") == 0)
14257 n = lua_enabled(FALSE);
14258#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014259#ifdef DYNAMIC_MZSCHEME
14260 else if (STRICMP(name, "mzscheme") == 0)
14261 n = mzscheme_enabled(FALSE);
14262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014263#ifdef DYNAMIC_RUBY
14264 else if (STRICMP(name, "ruby") == 0)
14265 n = ruby_enabled(FALSE);
14266#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014267#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268#ifdef DYNAMIC_PYTHON
14269 else if (STRICMP(name, "python") == 0)
14270 n = python_enabled(FALSE);
14271#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014272#endif
14273#ifdef FEAT_PYTHON3
14274#ifdef DYNAMIC_PYTHON3
14275 else if (STRICMP(name, "python3") == 0)
14276 n = python3_enabled(FALSE);
14277#endif
14278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279#ifdef DYNAMIC_PERL
14280 else if (STRICMP(name, "perl") == 0)
14281 n = perl_enabled(FALSE);
14282#endif
14283#ifdef FEAT_GUI
14284 else if (STRICMP(name, "gui_running") == 0)
14285 n = (gui.in_use || gui.starting);
14286# ifdef FEAT_GUI_W32
14287 else if (STRICMP(name, "gui_win32s") == 0)
14288 n = gui_is_win32s();
14289# endif
14290# ifdef FEAT_BROWSE
14291 else if (STRICMP(name, "browse") == 0)
14292 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14293# endif
14294#endif
14295#ifdef FEAT_SYN_HL
14296 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014297 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298#endif
14299#if defined(WIN3264)
14300 else if (STRICMP(name, "win95") == 0)
14301 n = mch_windows95();
14302#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014303#ifdef FEAT_NETBEANS_INTG
14304 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014305 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 }
14308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014309 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310}
14311
14312/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014313 * "has_key()" function
14314 */
14315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014316f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014317{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014318 if (argvars[0].v_type != VAR_DICT)
14319 {
14320 EMSG(_(e_dictreq));
14321 return;
14322 }
14323 if (argvars[0].vval.v_dict == NULL)
14324 return;
14325
14326 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014327 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014328}
14329
14330/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014331 * "haslocaldir()" function
14332 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014334f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014335{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014336 win_T *wp = NULL;
14337
14338 wp = find_tabwin(&argvars[0], &argvars[1]);
14339 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014340}
14341
14342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 * "hasmapto()" function
14344 */
14345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014346f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347{
14348 char_u *name;
14349 char_u *mode;
14350 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014351 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014354 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014355 mode = (char_u *)"nvo";
14356 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014359 if (argvars[2].v_type != VAR_UNKNOWN)
14360 abbr = get_tv_number(&argvars[2]);
14361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362
Bram Moolenaar2c932302006-03-18 21:42:09 +000014363 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014364 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014366 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367}
14368
14369/*
14370 * "histadd()" function
14371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014373f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374{
14375#ifdef FEAT_CMDHIST
14376 int histype;
14377 char_u *str;
14378 char_u buf[NUMBUFLEN];
14379#endif
14380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 if (check_restricted() || check_secure())
14383 return;
14384#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014385 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14386 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 if (histype >= 0)
14388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 if (*str != NUL)
14391 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014392 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014394 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 return;
14396 }
14397 }
14398#endif
14399}
14400
14401/*
14402 * "histdel()" function
14403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014405f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406{
14407#ifdef FEAT_CMDHIST
14408 int n;
14409 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014412 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14413 if (str == NULL)
14414 n = 0;
14415 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014418 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 else
14423 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014424 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 get_tv_string_buf(&argvars[1], buf));
14426 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427#endif
14428}
14429
14430/*
14431 * "histget()" function
14432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436#ifdef FEAT_CMDHIST
14437 int type;
14438 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014439 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14442 if (str == NULL)
14443 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 {
14446 type = get_histtype(str);
14447 if (argvars[1].v_type == VAR_UNKNOWN)
14448 idx = get_history_idx(type);
14449 else
14450 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14451 /* -1 on type error */
14452 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458}
14459
14460/*
14461 * "histnr()" function
14462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014464f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465{
14466 int i;
14467
14468#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014469 char_u *history = get_tv_string_chk(&argvars[0]);
14470
14471 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 if (i >= HIST_CMD && i < HIST_COUNT)
14473 i = get_history_idx(i);
14474 else
14475#endif
14476 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014477 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478}
14479
14480/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 * "highlightID(name)" function
14482 */
14483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014484f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487}
14488
14489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014490 * "highlight_exists()" function
14491 */
14492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014493f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014494{
14495 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14496}
14497
14498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 * "hostname()" function
14500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014502f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014503{
14504 char_u hostname[256];
14505
14506 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014507 rettv->v_type = VAR_STRING;
14508 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014509}
14510
14511/*
14512 * iconv() function
14513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014515f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516{
14517#ifdef FEAT_MBYTE
14518 char_u buf1[NUMBUFLEN];
14519 char_u buf2[NUMBUFLEN];
14520 char_u *from, *to, *str;
14521 vimconv_T vimconv;
14522#endif
14523
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014524 rettv->v_type = VAR_STRING;
14525 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526
14527#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014528 str = get_tv_string(&argvars[0]);
14529 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14530 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014531 vimconv.vc_type = CONV_NONE;
14532 convert_setup(&vimconv, from, to);
14533
14534 /* If the encodings are equal, no conversion needed. */
14535 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
14540 convert_setup(&vimconv, NULL, NULL);
14541 vim_free(from);
14542 vim_free(to);
14543#endif
14544}
14545
14546/*
14547 * "indent()" function
14548 */
14549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014550f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551{
14552 linenr_T lnum;
14553
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014554 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014555 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014556 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559}
14560
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014561/*
14562 * "index()" function
14563 */
14564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014565f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014566{
Bram Moolenaar33570922005-01-25 22:26:29 +000014567 list_T *l;
14568 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014569 long idx = 0;
14570 int ic = FALSE;
14571
14572 rettv->vval.v_number = -1;
14573 if (argvars[0].v_type != VAR_LIST)
14574 {
14575 EMSG(_(e_listreq));
14576 return;
14577 }
14578 l = argvars[0].vval.v_list;
14579 if (l != NULL)
14580 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014581 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014582 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014583 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 int error = FALSE;
14585
Bram Moolenaar758711c2005-02-02 23:11:38 +000014586 /* Start at specified item. Use the cached index that list_find()
14587 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014588 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014589 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014590 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014591 ic = get_tv_number_chk(&argvars[3], &error);
14592 if (error)
14593 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014594 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014595
Bram Moolenaar758711c2005-02-02 23:11:38 +000014596 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014597 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014598 {
14599 rettv->vval.v_number = idx;
14600 break;
14601 }
14602 }
14603}
14604
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605static int inputsecret_flag = 0;
14606
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014607static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014608
Bram Moolenaar071d4272004-06-13 20:20:40 +000014609/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014610 * This function is used by f_input() and f_inputdialog() functions. The third
14611 * argument to f_input() specifies the type of completion to use at the
14612 * prompt. The third argument to f_inputdialog() specifies the value to return
14613 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614 */
14615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014616get_user_input(
14617 typval_T *argvars,
14618 typval_T *rettv,
14619 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014621 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 char_u *p = NULL;
14623 int c;
14624 char_u buf[NUMBUFLEN];
14625 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014626 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014627 int xp_type = EXPAND_NOTHING;
14628 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014630 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014632
14633#ifdef NO_CONSOLE_INPUT
14634 /* While starting up, there is no place to enter text. */
14635 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637#endif
14638
14639 cmd_silent = FALSE; /* Want to see the prompt. */
14640 if (prompt != NULL)
14641 {
14642 /* Only the part of the message after the last NL is considered as
14643 * prompt for the command line */
14644 p = vim_strrchr(prompt, '\n');
14645 if (p == NULL)
14646 p = prompt;
14647 else
14648 {
14649 ++p;
14650 c = *p;
14651 *p = NUL;
14652 msg_start();
14653 msg_clr_eos();
14654 msg_puts_attr(prompt, echo_attr);
14655 msg_didout = FALSE;
14656 msg_starthere();
14657 *p = c;
14658 }
14659 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014661 if (argvars[1].v_type != VAR_UNKNOWN)
14662 {
14663 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14664 if (defstr != NULL)
14665 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014667 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014668 {
14669 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014670 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014671 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014672
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014673 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014674 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014675
Bram Moolenaar4463f292005-09-25 22:20:24 +000014676 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14677 if (xp_name == NULL)
14678 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014679
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014680 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014681
Bram Moolenaar4463f292005-09-25 22:20:24 +000014682 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14683 &xp_arg) == FAIL)
14684 return;
14685 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014686 }
14687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014688 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014689 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014690 int save_ex_normal_busy = ex_normal_busy;
14691 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014692 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014693 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14694 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014695 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014696 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014697 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014698 && argvars[1].v_type != VAR_UNKNOWN
14699 && argvars[2].v_type != VAR_UNKNOWN)
14700 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14701 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014702
14703 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014705 /* since the user typed this, no need to wait for return */
14706 need_wait_return = FALSE;
14707 msg_didout = FALSE;
14708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709 cmd_silent = cmd_silent_save;
14710}
14711
14712/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014713 * "input()" function
14714 * Also handles inputsecret() when inputsecret is set.
14715 */
14716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014717f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014718{
14719 get_user_input(argvars, rettv, FALSE);
14720}
14721
14722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014723 * "inputdialog()" function
14724 */
14725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014726f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014727{
14728#if defined(FEAT_GUI_TEXTDIALOG)
14729 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14730 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14731 {
14732 char_u *message;
14733 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014734 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014736 message = get_tv_string_chk(&argvars[0]);
14737 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014738 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014739 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 else
14741 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014742 if (message != NULL && defstr != NULL
14743 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014744 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014745 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 else
14747 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014748 if (message != NULL && defstr != NULL
14749 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014750 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014751 rettv->vval.v_string = vim_strsave(
14752 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014756 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014757 }
14758 else
14759#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014760 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761}
14762
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014763/*
14764 * "inputlist()" function
14765 */
14766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014767f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014768{
14769 listitem_T *li;
14770 int selected;
14771 int mouse_used;
14772
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014773#ifdef NO_CONSOLE_INPUT
14774 /* While starting up, there is no place to enter text. */
14775 if (no_console_input())
14776 return;
14777#endif
14778 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14779 {
14780 EMSG2(_(e_listarg), "inputlist()");
14781 return;
14782 }
14783
14784 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014785 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014786 lines_left = Rows; /* avoid more prompt */
14787 msg_scroll = TRUE;
14788 msg_clr_eos();
14789
14790 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14791 {
14792 msg_puts(get_tv_string(&li->li_tv));
14793 msg_putchar('\n');
14794 }
14795
14796 /* Ask for choice. */
14797 selected = prompt_for_number(&mouse_used);
14798 if (mouse_used)
14799 selected -= lines_left;
14800
14801 rettv->vval.v_number = selected;
14802}
14803
14804
Bram Moolenaar071d4272004-06-13 20:20:40 +000014805static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14806
14807/*
14808 * "inputrestore()" function
14809 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014811f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812{
14813 if (ga_userinput.ga_len > 0)
14814 {
14815 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14817 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014818 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014819 }
14820 else if (p_verbose > 1)
14821 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014822 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014824 }
14825}
14826
14827/*
14828 * "inputsave()" function
14829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014831f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014832{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014833 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014834 if (ga_grow(&ga_userinput, 1) == OK)
14835 {
14836 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14837 + ga_userinput.ga_len);
14838 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014839 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 }
14841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014842 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843}
14844
14845/*
14846 * "inputsecret()" function
14847 */
14848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014849f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014850{
14851 ++cmdline_star;
14852 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014853 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014854 --cmdline_star;
14855 --inputsecret_flag;
14856}
14857
14858/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014859 * "insert()" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863{
14864 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014865 listitem_T *item;
14866 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014867 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014868
14869 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014870 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014871 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014872 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 {
14874 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014875 before = get_tv_number_chk(&argvars[2], &error);
14876 if (error)
14877 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014878
Bram Moolenaar758711c2005-02-02 23:11:38 +000014879 if (before == l->lv_len)
14880 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014881 else
14882 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014883 item = list_find(l, before);
14884 if (item == NULL)
14885 {
14886 EMSGN(_(e_listidx), before);
14887 l = NULL;
14888 }
14889 }
14890 if (l != NULL)
14891 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014892 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014893 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014894 }
14895 }
14896}
14897
14898/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014899 * "invert(expr)" function
14900 */
14901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014902f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014903{
14904 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14905}
14906
14907/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 * "isdirectory()" function
14909 */
14910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914}
14915
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014916/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014917 * "islocked()" function
14918 */
14919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014920f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014921{
14922 lval_T lv;
14923 char_u *end;
14924 dictitem_T *di;
14925
14926 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014927 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14928 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014929 if (end != NULL && lv.ll_name != NULL)
14930 {
14931 if (*end != NUL)
14932 EMSG(_(e_trailing));
14933 else
14934 {
14935 if (lv.ll_tv == NULL)
14936 {
14937 if (check_changedtick(lv.ll_name))
14938 rettv->vval.v_number = 1; /* always locked */
14939 else
14940 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014941 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014942 if (di != NULL)
14943 {
14944 /* Consider a variable locked when:
14945 * 1. the variable itself is locked
14946 * 2. the value of the variable is locked.
14947 * 3. the List or Dict value is locked.
14948 */
14949 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14950 || tv_islocked(&di->di_tv));
14951 }
14952 }
14953 }
14954 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014955 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014956 else if (lv.ll_newkey != NULL)
14957 EMSG2(_(e_dictkey), lv.ll_newkey);
14958 else if (lv.ll_list != NULL)
14959 /* List item. */
14960 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14961 else
14962 /* Dictionary item. */
14963 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14964 }
14965 }
14966
14967 clear_lval(&lv);
14968}
14969
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014970#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14971/*
14972 * "isnan()" function
14973 */
14974 static void
14975f_isnan(typval_T *argvars, typval_T *rettv)
14976{
14977 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14978 && isnan(argvars[0].vval.v_float);
14979}
14980#endif
14981
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014982static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014983
14984/*
14985 * Turn a dict into a list:
14986 * "what" == 0: list of keys
14987 * "what" == 1: list of values
14988 * "what" == 2: list of items
14989 */
14990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014991dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014992{
Bram Moolenaar33570922005-01-25 22:26:29 +000014993 list_T *l2;
14994 dictitem_T *di;
14995 hashitem_T *hi;
14996 listitem_T *li;
14997 listitem_T *li2;
14998 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014999 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015000
Bram Moolenaar8c711452005-01-14 21:53:12 +000015001 if (argvars[0].v_type != VAR_DICT)
15002 {
15003 EMSG(_(e_dictreq));
15004 return;
15005 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015006 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015007 return;
15008
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015009 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015010 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015011
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015012 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015013 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015014 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015015 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015016 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015017 --todo;
15018 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015019
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015020 li = listitem_alloc();
15021 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015022 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015023 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015025 if (what == 0)
15026 {
15027 /* keys() */
15028 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015029 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015030 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15031 }
15032 else if (what == 1)
15033 {
15034 /* values() */
15035 copy_tv(&di->di_tv, &li->li_tv);
15036 }
15037 else
15038 {
15039 /* items() */
15040 l2 = list_alloc();
15041 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015042 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015043 li->li_tv.vval.v_list = l2;
15044 if (l2 == NULL)
15045 break;
15046 ++l2->lv_refcount;
15047
15048 li2 = listitem_alloc();
15049 if (li2 == NULL)
15050 break;
15051 list_append(l2, li2);
15052 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015053 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015054 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15055
15056 li2 = listitem_alloc();
15057 if (li2 == NULL)
15058 break;
15059 list_append(l2, li2);
15060 copy_tv(&di->di_tv, &li2->li_tv);
15061 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015062 }
15063 }
15064}
15065
15066/*
15067 * "items(dict)" function
15068 */
15069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015070f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015071{
15072 dict_list(argvars, rettv, 2);
15073}
15074
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015075#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015076/*
15077 * Get the job from the argument.
15078 * Returns NULL if the job is invalid.
15079 */
15080 static job_T *
15081get_job_arg(typval_T *tv)
15082{
15083 job_T *job;
15084
15085 if (tv->v_type != VAR_JOB)
15086 {
15087 EMSG2(_(e_invarg2), get_tv_string(tv));
15088 return NULL;
15089 }
15090 job = tv->vval.v_job;
15091
15092 if (job == NULL)
15093 EMSG(_("E916: not a valid job"));
15094 return job;
15095}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015096
Bram Moolenaar835dc632016-02-07 14:27:38 +010015097/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015098 * "job_getchannel()" function
15099 */
15100 static void
15101f_job_getchannel(typval_T *argvars, typval_T *rettv)
15102{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015103 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015104
Bram Moolenaar65edff82016-02-21 16:40:11 +010015105 if (job != NULL)
15106 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015107 rettv->v_type = VAR_CHANNEL;
15108 rettv->vval.v_channel = job->jv_channel;
15109 if (job->jv_channel != NULL)
15110 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015111 }
15112}
15113
15114/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015115 * "job_info()" function
15116 */
15117 static void
15118f_job_info(typval_T *argvars, typval_T *rettv)
15119{
15120 job_T *job = get_job_arg(&argvars[0]);
15121
15122 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15123 job_info(job, rettv->vval.v_dict);
15124}
15125
15126/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015127 * "job_setoptions()" function
15128 */
15129 static void
15130f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15131{
15132 job_T *job = get_job_arg(&argvars[0]);
15133 jobopt_T opt;
15134
15135 if (job == NULL)
15136 return;
15137 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015138 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15139 job_set_options(job, &opt);
15140 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015141}
15142
15143/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015144 * "job_start()" function
15145 */
15146 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015147f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015148{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015149 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015150 if (check_restricted() || check_secure())
15151 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015152 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015153}
15154
15155/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015156 * "job_status()" function
15157 */
15158 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015159f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015160{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015161 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015162
Bram Moolenaar65edff82016-02-21 16:40:11 +010015163 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015164 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015165 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015166 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015167 }
15168}
15169
15170/*
15171 * "job_stop()" function
15172 */
15173 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015174f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015175{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015176 job_T *job = get_job_arg(&argvars[0]);
15177
15178 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015179 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015180}
15181#endif
15182
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015184 * "join()" function
15185 */
15186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015187f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015188{
15189 garray_T ga;
15190 char_u *sep;
15191
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015192 if (argvars[0].v_type != VAR_LIST)
15193 {
15194 EMSG(_(e_listreq));
15195 return;
15196 }
15197 if (argvars[0].vval.v_list == NULL)
15198 return;
15199 if (argvars[1].v_type == VAR_UNKNOWN)
15200 sep = (char_u *)" ";
15201 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015202 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015203
15204 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015205
15206 if (sep != NULL)
15207 {
15208 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015209 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015210 ga_append(&ga, NUL);
15211 rettv->vval.v_string = (char_u *)ga.ga_data;
15212 }
15213 else
15214 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015215}
15216
15217/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015218 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015219 */
15220 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015221f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015222{
15223 js_read_T reader;
15224
15225 reader.js_buf = get_tv_string(&argvars[0]);
15226 reader.js_fill = NULL;
15227 reader.js_used = 0;
15228 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15229 EMSG(_(e_invarg));
15230}
15231
15232/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015233 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015234 */
15235 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015236f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015237{
15238 rettv->v_type = VAR_STRING;
15239 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15240}
15241
15242/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015243 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015244 */
15245 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015246f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015247{
15248 js_read_T reader;
15249
15250 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015251 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015252 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015253 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015254 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015255}
15256
15257/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015258 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015259 */
15260 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015261f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015262{
15263 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015264 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015265}
15266
15267/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015268 * "keys()" function
15269 */
15270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015271f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015272{
15273 dict_list(argvars, rettv, 0);
15274}
15275
15276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 * "last_buffer_nr()" function.
15278 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015280f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281{
15282 int n = 0;
15283 buf_T *buf;
15284
15285 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15286 if (n < buf->b_fnum)
15287 n = buf->b_fnum;
15288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015289 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015290}
15291
15292/*
15293 * "len()" function
15294 */
15295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015296f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015297{
15298 switch (argvars[0].v_type)
15299 {
15300 case VAR_STRING:
15301 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015302 rettv->vval.v_number = (varnumber_T)STRLEN(
15303 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 break;
15305 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015307 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015308 case VAR_DICT:
15309 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15310 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015311 case VAR_UNKNOWN:
15312 case VAR_SPECIAL:
15313 case VAR_FLOAT:
15314 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015315 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015316 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015317 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015318 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015319 break;
15320 }
15321}
15322
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015323static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015324
15325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015326libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015327{
15328#ifdef FEAT_LIBCALL
15329 char_u *string_in;
15330 char_u **string_result;
15331 int nr_result;
15332#endif
15333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015334 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015335 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015336 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015337
15338 if (check_restricted() || check_secure())
15339 return;
15340
15341#ifdef FEAT_LIBCALL
15342 /* The first two args must be strings, otherwise its meaningless */
15343 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15344 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015345 string_in = NULL;
15346 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015347 string_in = argvars[2].vval.v_string;
15348 if (type == VAR_NUMBER)
15349 string_result = NULL;
15350 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015352 if (mch_libcall(argvars[0].vval.v_string,
15353 argvars[1].vval.v_string,
15354 string_in,
15355 argvars[2].vval.v_number,
15356 string_result,
15357 &nr_result) == OK
15358 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015360 }
15361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362}
15363
15364/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015365 * "libcall()" function
15366 */
15367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015368f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015369{
15370 libcall_common(argvars, rettv, VAR_STRING);
15371}
15372
15373/*
15374 * "libcallnr()" function
15375 */
15376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015377f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015378{
15379 libcall_common(argvars, rettv, VAR_NUMBER);
15380}
15381
15382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015383 * "line(string)" function
15384 */
15385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015386f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387{
15388 linenr_T lnum = 0;
15389 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015390 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015392 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 if (fp != NULL)
15394 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015395 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396}
15397
15398/*
15399 * "line2byte(lnum)" function
15400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015402f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403{
15404#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015405 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406#else
15407 linenr_T lnum;
15408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015413 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15414 if (rettv->vval.v_number >= 0)
15415 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015416#endif
15417}
15418
15419/*
15420 * "lispindent(lnum)" function
15421 */
15422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015423f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424{
15425#ifdef FEAT_LISP
15426 pos_T pos;
15427 linenr_T lnum;
15428
15429 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015430 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15432 {
15433 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015434 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 curwin->w_cursor = pos;
15436 }
15437 else
15438#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440}
15441
15442/*
15443 * "localtime()" function
15444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015446f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449}
15450
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015451static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452
15453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015454get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015455{
15456 char_u *keys;
15457 char_u *which;
15458 char_u buf[NUMBUFLEN];
15459 char_u *keys_buf = NULL;
15460 char_u *rhs;
15461 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015462 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015463 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015464 mapblock_T *mp;
15465 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
15467 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015468 rettv->v_type = VAR_STRING;
15469 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015471 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 if (*keys == NUL)
15473 return;
15474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015477 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015478 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015479 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015480 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015481 if (argvars[3].v_type != VAR_UNKNOWN)
15482 get_dict = get_tv_number(&argvars[3]);
15483 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015485 else
15486 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015487 if (which == NULL)
15488 return;
15489
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 mode = get_map_mode(&which, 0);
15491
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015492 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015493 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015495
15496 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015498 /* Return a string. */
15499 if (rhs != NULL)
15500 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501
Bram Moolenaarbd743252010-10-20 21:23:33 +020015502 }
15503 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15504 {
15505 /* Return a dictionary. */
15506 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15507 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15508 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509
Bram Moolenaarbd743252010-10-20 21:23:33 +020015510 dict_add_nr_str(dict, "lhs", 0L, lhs);
15511 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15512 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15513 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15514 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15515 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15516 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015517 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015518 dict_add_nr_str(dict, "mode", 0L, mapmode);
15519
15520 vim_free(lhs);
15521 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015522 }
15523}
15524
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015525#ifdef FEAT_FLOAT
15526/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015527 * "log()" function
15528 */
15529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015530f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015531{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015532 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015533
15534 rettv->v_type = VAR_FLOAT;
15535 if (get_float_arg(argvars, &f) == OK)
15536 rettv->vval.v_float = log(f);
15537 else
15538 rettv->vval.v_float = 0.0;
15539}
15540
15541/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015542 * "log10()" function
15543 */
15544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015545f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015546{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015547 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015548
15549 rettv->v_type = VAR_FLOAT;
15550 if (get_float_arg(argvars, &f) == OK)
15551 rettv->vval.v_float = log10(f);
15552 else
15553 rettv->vval.v_float = 0.0;
15554}
15555#endif
15556
Bram Moolenaar1dced572012-04-05 16:54:08 +020015557#ifdef FEAT_LUA
15558/*
15559 * "luaeval()" function
15560 */
15561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015562f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015563{
15564 char_u *str;
15565 char_u buf[NUMBUFLEN];
15566
15567 str = get_tv_string_buf(&argvars[0], buf);
15568 do_luaeval(str, argvars + 1, rettv);
15569}
15570#endif
15571
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015573 * "map()" function
15574 */
15575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015576f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015577{
15578 filter_map(argvars, rettv, TRUE);
15579}
15580
15581/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015582 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 */
15584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015585f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015587 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015588}
15589
15590/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015591 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 */
15593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015594f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015596 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597}
15598
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015599static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600
15601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015602find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015604 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015605 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015606 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 char_u *pat;
15608 regmatch_T regmatch;
15609 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015610 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 char_u *save_cpo;
15612 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015613 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015614 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015615 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015616 list_T *l = NULL;
15617 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015618 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015619 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620
15621 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15622 save_cpo = p_cpo;
15623 p_cpo = (char_u *)"";
15624
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015625 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015626 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015627 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015628 /* type 3: return empty list when there are no matches.
15629 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015630 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015631 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015632 if (type == 4
15633 && (list_append_string(rettv->vval.v_list,
15634 (char_u *)"", 0) == FAIL
15635 || list_append_number(rettv->vval.v_list,
15636 (varnumber_T)-1) == FAIL
15637 || list_append_number(rettv->vval.v_list,
15638 (varnumber_T)-1) == FAIL
15639 || list_append_number(rettv->vval.v_list,
15640 (varnumber_T)-1) == FAIL))
15641 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015642 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015643 rettv->vval.v_list = NULL;
15644 goto theend;
15645 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015646 }
15647 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015649 rettv->v_type = VAR_STRING;
15650 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015653 if (argvars[0].v_type == VAR_LIST)
15654 {
15655 if ((l = argvars[0].vval.v_list) == NULL)
15656 goto theend;
15657 li = l->lv_first;
15658 }
15659 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015660 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015661 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015662 len = (long)STRLEN(str);
15663 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015665 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15666 if (pat == NULL)
15667 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015668
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015669 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015671 int error = FALSE;
15672
15673 start = get_tv_number_chk(&argvars[2], &error);
15674 if (error)
15675 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015676 if (l != NULL)
15677 {
15678 li = list_find(l, start);
15679 if (li == NULL)
15680 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015681 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015682 }
15683 else
15684 {
15685 if (start < 0)
15686 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015687 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015688 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015689 /* When "count" argument is there ignore matches before "start",
15690 * otherwise skip part of the string. Differs when pattern is "^"
15691 * or "\<". */
15692 if (argvars[3].v_type != VAR_UNKNOWN)
15693 startcol = start;
15694 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015695 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015696 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015697 len -= start;
15698 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015699 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015700
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015701 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015702 nth = get_tv_number_chk(&argvars[3], &error);
15703 if (error)
15704 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 }
15706
15707 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15708 if (regmatch.regprog != NULL)
15709 {
15710 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015711
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015712 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015713 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015714 if (l != NULL)
15715 {
15716 if (li == NULL)
15717 {
15718 match = FALSE;
15719 break;
15720 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015721 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015722 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015723 if (str == NULL)
15724 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015725 }
15726
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015727 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015728
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015729 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015730 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015731 if (l == NULL && !match)
15732 break;
15733
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015734 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015735 if (l != NULL)
15736 {
15737 li = li->li_next;
15738 ++idx;
15739 }
15740 else
15741 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015742#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015743 startcol = (colnr_T)(regmatch.startp[0]
15744 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015745#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015746 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015747#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015748 if (startcol > (colnr_T)len
15749 || str + startcol <= regmatch.startp[0])
15750 {
15751 match = FALSE;
15752 break;
15753 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015754 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015755 }
15756
15757 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015759 if (type == 4)
15760 {
15761 listitem_T *li1 = rettv->vval.v_list->lv_first;
15762 listitem_T *li2 = li1->li_next;
15763 listitem_T *li3 = li2->li_next;
15764 listitem_T *li4 = li3->li_next;
15765
15766 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15767 (int)(regmatch.endp[0] - regmatch.startp[0]));
15768 li3->li_tv.vval.v_number =
15769 (varnumber_T)(regmatch.startp[0] - expr);
15770 li4->li_tv.vval.v_number =
15771 (varnumber_T)(regmatch.endp[0] - expr);
15772 if (l != NULL)
15773 li2->li_tv.vval.v_number = (varnumber_T)idx;
15774 }
15775 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015776 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015777 int i;
15778
15779 /* return list with matched string and submatches */
15780 for (i = 0; i < NSUBEXP; ++i)
15781 {
15782 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015783 {
15784 if (list_append_string(rettv->vval.v_list,
15785 (char_u *)"", 0) == FAIL)
15786 break;
15787 }
15788 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015789 regmatch.startp[i],
15790 (int)(regmatch.endp[i] - regmatch.startp[i]))
15791 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015792 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015793 }
15794 }
15795 else if (type == 2)
15796 {
15797 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015798 if (l != NULL)
15799 copy_tv(&li->li_tv, rettv);
15800 else
15801 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015803 }
15804 else if (l != NULL)
15805 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806 else
15807 {
15808 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015810 (varnumber_T)(regmatch.startp[0] - str);
15811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015812 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015814 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 }
15816 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015817 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 }
15819
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015820 if (type == 4 && l == NULL)
15821 /* matchstrpos() without a list: drop the second item. */
15822 listitem_remove(rettv->vval.v_list,
15823 rettv->vval.v_list->lv_first->li_next);
15824
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015826 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 p_cpo = save_cpo;
15828}
15829
15830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015831 * "match()" function
15832 */
15833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015834f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015835{
15836 find_some_match(argvars, rettv, 1);
15837}
15838
15839/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015840 * "matchadd()" function
15841 */
15842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015843f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015844{
15845#ifdef FEAT_SEARCH_EXTRA
15846 char_u buf[NUMBUFLEN];
15847 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15848 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15849 int prio = 10; /* default priority */
15850 int id = -1;
15851 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015852 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015853
15854 rettv->vval.v_number = -1;
15855
15856 if (grp == NULL || pat == NULL)
15857 return;
15858 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015859 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015860 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015861 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015862 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015863 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015864 if (argvars[4].v_type != VAR_UNKNOWN)
15865 {
15866 if (argvars[4].v_type != VAR_DICT)
15867 {
15868 EMSG(_(e_dictreq));
15869 return;
15870 }
15871 if (dict_find(argvars[4].vval.v_dict,
15872 (char_u *)"conceal", -1) != NULL)
15873 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15874 (char_u *)"conceal", FALSE);
15875 }
15876 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015877 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015878 if (error == TRUE)
15879 return;
15880 if (id >= 1 && id <= 3)
15881 {
15882 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15883 return;
15884 }
15885
Bram Moolenaar6561d522015-07-21 15:48:27 +020015886 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15887 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015888#endif
15889}
15890
15891/*
15892 * "matchaddpos()" function
15893 */
15894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015895f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015896{
15897#ifdef FEAT_SEARCH_EXTRA
15898 char_u buf[NUMBUFLEN];
15899 char_u *group;
15900 int prio = 10;
15901 int id = -1;
15902 int error = FALSE;
15903 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015904 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015905
15906 rettv->vval.v_number = -1;
15907
15908 group = get_tv_string_buf_chk(&argvars[0], buf);
15909 if (group == NULL)
15910 return;
15911
15912 if (argvars[1].v_type != VAR_LIST)
15913 {
15914 EMSG2(_(e_listarg), "matchaddpos()");
15915 return;
15916 }
15917 l = argvars[1].vval.v_list;
15918 if (l == NULL)
15919 return;
15920
15921 if (argvars[2].v_type != VAR_UNKNOWN)
15922 {
15923 prio = get_tv_number_chk(&argvars[2], &error);
15924 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015925 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015926 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015927 if (argvars[4].v_type != VAR_UNKNOWN)
15928 {
15929 if (argvars[4].v_type != VAR_DICT)
15930 {
15931 EMSG(_(e_dictreq));
15932 return;
15933 }
15934 if (dict_find(argvars[4].vval.v_dict,
15935 (char_u *)"conceal", -1) != NULL)
15936 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15937 (char_u *)"conceal", FALSE);
15938 }
15939 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015940 }
15941 if (error == TRUE)
15942 return;
15943
15944 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15945 if (id == 1 || id == 2)
15946 {
15947 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15948 return;
15949 }
15950
Bram Moolenaar6561d522015-07-21 15:48:27 +020015951 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15952 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015953#endif
15954}
15955
15956/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015957 * "matcharg()" function
15958 */
15959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015960f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015961{
15962 if (rettv_list_alloc(rettv) == OK)
15963 {
15964#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015965 int id = get_tv_number(&argvars[0]);
15966 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015967
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015968 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015969 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015970 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15971 {
15972 list_append_string(rettv->vval.v_list,
15973 syn_id2name(m->hlg_id), -1);
15974 list_append_string(rettv->vval.v_list, m->pattern, -1);
15975 }
15976 else
15977 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015978 list_append_string(rettv->vval.v_list, NULL, -1);
15979 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015980 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015981 }
15982#endif
15983 }
15984}
15985
15986/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015987 * "matchdelete()" function
15988 */
15989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015990f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015991{
15992#ifdef FEAT_SEARCH_EXTRA
15993 rettv->vval.v_number = match_delete(curwin,
15994 (int)get_tv_number(&argvars[0]), TRUE);
15995#endif
15996}
15997
15998/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015999 * "matchend()" function
16000 */
16001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016002f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016003{
16004 find_some_match(argvars, rettv, 0);
16005}
16006
16007/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016008 * "matchlist()" function
16009 */
16010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016011f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016012{
16013 find_some_match(argvars, rettv, 3);
16014}
16015
16016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016017 * "matchstr()" function
16018 */
16019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016020f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016021{
16022 find_some_match(argvars, rettv, 2);
16023}
16024
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016025/*
16026 * "matchstrpos()" function
16027 */
16028 static void
16029f_matchstrpos(typval_T *argvars, typval_T *rettv)
16030{
16031 find_some_match(argvars, rettv, 4);
16032}
16033
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016034static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016035
16036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016037max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016038{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016039 long n = 0;
16040 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016042
16043 if (argvars[0].v_type == VAR_LIST)
16044 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016045 list_T *l;
16046 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016047
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016048 l = argvars[0].vval.v_list;
16049 if (l != NULL)
16050 {
16051 li = l->lv_first;
16052 if (li != NULL)
16053 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016054 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016055 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016056 {
16057 li = li->li_next;
16058 if (li == NULL)
16059 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016060 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016061 if (domax ? i > n : i < n)
16062 n = i;
16063 }
16064 }
16065 }
16066 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016067 else if (argvars[0].v_type == VAR_DICT)
16068 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016070 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016071 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016072 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016073
16074 d = argvars[0].vval.v_dict;
16075 if (d != NULL)
16076 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016077 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016078 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016079 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016080 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016081 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016082 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016084 if (first)
16085 {
16086 n = i;
16087 first = FALSE;
16088 }
16089 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016090 n = i;
16091 }
16092 }
16093 }
16094 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016095 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016096 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016097 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016098}
16099
16100/*
16101 * "max()" function
16102 */
16103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016104f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016105{
16106 max_min(argvars, rettv, TRUE);
16107}
16108
16109/*
16110 * "min()" function
16111 */
16112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016113f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016114{
16115 max_min(argvars, rettv, FALSE);
16116}
16117
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016118static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016119
16120/*
16121 * Create the directory in which "dir" is located, and higher levels when
16122 * needed.
16123 */
16124 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016125mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016126{
16127 char_u *p;
16128 char_u *updir;
16129 int r = FAIL;
16130
16131 /* Get end of directory name in "dir".
16132 * We're done when it's "/" or "c:/". */
16133 p = gettail_sep(dir);
16134 if (p <= get_past_head(dir))
16135 return OK;
16136
16137 /* If the directory exists we're done. Otherwise: create it.*/
16138 updir = vim_strnsave(dir, (int)(p - dir));
16139 if (updir == NULL)
16140 return FAIL;
16141 if (mch_isdir(updir))
16142 r = OK;
16143 else if (mkdir_recurse(updir, prot) == OK)
16144 r = vim_mkdir_emsg(updir, prot);
16145 vim_free(updir);
16146 return r;
16147}
16148
16149#ifdef vim_mkdir
16150/*
16151 * "mkdir()" function
16152 */
16153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016154f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016155{
16156 char_u *dir;
16157 char_u buf[NUMBUFLEN];
16158 int prot = 0755;
16159
16160 rettv->vval.v_number = FAIL;
16161 if (check_restricted() || check_secure())
16162 return;
16163
16164 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016165 if (*dir == NUL)
16166 rettv->vval.v_number = FAIL;
16167 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016168 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016169 if (*gettail(dir) == NUL)
16170 /* remove trailing slashes */
16171 *gettail_sep(dir) = NUL;
16172
16173 if (argvars[1].v_type != VAR_UNKNOWN)
16174 {
16175 if (argvars[2].v_type != VAR_UNKNOWN)
16176 prot = get_tv_number_chk(&argvars[2], NULL);
16177 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16178 mkdir_recurse(dir, prot);
16179 }
16180 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016181 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016182}
16183#endif
16184
Bram Moolenaar0d660222005-01-07 21:51:51 +000016185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 * "mode()" function
16187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016189f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016191 char_u buf[3];
16192
16193 buf[1] = NUL;
16194 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195
Bram Moolenaar071d4272004-06-13 20:20:40 +000016196 if (VIsual_active)
16197 {
16198 if (VIsual_select)
16199 buf[0] = VIsual_mode + 's' - 'v';
16200 else
16201 buf[0] = VIsual_mode;
16202 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016203 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016204 || State == CONFIRM)
16205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016207 if (State == ASKMORE)
16208 buf[1] = 'm';
16209 else if (State == CONFIRM)
16210 buf[1] = '?';
16211 }
16212 else if (State == EXTERNCMD)
16213 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016214 else if (State & INSERT)
16215 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016216#ifdef FEAT_VREPLACE
16217 if (State & VREPLACE_FLAG)
16218 {
16219 buf[0] = 'R';
16220 buf[1] = 'v';
16221 }
16222 else
16223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 if (State & REPLACE_FLAG)
16225 buf[0] = 'R';
16226 else
16227 buf[0] = 'i';
16228 }
16229 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016232 if (exmode_active)
16233 buf[1] = 'v';
16234 }
16235 else if (exmode_active)
16236 {
16237 buf[0] = 'c';
16238 buf[1] = 'e';
16239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016240 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016242 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016243 if (finish_op)
16244 buf[1] = 'o';
16245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016247 /* Clear out the minor mode when the argument is not a non-zero number or
16248 * non-empty string. */
16249 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016250 buf[1] = NUL;
16251
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 rettv->vval.v_string = vim_strsave(buf);
16253 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254}
16255
Bram Moolenaar429fa852013-04-15 12:27:36 +020016256#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016257/*
16258 * "mzeval()" function
16259 */
16260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016261f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016262{
16263 char_u *str;
16264 char_u buf[NUMBUFLEN];
16265
16266 str = get_tv_string_buf(&argvars[0], buf);
16267 do_mzeval(str, rettv);
16268}
Bram Moolenaar75676462013-01-30 14:55:42 +010016269
16270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016271mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016272{
16273 typval_T argvars[3];
16274
16275 argvars[0].v_type = VAR_STRING;
16276 argvars[0].vval.v_string = name;
16277 copy_tv(args, &argvars[1]);
16278 argvars[2].v_type = VAR_UNKNOWN;
16279 f_call(argvars, rettv);
16280 clear_tv(&argvars[1]);
16281}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016282#endif
16283
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 * "nextnonblank()" function
16286 */
16287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016288f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016289{
16290 linenr_T lnum;
16291
16292 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016294 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295 {
16296 lnum = 0;
16297 break;
16298 }
16299 if (*skipwhite(ml_get(lnum)) != NUL)
16300 break;
16301 }
16302 rettv->vval.v_number = lnum;
16303}
16304
16305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016306 * "nr2char()" function
16307 */
16308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016309f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
16311 char_u buf[NUMBUFLEN];
16312
16313#ifdef FEAT_MBYTE
16314 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016315 {
16316 int utf8 = 0;
16317
16318 if (argvars[1].v_type != VAR_UNKNOWN)
16319 utf8 = get_tv_number_chk(&argvars[1], NULL);
16320 if (utf8)
16321 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16322 else
16323 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325 else
16326#endif
16327 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016328 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016329 buf[1] = NUL;
16330 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016331 rettv->v_type = VAR_STRING;
16332 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016333}
16334
16335/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016336 * "or(expr, expr)" function
16337 */
16338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016340{
16341 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16342 | get_tv_number_chk(&argvars[1], NULL);
16343}
16344
16345/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016346 * "pathshorten()" function
16347 */
16348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016349f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016350{
16351 char_u *p;
16352
16353 rettv->v_type = VAR_STRING;
16354 p = get_tv_string_chk(&argvars[0]);
16355 if (p == NULL)
16356 rettv->vval.v_string = NULL;
16357 else
16358 {
16359 p = vim_strsave(p);
16360 rettv->vval.v_string = p;
16361 if (p != NULL)
16362 shorten_dir(p);
16363 }
16364}
16365
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016366#ifdef FEAT_PERL
16367/*
16368 * "perleval()" function
16369 */
16370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016371f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016372{
16373 char_u *str;
16374 char_u buf[NUMBUFLEN];
16375
16376 str = get_tv_string_buf(&argvars[0], buf);
16377 do_perleval(str, rettv);
16378}
16379#endif
16380
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016381#ifdef FEAT_FLOAT
16382/*
16383 * "pow()" function
16384 */
16385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016386f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016387{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016388 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016389
16390 rettv->v_type = VAR_FLOAT;
16391 if (get_float_arg(argvars, &fx) == OK
16392 && get_float_arg(&argvars[1], &fy) == OK)
16393 rettv->vval.v_float = pow(fx, fy);
16394 else
16395 rettv->vval.v_float = 0.0;
16396}
16397#endif
16398
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016399/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016400 * "prevnonblank()" function
16401 */
16402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404{
16405 linenr_T lnum;
16406
16407 lnum = get_tv_lnum(argvars);
16408 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16409 lnum = 0;
16410 else
16411 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16412 --lnum;
16413 rettv->vval.v_number = lnum;
16414}
16415
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016416/* This dummy va_list is here because:
16417 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16418 * - locally in the function results in a "used before set" warning
16419 * - using va_start() to initialize it gives "function with fixed args" error */
16420static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016421
Bram Moolenaar8c711452005-01-14 21:53:12 +000016422/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016423 * "printf()" function
16424 */
16425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016427{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016428 char_u buf[NUMBUFLEN];
16429 int len;
16430 char_u *s;
16431 int saved_did_emsg = did_emsg;
16432 char *fmt;
16433
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016434 rettv->v_type = VAR_STRING;
16435 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016436
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016437 /* Get the required length, allocate the buffer and do it for real. */
16438 did_emsg = FALSE;
16439 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16440 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16441 if (!did_emsg)
16442 {
16443 s = alloc(len + 1);
16444 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016445 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016446 rettv->vval.v_string = s;
16447 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016448 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016449 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016450 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016451}
16452
16453/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016454 * "pumvisible()" function
16455 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016457f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016458{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016459#ifdef FEAT_INS_EXPAND
16460 if (pum_visible())
16461 rettv->vval.v_number = 1;
16462#endif
16463}
16464
Bram Moolenaardb913952012-06-29 12:54:53 +020016465#ifdef FEAT_PYTHON3
16466/*
16467 * "py3eval()" function
16468 */
16469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016470f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016471{
16472 char_u *str;
16473 char_u buf[NUMBUFLEN];
16474
16475 str = get_tv_string_buf(&argvars[0], buf);
16476 do_py3eval(str, rettv);
16477}
16478#endif
16479
16480#ifdef FEAT_PYTHON
16481/*
16482 * "pyeval()" function
16483 */
16484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016485f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016486{
16487 char_u *str;
16488 char_u buf[NUMBUFLEN];
16489
16490 str = get_tv_string_buf(&argvars[0], buf);
16491 do_pyeval(str, rettv);
16492}
16493#endif
16494
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016495/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016496 * "range()" function
16497 */
16498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016499f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016500{
16501 long start;
16502 long end;
16503 long stride = 1;
16504 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016505 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016507 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016508 if (argvars[1].v_type == VAR_UNKNOWN)
16509 {
16510 end = start - 1;
16511 start = 0;
16512 }
16513 else
16514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016515 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016516 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016518 }
16519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 if (error)
16521 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016522 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016523 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016524 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016525 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016526 else
16527 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016528 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016529 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016530 if (list_append_number(rettv->vval.v_list,
16531 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016532 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016533 }
16534}
16535
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016536/*
16537 * "readfile()" function
16538 */
16539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016540f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016541{
16542 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016543 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016544 char_u *fname;
16545 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16547 int io_size = sizeof(buf);
16548 int readlen; /* size of last fread() */
16549 char_u *prev = NULL; /* previously read bytes, if any */
16550 long prevlen = 0; /* length of data in prev */
16551 long prevsize = 0; /* size of prev buffer */
16552 long maxline = MAXLNUM;
16553 long cnt = 0;
16554 char_u *p; /* position in buf */
16555 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016557 if (argvars[1].v_type != VAR_UNKNOWN)
16558 {
16559 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16560 binary = TRUE;
16561 if (argvars[2].v_type != VAR_UNKNOWN)
16562 maxline = get_tv_number(&argvars[2]);
16563 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016564
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016565 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016566 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016567
16568 /* Always open the file in binary mode, library functions have a mind of
16569 * their own about CR-LF conversion. */
16570 fname = get_tv_string(&argvars[0]);
16571 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16572 {
16573 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16574 return;
16575 }
16576
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016577 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016578 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016580
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016581 /* This for loop processes what was read, but is also entered at end
16582 * of file so that either:
16583 * - an incomplete line gets written
16584 * - a "binary" file gets an empty line at the end if it ends in a
16585 * newline. */
16586 for (p = buf, start = buf;
16587 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16588 ++p)
16589 {
16590 if (*p == '\n' || readlen <= 0)
16591 {
16592 listitem_T *li;
16593 char_u *s = NULL;
16594 long_u len = p - start;
16595
16596 /* Finished a line. Remove CRs before NL. */
16597 if (readlen > 0 && !binary)
16598 {
16599 while (len > 0 && start[len - 1] == '\r')
16600 --len;
16601 /* removal may cross back to the "prev" string */
16602 if (len == 0)
16603 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16604 --prevlen;
16605 }
16606 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016607 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016608 else
16609 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016610 /* Change "prev" buffer to be the right size. This way
16611 * the bytes are only copied once, and very long lines are
16612 * allocated only once. */
16613 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016614 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016615 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016616 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016617 prev = NULL; /* the list will own the string */
16618 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016619 }
16620 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016621 if (s == NULL)
16622 {
16623 do_outofmem_msg((long_u) prevlen + len + 1);
16624 failed = TRUE;
16625 break;
16626 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016627
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016628 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629 {
16630 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016631 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016632 break;
16633 }
16634 li->li_tv.v_type = VAR_STRING;
16635 li->li_tv.v_lock = 0;
16636 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016637 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016639 start = p + 1; /* step over newline */
16640 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641 break;
16642 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 else if (*p == NUL)
16644 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016645#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16647 * when finding the BF and check the previous two bytes. */
16648 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016649 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016650 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16651 * + 1, these may be in the "prev" string. */
16652 char_u back1 = p >= buf + 1 ? p[-1]
16653 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16654 char_u back2 = p >= buf + 2 ? p[-2]
16655 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16656 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016657
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016658 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 char_u *dest = p - 2;
16661
16662 /* Usually a BOM is at the beginning of a file, and so at
16663 * the beginning of a line; then we can just step over it.
16664 */
16665 if (start == dest)
16666 start = p + 1;
16667 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016668 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016669 /* have to shuffle buf to close gap */
16670 int adjust_prevlen = 0;
16671
16672 if (dest < buf)
16673 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016674 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016675 dest = buf;
16676 }
16677 if (readlen > p - buf + 1)
16678 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16679 readlen -= 3 - adjust_prevlen;
16680 prevlen -= adjust_prevlen;
16681 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016682 }
16683 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016684 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016685#endif
16686 } /* for */
16687
16688 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16689 break;
16690 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016691 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016692 /* There's part of a line in buf, store it in "prev". */
16693 if (p - start + prevlen >= prevsize)
16694 {
16695 /* need bigger "prev" buffer */
16696 char_u *newprev;
16697
16698 /* A common use case is ordinary text files and "prev" gets a
16699 * fragment of a line, so the first allocation is made
16700 * small, to avoid repeatedly 'allocing' large and
16701 * 'reallocing' small. */
16702 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016703 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016704 else
16705 {
16706 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016707 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016708 prevsize = grow50pc > growmin ? grow50pc : growmin;
16709 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016710 newprev = prev == NULL ? alloc(prevsize)
16711 : vim_realloc(prev, prevsize);
16712 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016713 {
16714 do_outofmem_msg((long_u)prevsize);
16715 failed = TRUE;
16716 break;
16717 }
16718 prev = newprev;
16719 }
16720 /* Add the line part to end of "prev". */
16721 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016722 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016723 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016724 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016725
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016726 /*
16727 * For a negative line count use only the lines at the end of the file,
16728 * free the rest.
16729 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016730 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016731 while (cnt > -maxline)
16732 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016733 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016734 --cnt;
16735 }
16736
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016737 if (failed)
16738 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016739 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016740 /* readfile doc says an empty list is returned on error */
16741 rettv->vval.v_list = list_alloc();
16742 }
16743
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016744 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016745 fclose(fd);
16746}
16747
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016748#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016749static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016750
16751/*
16752 * Convert a List to proftime_T.
16753 * Return FAIL when there is something wrong.
16754 */
16755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016756list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016757{
16758 long n1, n2;
16759 int error = FALSE;
16760
16761 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16762 || arg->vval.v_list->lv_len != 2)
16763 return FAIL;
16764 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16765 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16766# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016767 tm->HighPart = n1;
16768 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016769# else
16770 tm->tv_sec = n1;
16771 tm->tv_usec = n2;
16772# endif
16773 return error ? FAIL : OK;
16774}
16775#endif /* FEAT_RELTIME */
16776
16777/*
16778 * "reltime()" function
16779 */
16780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016781f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016782{
16783#ifdef FEAT_RELTIME
16784 proftime_T res;
16785 proftime_T start;
16786
16787 if (argvars[0].v_type == VAR_UNKNOWN)
16788 {
16789 /* No arguments: get current time. */
16790 profile_start(&res);
16791 }
16792 else if (argvars[1].v_type == VAR_UNKNOWN)
16793 {
16794 if (list2proftime(&argvars[0], &res) == FAIL)
16795 return;
16796 profile_end(&res);
16797 }
16798 else
16799 {
16800 /* Two arguments: compute the difference. */
16801 if (list2proftime(&argvars[0], &start) == FAIL
16802 || list2proftime(&argvars[1], &res) == FAIL)
16803 return;
16804 profile_sub(&res, &start);
16805 }
16806
16807 if (rettv_list_alloc(rettv) == OK)
16808 {
16809 long n1, n2;
16810
16811# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016812 n1 = res.HighPart;
16813 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016814# else
16815 n1 = res.tv_sec;
16816 n2 = res.tv_usec;
16817# endif
16818 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16819 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16820 }
16821#endif
16822}
16823
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016824#ifdef FEAT_FLOAT
16825/*
16826 * "reltimefloat()" function
16827 */
16828 static void
16829f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16830{
16831# ifdef FEAT_RELTIME
16832 proftime_T tm;
16833# endif
16834
16835 rettv->v_type = VAR_FLOAT;
16836 rettv->vval.v_float = 0;
16837# ifdef FEAT_RELTIME
16838 if (list2proftime(&argvars[0], &tm) == OK)
16839 rettv->vval.v_float = profile_float(&tm);
16840# endif
16841}
16842#endif
16843
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016844/*
16845 * "reltimestr()" function
16846 */
16847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016848f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016849{
16850#ifdef FEAT_RELTIME
16851 proftime_T tm;
16852#endif
16853
16854 rettv->v_type = VAR_STRING;
16855 rettv->vval.v_string = NULL;
16856#ifdef FEAT_RELTIME
16857 if (list2proftime(&argvars[0], &tm) == OK)
16858 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16859#endif
16860}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016861
Bram Moolenaar0d660222005-01-07 21:51:51 +000016862#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016863static void make_connection(void);
16864static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016865
16866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016867make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016868{
16869 if (X_DISPLAY == NULL
16870# ifdef FEAT_GUI
16871 && !gui.in_use
16872# endif
16873 )
16874 {
16875 x_force_connect = TRUE;
16876 setup_term_clip();
16877 x_force_connect = FALSE;
16878 }
16879}
16880
16881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016882check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883{
16884 make_connection();
16885 if (X_DISPLAY == NULL)
16886 {
16887 EMSG(_("E240: No connection to Vim server"));
16888 return FAIL;
16889 }
16890 return OK;
16891}
16892#endif
16893
16894#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016896remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897{
16898 char_u *server_name;
16899 char_u *keys;
16900 char_u *r = NULL;
16901 char_u buf[NUMBUFLEN];
16902# ifdef WIN32
16903 HWND w;
16904# else
16905 Window w;
16906# endif
16907
16908 if (check_restricted() || check_secure())
16909 return;
16910
16911# ifdef FEAT_X11
16912 if (check_connection() == FAIL)
16913 return;
16914# endif
16915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016916 server_name = get_tv_string_chk(&argvars[0]);
16917 if (server_name == NULL)
16918 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016919 keys = get_tv_string_buf(&argvars[1], buf);
16920# ifdef WIN32
16921 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16922# else
16923 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16924 < 0)
16925# endif
16926 {
16927 if (r != NULL)
16928 EMSG(r); /* sending worked but evaluation failed */
16929 else
16930 EMSG2(_("E241: Unable to send to %s"), server_name);
16931 return;
16932 }
16933
16934 rettv->vval.v_string = r;
16935
16936 if (argvars[2].v_type != VAR_UNKNOWN)
16937 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016938 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016939 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016940 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016942 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016943 v.di_tv.v_type = VAR_STRING;
16944 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016945 idvar = get_tv_string_chk(&argvars[2]);
16946 if (idvar != NULL)
16947 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016948 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 }
16950}
16951#endif
16952
16953/*
16954 * "remote_expr()" function
16955 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016957f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958{
16959 rettv->v_type = VAR_STRING;
16960 rettv->vval.v_string = NULL;
16961#ifdef FEAT_CLIENTSERVER
16962 remote_common(argvars, rettv, TRUE);
16963#endif
16964}
16965
16966/*
16967 * "remote_foreground()" function
16968 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016970f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972#ifdef FEAT_CLIENTSERVER
16973# ifdef WIN32
16974 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016975 {
16976 char_u *server_name = get_tv_string_chk(&argvars[0]);
16977
16978 if (server_name != NULL)
16979 serverForeground(server_name);
16980 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981# else
16982 /* Send a foreground() expression to the server. */
16983 argvars[1].v_type = VAR_STRING;
16984 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16985 argvars[2].v_type = VAR_UNKNOWN;
16986 remote_common(argvars, rettv, TRUE);
16987 vim_free(argvars[1].vval.v_string);
16988# endif
16989#endif
16990}
16991
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016993f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016994{
16995#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016996 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 char_u *s = NULL;
16998# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016999 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017002
17003 if (check_restricted() || check_secure())
17004 {
17005 rettv->vval.v_number = -1;
17006 return;
17007 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017008 serverid = get_tv_string_chk(&argvars[0]);
17009 if (serverid == NULL)
17010 {
17011 rettv->vval.v_number = -1;
17012 return; /* type error; errmsg already given */
17013 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017015 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016 if (n == 0)
17017 rettv->vval.v_number = -1;
17018 else
17019 {
17020 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17021 rettv->vval.v_number = (s != NULL);
17022 }
17023# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017024 if (check_connection() == FAIL)
17025 return;
17026
17027 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017028 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017029# endif
17030
17031 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017033 char_u *retvar;
17034
Bram Moolenaar33570922005-01-25 22:26:29 +000017035 v.di_tv.v_type = VAR_STRING;
17036 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 retvar = get_tv_string_chk(&argvars[1]);
17038 if (retvar != NULL)
17039 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017040 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041 }
17042#else
17043 rettv->vval.v_number = -1;
17044#endif
17045}
17046
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017048f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049{
17050 char_u *r = NULL;
17051
17052#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017053 char_u *serverid = get_tv_string_chk(&argvars[0]);
17054
17055 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 {
17057# ifdef WIN32
17058 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017059 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017061 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062 if (n != 0)
17063 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17064 if (r == NULL)
17065# else
17066 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068# endif
17069 EMSG(_("E277: Unable to read a server reply"));
17070 }
17071#endif
17072 rettv->v_type = VAR_STRING;
17073 rettv->vval.v_string = r;
17074}
17075
17076/*
17077 * "remote_send()" function
17078 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017080f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081{
17082 rettv->v_type = VAR_STRING;
17083 rettv->vval.v_string = NULL;
17084#ifdef FEAT_CLIENTSERVER
17085 remote_common(argvars, rettv, FALSE);
17086#endif
17087}
17088
17089/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017090 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017091 */
17092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017093f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017094{
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 list_T *l;
17096 listitem_T *item, *item2;
17097 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017098 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017099 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017100 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017101 dict_T *d;
17102 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017103 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017104
Bram Moolenaar8c711452005-01-14 21:53:12 +000017105 if (argvars[0].v_type == VAR_DICT)
17106 {
17107 if (argvars[2].v_type != VAR_UNKNOWN)
17108 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017109 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017110 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017111 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017112 key = get_tv_string_chk(&argvars[1]);
17113 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017115 di = dict_find(d, key, -1);
17116 if (di == NULL)
17117 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017118 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17119 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 {
17121 *rettv = di->di_tv;
17122 init_tv(&di->di_tv);
17123 dictitem_remove(d, di);
17124 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017125 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017126 }
17127 }
17128 else if (argvars[0].v_type != VAR_LIST)
17129 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017130 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017131 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017133 int error = FALSE;
17134
17135 idx = get_tv_number_chk(&argvars[1], &error);
17136 if (error)
17137 ; /* type error: do nothing, errmsg already given */
17138 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017139 EMSGN(_(e_listidx), idx);
17140 else
17141 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017142 if (argvars[2].v_type == VAR_UNKNOWN)
17143 {
17144 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017145 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017146 *rettv = item->li_tv;
17147 vim_free(item);
17148 }
17149 else
17150 {
17151 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 end = get_tv_number_chk(&argvars[2], &error);
17153 if (error)
17154 ; /* type error: do nothing */
17155 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 EMSGN(_(e_listidx), end);
17157 else
17158 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017159 int cnt = 0;
17160
17161 for (li = item; li != NULL; li = li->li_next)
17162 {
17163 ++cnt;
17164 if (li == item2)
17165 break;
17166 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017167 if (li == NULL) /* didn't find "item2" after "item" */
17168 EMSG(_(e_invrange));
17169 else
17170 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017171 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017172 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017173 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017174 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017175 l->lv_first = item;
17176 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017177 item->li_prev = NULL;
17178 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017179 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017180 }
17181 }
17182 }
17183 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017184 }
17185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017186}
17187
17188/*
17189 * "rename({from}, {to})" function
17190 */
17191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017192f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017193{
17194 char_u buf[NUMBUFLEN];
17195
17196 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17200 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017201}
17202
17203/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017204 * "repeat()" function
17205 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017207f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017208{
17209 char_u *p;
17210 int n;
17211 int slen;
17212 int len;
17213 char_u *r;
17214 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017215
17216 n = get_tv_number(&argvars[1]);
17217 if (argvars[0].v_type == VAR_LIST)
17218 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017219 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017220 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017221 if (list_extend(rettv->vval.v_list,
17222 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017223 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017224 }
17225 else
17226 {
17227 p = get_tv_string(&argvars[0]);
17228 rettv->v_type = VAR_STRING;
17229 rettv->vval.v_string = NULL;
17230
17231 slen = (int)STRLEN(p);
17232 len = slen * n;
17233 if (len <= 0)
17234 return;
17235
17236 r = alloc(len + 1);
17237 if (r != NULL)
17238 {
17239 for (i = 0; i < n; i++)
17240 mch_memmove(r + i * slen, p, (size_t)slen);
17241 r[len] = NUL;
17242 }
17243
17244 rettv->vval.v_string = r;
17245 }
17246}
17247
17248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 * "resolve()" function
17250 */
17251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017252f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253{
17254 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017255#ifdef HAVE_READLINK
17256 char_u *buf = NULL;
17257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017259 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017260#ifdef FEAT_SHORTCUT
17261 {
17262 char_u *v = NULL;
17263
17264 v = mch_resolve_shortcut(p);
17265 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017266 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017268 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017269 }
17270#else
17271# ifdef HAVE_READLINK
17272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 char_u *cpy;
17274 int len;
17275 char_u *remain = NULL;
17276 char_u *q;
17277 int is_relative_to_current = FALSE;
17278 int has_trailing_pathsep = FALSE;
17279 int limit = 100;
17280
17281 p = vim_strsave(p);
17282
17283 if (p[0] == '.' && (vim_ispathsep(p[1])
17284 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17285 is_relative_to_current = TRUE;
17286
17287 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017288 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017291 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017293
17294 q = getnextcomp(p);
17295 if (*q != NUL)
17296 {
17297 /* Separate the first path component in "p", and keep the
17298 * remainder (beginning with the path separator). */
17299 remain = vim_strsave(q - 1);
17300 q[-1] = NUL;
17301 }
17302
Bram Moolenaard9462e32011-04-11 21:35:11 +020017303 buf = alloc(MAXPATHL + 1);
17304 if (buf == NULL)
17305 goto fail;
17306
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 for (;;)
17308 {
17309 for (;;)
17310 {
17311 len = readlink((char *)p, (char *)buf, MAXPATHL);
17312 if (len <= 0)
17313 break;
17314 buf[len] = NUL;
17315
17316 if (limit-- == 0)
17317 {
17318 vim_free(p);
17319 vim_free(remain);
17320 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017321 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 goto fail;
17323 }
17324
17325 /* Ensure that the result will have a trailing path separator
17326 * if the argument has one. */
17327 if (remain == NULL && has_trailing_pathsep)
17328 add_pathsep(buf);
17329
17330 /* Separate the first path component in the link value and
17331 * concatenate the remainders. */
17332 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17333 if (*q != NUL)
17334 {
17335 if (remain == NULL)
17336 remain = vim_strsave(q - 1);
17337 else
17338 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017339 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 if (cpy != NULL)
17341 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017342 vim_free(remain);
17343 remain = cpy;
17344 }
17345 }
17346 q[-1] = NUL;
17347 }
17348
17349 q = gettail(p);
17350 if (q > p && *q == NUL)
17351 {
17352 /* Ignore trailing path separator. */
17353 q[-1] = NUL;
17354 q = gettail(p);
17355 }
17356 if (q > p && !mch_isFullName(buf))
17357 {
17358 /* symlink is relative to directory of argument */
17359 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17360 if (cpy != NULL)
17361 {
17362 STRCPY(cpy, p);
17363 STRCPY(gettail(cpy), buf);
17364 vim_free(p);
17365 p = cpy;
17366 }
17367 }
17368 else
17369 {
17370 vim_free(p);
17371 p = vim_strsave(buf);
17372 }
17373 }
17374
17375 if (remain == NULL)
17376 break;
17377
17378 /* Append the first path component of "remain" to "p". */
17379 q = getnextcomp(remain + 1);
17380 len = q - remain - (*q != NUL);
17381 cpy = vim_strnsave(p, STRLEN(p) + len);
17382 if (cpy != NULL)
17383 {
17384 STRNCAT(cpy, remain, len);
17385 vim_free(p);
17386 p = cpy;
17387 }
17388 /* Shorten "remain". */
17389 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017390 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391 else
17392 {
17393 vim_free(remain);
17394 remain = NULL;
17395 }
17396 }
17397
17398 /* If the result is a relative path name, make it explicitly relative to
17399 * the current directory if and only if the argument had this form. */
17400 if (!vim_ispathsep(*p))
17401 {
17402 if (is_relative_to_current
17403 && *p != NUL
17404 && !(p[0] == '.'
17405 && (p[1] == NUL
17406 || vim_ispathsep(p[1])
17407 || (p[1] == '.'
17408 && (p[2] == NUL
17409 || vim_ispathsep(p[2]))))))
17410 {
17411 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017412 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 if (cpy != NULL)
17414 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 vim_free(p);
17416 p = cpy;
17417 }
17418 }
17419 else if (!is_relative_to_current)
17420 {
17421 /* Strip leading "./". */
17422 q = p;
17423 while (q[0] == '.' && vim_ispathsep(q[1]))
17424 q += 2;
17425 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017426 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 }
17428 }
17429
17430 /* Ensure that the result will have no trailing path separator
17431 * if the argument had none. But keep "/" or "//". */
17432 if (!has_trailing_pathsep)
17433 {
17434 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017435 if (after_pathsep(p, q))
17436 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437 }
17438
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017439 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 }
17441# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017442 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443# endif
17444#endif
17445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017446 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447
17448#ifdef HAVE_READLINK
17449fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017450 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453}
17454
17455/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017456 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 */
17458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017459f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460{
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 list_T *l;
17462 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017463
Bram Moolenaar0d660222005-01-07 21:51:51 +000017464 if (argvars[0].v_type != VAR_LIST)
17465 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017466 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017467 && !tv_check_lock(l->lv_lock,
17468 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017469 {
17470 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017471 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017472 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017473 while (li != NULL)
17474 {
17475 ni = li->li_prev;
17476 list_append(l, li);
17477 li = ni;
17478 }
17479 rettv->vval.v_list = l;
17480 rettv->v_type = VAR_LIST;
17481 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017482 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484}
17485
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017486#define SP_NOMOVE 0x01 /* don't move cursor */
17487#define SP_REPEAT 0x02 /* repeat to find outer pair */
17488#define SP_RETCOUNT 0x04 /* return matchcount */
17489#define SP_SETPCMARK 0x08 /* set previous context mark */
17490#define SP_START 0x10 /* accept match at start position */
17491#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17492#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017493#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017494
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017495static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017496
17497/*
17498 * Get flags for a search function.
17499 * Possibly sets "p_ws".
17500 * Returns BACKWARD, FORWARD or zero (for an error).
17501 */
17502 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017503get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017504{
17505 int dir = FORWARD;
17506 char_u *flags;
17507 char_u nbuf[NUMBUFLEN];
17508 int mask;
17509
17510 if (varp->v_type != VAR_UNKNOWN)
17511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017512 flags = get_tv_string_buf_chk(varp, nbuf);
17513 if (flags == NULL)
17514 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017515 while (*flags != NUL)
17516 {
17517 switch (*flags)
17518 {
17519 case 'b': dir = BACKWARD; break;
17520 case 'w': p_ws = TRUE; break;
17521 case 'W': p_ws = FALSE; break;
17522 default: mask = 0;
17523 if (flagsp != NULL)
17524 switch (*flags)
17525 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017526 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017527 case 'e': mask = SP_END; break;
17528 case 'm': mask = SP_RETCOUNT; break;
17529 case 'n': mask = SP_NOMOVE; break;
17530 case 'p': mask = SP_SUBPAT; break;
17531 case 'r': mask = SP_REPEAT; break;
17532 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017533 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 }
17535 if (mask == 0)
17536 {
17537 EMSG2(_(e_invarg2), flags);
17538 dir = 0;
17539 }
17540 else
17541 *flagsp |= mask;
17542 }
17543 if (dir == 0)
17544 break;
17545 ++flags;
17546 }
17547 }
17548 return dir;
17549}
17550
Bram Moolenaar071d4272004-06-13 20:20:40 +000017551/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017552 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017555search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017557 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017558 char_u *pat;
17559 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017560 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 int save_p_ws = p_ws;
17562 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017563 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017564 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017565 proftime_T tm;
17566#ifdef FEAT_RELTIME
17567 long time_limit = 0;
17568#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017569 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017570 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017571
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017572 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017573 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017574 if (dir == 0)
17575 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017576 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017577 if (flags & SP_START)
17578 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017579 if (flags & SP_END)
17580 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017581 if (flags & SP_COLUMN)
17582 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017583
Bram Moolenaar76929292008-01-06 19:07:36 +000017584 /* Optional arguments: line number to stop searching and timeout. */
17585 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017586 {
17587 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17588 if (lnum_stop < 0)
17589 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017590#ifdef FEAT_RELTIME
17591 if (argvars[3].v_type != VAR_UNKNOWN)
17592 {
17593 time_limit = get_tv_number_chk(&argvars[3], NULL);
17594 if (time_limit < 0)
17595 goto theend;
17596 }
17597#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017598 }
17599
Bram Moolenaar76929292008-01-06 19:07:36 +000017600#ifdef FEAT_RELTIME
17601 /* Set the time limit, if there is one. */
17602 profile_setlimit(time_limit, &tm);
17603#endif
17604
Bram Moolenaar231334e2005-07-25 20:46:57 +000017605 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017606 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017607 * Check to make sure only those flags are set.
17608 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17609 * flags cannot be set. Check for that condition also.
17610 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017611 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017612 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017615 goto theend;
17616 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017618 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017619 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017620 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017621 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017623 if (flags & SP_SUBPAT)
17624 retval = subpatnum;
17625 else
17626 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017627 if (flags & SP_SETPCMARK)
17628 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017629 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017630 if (match_pos != NULL)
17631 {
17632 /* Store the match cursor position */
17633 match_pos->lnum = pos.lnum;
17634 match_pos->col = pos.col + 1;
17635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 /* "/$" will put the cursor after the end of the line, may need to
17637 * correct that here */
17638 check_cursor();
17639 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017640
17641 /* If 'n' flag is used: restore cursor position. */
17642 if (flags & SP_NOMOVE)
17643 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017644 else
17645 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017646theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017648
17649 return retval;
17650}
17651
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017652#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017653
17654/*
17655 * round() is not in C90, use ceil() or floor() instead.
17656 */
17657 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017658vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017659{
17660 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17661}
17662
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017663/*
17664 * "round({float})" function
17665 */
17666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017667f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017668{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017669 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017670
17671 rettv->v_type = VAR_FLOAT;
17672 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017673 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017674 else
17675 rettv->vval.v_float = 0.0;
17676}
17677#endif
17678
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017679/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017680 * "screenattr()" function
17681 */
17682 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017683f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017684{
17685 int row;
17686 int col;
17687 int c;
17688
17689 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17690 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17691 if (row < 0 || row >= screen_Rows
17692 || col < 0 || col >= screen_Columns)
17693 c = -1;
17694 else
17695 c = ScreenAttrs[LineOffset[row] + col];
17696 rettv->vval.v_number = c;
17697}
17698
17699/*
17700 * "screenchar()" function
17701 */
17702 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017703f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017704{
17705 int row;
17706 int col;
17707 int off;
17708 int c;
17709
17710 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17711 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17712 if (row < 0 || row >= screen_Rows
17713 || col < 0 || col >= screen_Columns)
17714 c = -1;
17715 else
17716 {
17717 off = LineOffset[row] + col;
17718#ifdef FEAT_MBYTE
17719 if (enc_utf8 && ScreenLinesUC[off] != 0)
17720 c = ScreenLinesUC[off];
17721 else
17722#endif
17723 c = ScreenLines[off];
17724 }
17725 rettv->vval.v_number = c;
17726}
17727
17728/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017729 * "screencol()" function
17730 *
17731 * First column is 1 to be consistent with virtcol().
17732 */
17733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017734f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017735{
17736 rettv->vval.v_number = screen_screencol() + 1;
17737}
17738
17739/*
17740 * "screenrow()" function
17741 */
17742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017743f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017744{
17745 rettv->vval.v_number = screen_screenrow() + 1;
17746}
17747
17748/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017749 * "search()" function
17750 */
17751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017752f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017753{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017754 int flags = 0;
17755
17756 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757}
17758
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017760 * "searchdecl()" function
17761 */
17762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017763f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017764{
17765 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017766 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017767 int error = FALSE;
17768 char_u *name;
17769
17770 rettv->vval.v_number = 1; /* default: FAIL */
17771
17772 name = get_tv_string_chk(&argvars[0]);
17773 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017774 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017775 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017776 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17777 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17778 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017779 if (!error && name != NULL)
17780 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017781 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017782}
17783
17784/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017785 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017788searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789{
17790 char_u *spat, *mpat, *epat;
17791 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793 int dir;
17794 int flags = 0;
17795 char_u nbuf1[NUMBUFLEN];
17796 char_u nbuf2[NUMBUFLEN];
17797 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017798 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017799 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017800 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801
Bram Moolenaar071d4272004-06-13 20:20:40 +000017802 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017803 spat = get_tv_string_chk(&argvars[0]);
17804 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17805 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17806 if (spat == NULL || mpat == NULL || epat == NULL)
17807 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 /* Handle the optional fourth argument: flags */
17810 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017811 if (dir == 0)
17812 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017813
17814 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017815 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17816 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017817 if ((flags & (SP_END | SP_SUBPAT)) != 0
17818 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017819 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017820 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017821 goto theend;
17822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017824 /* Using 'r' implies 'W', otherwise it doesn't work. */
17825 if (flags & SP_REPEAT)
17826 p_ws = FALSE;
17827
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017828 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017829 if (argvars[3].v_type == VAR_UNKNOWN
17830 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 skip = (char_u *)"";
17832 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017834 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017835 if (argvars[5].v_type != VAR_UNKNOWN)
17836 {
17837 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17838 if (lnum_stop < 0)
17839 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017840#ifdef FEAT_RELTIME
17841 if (argvars[6].v_type != VAR_UNKNOWN)
17842 {
17843 time_limit = get_tv_number_chk(&argvars[6], NULL);
17844 if (time_limit < 0)
17845 goto theend;
17846 }
17847#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017848 }
17849 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017850 if (skip == NULL)
17851 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017853 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017854 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017855
17856theend:
17857 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017858
17859 return retval;
17860}
17861
17862/*
17863 * "searchpair()" function
17864 */
17865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017866f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017867{
17868 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17869}
17870
17871/*
17872 * "searchpairpos()" function
17873 */
17874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017875f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017876{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017877 pos_T match_pos;
17878 int lnum = 0;
17879 int col = 0;
17880
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017881 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017882 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017883
17884 if (searchpair_cmn(argvars, &match_pos) > 0)
17885 {
17886 lnum = match_pos.lnum;
17887 col = match_pos.col;
17888 }
17889
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017890 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17891 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017892}
17893
17894/*
17895 * Search for a start/middle/end thing.
17896 * Used by searchpair(), see its documentation for the details.
17897 * Returns 0 or -1 for no match,
17898 */
17899 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017900do_searchpair(
17901 char_u *spat, /* start pattern */
17902 char_u *mpat, /* middle pattern */
17903 char_u *epat, /* end pattern */
17904 int dir, /* BACKWARD or FORWARD */
17905 char_u *skip, /* skip expression */
17906 int flags, /* SP_SETPCMARK and other SP_ values */
17907 pos_T *match_pos,
17908 linenr_T lnum_stop, /* stop at this line if not zero */
17909 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017910{
17911 char_u *save_cpo;
17912 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17913 long retval = 0;
17914 pos_T pos;
17915 pos_T firstpos;
17916 pos_T foundpos;
17917 pos_T save_cursor;
17918 pos_T save_pos;
17919 int n;
17920 int r;
17921 int nest = 1;
17922 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017923 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017924 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017925
17926 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17927 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017928 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017929
Bram Moolenaar76929292008-01-06 19:07:36 +000017930#ifdef FEAT_RELTIME
17931 /* Set the time limit, if there is one. */
17932 profile_setlimit(time_limit, &tm);
17933#endif
17934
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017935 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17936 * start/middle/end (pat3, for the top pair). */
17937 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17938 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17939 if (pat2 == NULL || pat3 == NULL)
17940 goto theend;
17941 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17942 if (*mpat == NUL)
17943 STRCPY(pat3, pat2);
17944 else
17945 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17946 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017947 if (flags & SP_START)
17948 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017949
Bram Moolenaar071d4272004-06-13 20:20:40 +000017950 save_cursor = curwin->w_cursor;
17951 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017952 clearpos(&firstpos);
17953 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017954 pat = pat3;
17955 for (;;)
17956 {
17957 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017958 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17960 /* didn't find it or found the first match again: FAIL */
17961 break;
17962
17963 if (firstpos.lnum == 0)
17964 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017965 if (equalpos(pos, foundpos))
17966 {
17967 /* Found the same position again. Can happen with a pattern that
17968 * has "\zs" at the end and searching backwards. Advance one
17969 * character and try again. */
17970 if (dir == BACKWARD)
17971 decl(&pos);
17972 else
17973 incl(&pos);
17974 }
17975 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017977 /* clear the start flag to avoid getting stuck here */
17978 options &= ~SEARCH_START;
17979
Bram Moolenaar071d4272004-06-13 20:20:40 +000017980 /* If the skip pattern matches, ignore this match. */
17981 if (*skip != NUL)
17982 {
17983 save_pos = curwin->w_cursor;
17984 curwin->w_cursor = pos;
17985 r = eval_to_bool(skip, &err, NULL, FALSE);
17986 curwin->w_cursor = save_pos;
17987 if (err)
17988 {
17989 /* Evaluating {skip} caused an error, break here. */
17990 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017991 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017992 break;
17993 }
17994 if (r)
17995 continue;
17996 }
17997
17998 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17999 {
18000 /* Found end when searching backwards or start when searching
18001 * forward: nested pair. */
18002 ++nest;
18003 pat = pat2; /* nested, don't search for middle */
18004 }
18005 else
18006 {
18007 /* Found end when searching forward or start when searching
18008 * backward: end of (nested) pair; or found middle in outer pair. */
18009 if (--nest == 1)
18010 pat = pat3; /* outer level, search for middle */
18011 }
18012
18013 if (nest == 0)
18014 {
18015 /* Found the match: return matchcount or line number. */
18016 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018017 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018019 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018020 if (flags & SP_SETPCMARK)
18021 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018022 curwin->w_cursor = pos;
18023 if (!(flags & SP_REPEAT))
18024 break;
18025 nest = 1; /* search for next unmatched */
18026 }
18027 }
18028
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018029 if (match_pos != NULL)
18030 {
18031 /* Store the match cursor position */
18032 match_pos->lnum = curwin->w_cursor.lnum;
18033 match_pos->col = curwin->w_cursor.col + 1;
18034 }
18035
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018037 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038 curwin->w_cursor = save_cursor;
18039
18040theend:
18041 vim_free(pat2);
18042 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018043 if (p_cpo == empty_option)
18044 p_cpo = save_cpo;
18045 else
18046 /* Darn, evaluating the {skip} expression changed the value. */
18047 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018048
18049 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050}
18051
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018052/*
18053 * "searchpos()" function
18054 */
18055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018056f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018057{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018058 pos_T match_pos;
18059 int lnum = 0;
18060 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018061 int n;
18062 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018064 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018065 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018066
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018067 n = search_cmn(argvars, &match_pos, &flags);
18068 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018069 {
18070 lnum = match_pos.lnum;
18071 col = match_pos.col;
18072 }
18073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018074 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18075 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018076 if (flags & SP_SUBPAT)
18077 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018078}
18079
Bram Moolenaar0d660222005-01-07 21:51:51 +000018080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018081f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018082{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018083#ifdef FEAT_CLIENTSERVER
18084 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018085 char_u *server = get_tv_string_chk(&argvars[0]);
18086 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087
Bram Moolenaar0d660222005-01-07 21:51:51 +000018088 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 if (server == NULL || reply == NULL)
18090 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018091 if (check_restricted() || check_secure())
18092 return;
18093# ifdef FEAT_X11
18094 if (check_connection() == FAIL)
18095 return;
18096# endif
18097
18098 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018100 EMSG(_("E258: Unable to send to client"));
18101 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018103 rettv->vval.v_number = 0;
18104#else
18105 rettv->vval.v_number = -1;
18106#endif
18107}
18108
Bram Moolenaar0d660222005-01-07 21:51:51 +000018109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018110f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018111{
18112 char_u *r = NULL;
18113
18114#ifdef FEAT_CLIENTSERVER
18115# ifdef WIN32
18116 r = serverGetVimNames();
18117# else
18118 make_connection();
18119 if (X_DISPLAY != NULL)
18120 r = serverGetVimNames(X_DISPLAY);
18121# endif
18122#endif
18123 rettv->v_type = VAR_STRING;
18124 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125}
18126
18127/*
18128 * "setbufvar()" function
18129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018131f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018132{
18133 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018134 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018136 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 char_u nbuf[NUMBUFLEN];
18138
18139 if (check_restricted() || check_secure())
18140 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018141 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18142 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018143 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 varp = &argvars[2];
18145
18146 if (buf != NULL && varname != NULL && varp != NULL)
18147 {
18148 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150
18151 if (*varname == '&')
18152 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018153 long numval;
18154 char_u *strval;
18155 int error = FALSE;
18156
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018158 numval = get_tv_number_chk(varp, &error);
18159 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018160 if (!error && strval != NULL)
18161 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018162 }
18163 else
18164 {
18165 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18166 if (bufvarname != NULL)
18167 {
18168 STRCPY(bufvarname, "b:");
18169 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018170 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 vim_free(bufvarname);
18172 }
18173 }
18174
18175 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178}
18179
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018181f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018182{
18183 dict_T *d;
18184 dictitem_T *di;
18185 char_u *csearch;
18186
18187 if (argvars[0].v_type != VAR_DICT)
18188 {
18189 EMSG(_(e_dictreq));
18190 return;
18191 }
18192
18193 if ((d = argvars[0].vval.v_dict) != NULL)
18194 {
18195 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18196 if (csearch != NULL)
18197 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018198#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018199 if (enc_utf8)
18200 {
18201 int pcc[MAX_MCO];
18202 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018203
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018204 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18205 }
18206 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018207#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018208 set_last_csearch(PTR2CHAR(csearch),
18209 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018210 }
18211
18212 di = dict_find(d, (char_u *)"forward", -1);
18213 if (di != NULL)
18214 set_csearch_direction(get_tv_number(&di->di_tv)
18215 ? FORWARD : BACKWARD);
18216
18217 di = dict_find(d, (char_u *)"until", -1);
18218 if (di != NULL)
18219 set_csearch_until(!!get_tv_number(&di->di_tv));
18220 }
18221}
18222
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223/*
18224 * "setcmdpos()" function
18225 */
18226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018227f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018229 int pos = (int)get_tv_number(&argvars[0]) - 1;
18230
18231 if (pos >= 0)
18232 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233}
18234
18235/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018236 * "setfperm({fname}, {mode})" function
18237 */
18238 static void
18239f_setfperm(typval_T *argvars, typval_T *rettv)
18240{
18241 char_u *fname;
18242 char_u modebuf[NUMBUFLEN];
18243 char_u *mode_str;
18244 int i;
18245 int mask;
18246 int mode = 0;
18247
18248 rettv->vval.v_number = 0;
18249 fname = get_tv_string_chk(&argvars[0]);
18250 if (fname == NULL)
18251 return;
18252 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18253 if (mode_str == NULL)
18254 return;
18255 if (STRLEN(mode_str) != 9)
18256 {
18257 EMSG2(_(e_invarg2), mode_str);
18258 return;
18259 }
18260
18261 mask = 1;
18262 for (i = 8; i >= 0; --i)
18263 {
18264 if (mode_str[i] != '-')
18265 mode |= mask;
18266 mask = mask << 1;
18267 }
18268 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18269}
18270
18271/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 * "setline()" function
18273 */
18274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018275f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276{
18277 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018278 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018279 list_T *l = NULL;
18280 listitem_T *li = NULL;
18281 long added = 0;
18282 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018284 lnum = get_tv_lnum(&argvars[0]);
18285 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018287 l = argvars[1].vval.v_list;
18288 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018289 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018290 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018291 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018292
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018293 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018294 for (;;)
18295 {
18296 if (l != NULL)
18297 {
18298 /* list argument, get next string */
18299 if (li == NULL)
18300 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018302 li = li->li_next;
18303 }
18304
18305 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018306 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018307 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018308
18309 /* When coming here from Insert mode, sync undo, so that this can be
18310 * undone separately from what was previously inserted. */
18311 if (u_sync_once == 2)
18312 {
18313 u_sync_once = 1; /* notify that u_sync() was called */
18314 u_sync(TRUE);
18315 }
18316
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018317 if (lnum <= curbuf->b_ml.ml_line_count)
18318 {
18319 /* existing line, replace it */
18320 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18321 {
18322 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018323 if (lnum == curwin->w_cursor.lnum)
18324 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018325 rettv->vval.v_number = 0; /* OK */
18326 }
18327 }
18328 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18329 {
18330 /* lnum is one past the last line, append the line */
18331 ++added;
18332 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18333 rettv->vval.v_number = 0; /* OK */
18334 }
18335
18336 if (l == NULL) /* only one string argument */
18337 break;
18338 ++lnum;
18339 }
18340
18341 if (added > 0)
18342 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018343}
18344
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018345static 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 +000018346
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018348 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018349 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018351set_qf_ll_list(
18352 win_T *wp UNUSED,
18353 typval_T *list_arg UNUSED,
18354 typval_T *action_arg UNUSED,
18355 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018356{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018357#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018358 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018359 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018360 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018361#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018362
Bram Moolenaar2641f772005-03-25 21:58:17 +000018363 rettv->vval.v_number = -1;
18364
18365#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018366 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018367 EMSG(_(e_listreq));
18368 else
18369 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018370 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018371
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018372 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018373 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018374 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018375 if (act == NULL)
18376 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018377 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018378 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018379 else
18380 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018381 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018382 else if (action_arg->v_type == VAR_UNKNOWN)
18383 action = ' ';
18384 else
18385 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018386
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018387 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018388 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018389 rettv->vval.v_number = 0;
18390 }
18391#endif
18392}
18393
18394/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018395 * "setloclist()" function
18396 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018398f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018399{
18400 win_T *win;
18401
18402 rettv->vval.v_number = -1;
18403
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018404 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018405 if (win != NULL)
18406 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18407}
18408
18409/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018410 * "setmatches()" function
18411 */
18412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018413f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018414{
18415#ifdef FEAT_SEARCH_EXTRA
18416 list_T *l;
18417 listitem_T *li;
18418 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018419 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018420
18421 rettv->vval.v_number = -1;
18422 if (argvars[0].v_type != VAR_LIST)
18423 {
18424 EMSG(_(e_listreq));
18425 return;
18426 }
18427 if ((l = argvars[0].vval.v_list) != NULL)
18428 {
18429
18430 /* To some extent make sure that we are dealing with a list from
18431 * "getmatches()". */
18432 li = l->lv_first;
18433 while (li != NULL)
18434 {
18435 if (li->li_tv.v_type != VAR_DICT
18436 || (d = li->li_tv.vval.v_dict) == NULL)
18437 {
18438 EMSG(_(e_invarg));
18439 return;
18440 }
18441 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018442 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18443 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018444 && dict_find(d, (char_u *)"priority", -1) != NULL
18445 && dict_find(d, (char_u *)"id", -1) != NULL))
18446 {
18447 EMSG(_(e_invarg));
18448 return;
18449 }
18450 li = li->li_next;
18451 }
18452
18453 clear_matches(curwin);
18454 li = l->lv_first;
18455 while (li != NULL)
18456 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018457 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018458 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018459 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018460 char_u *group;
18461 int priority;
18462 int id;
18463 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018464
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018465 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018466 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18467 {
18468 if (s == NULL)
18469 {
18470 s = list_alloc();
18471 if (s == NULL)
18472 return;
18473 }
18474
18475 /* match from matchaddpos() */
18476 for (i = 1; i < 9; i++)
18477 {
18478 sprintf((char *)buf, (char *)"pos%d", i);
18479 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18480 {
18481 if (di->di_tv.v_type != VAR_LIST)
18482 return;
18483
18484 list_append_tv(s, &di->di_tv);
18485 s->lv_refcount++;
18486 }
18487 else
18488 break;
18489 }
18490 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018491
18492 group = get_dict_string(d, (char_u *)"group", FALSE);
18493 priority = (int)get_dict_number(d, (char_u *)"priority");
18494 id = (int)get_dict_number(d, (char_u *)"id");
18495 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18496 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18497 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018498 if (i == 0)
18499 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018500 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018501 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018502 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018503 }
18504 else
18505 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018506 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018507 list_unref(s);
18508 s = NULL;
18509 }
18510
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018511 li = li->li_next;
18512 }
18513 rettv->vval.v_number = 0;
18514 }
18515#endif
18516}
18517
18518/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018519 * "setpos()" function
18520 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018522f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018523{
18524 pos_T pos;
18525 int fnum;
18526 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018527 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018528
Bram Moolenaar08250432008-02-13 11:42:46 +000018529 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018530 name = get_tv_string_chk(argvars);
18531 if (name != NULL)
18532 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018533 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018534 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018535 if (--pos.col < 0)
18536 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018537 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018538 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018539 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018540 if (fnum == curbuf->b_fnum)
18541 {
18542 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018543 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018544 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018545 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018546 curwin->w_set_curswant = FALSE;
18547 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018548 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018549 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018550 }
18551 else
18552 EMSG(_(e_invarg));
18553 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018554 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18555 {
18556 /* set mark */
18557 if (setmark_pos(name[1], &pos, fnum) == OK)
18558 rettv->vval.v_number = 0;
18559 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018560 else
18561 EMSG(_(e_invarg));
18562 }
18563 }
18564}
18565
18566/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018567 * "setqflist()" function
18568 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018570f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018571{
18572 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18573}
18574
18575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 * "setreg()" function
18577 */
18578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018579f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580{
18581 int regname;
18582 char_u *strregname;
18583 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018584 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585 int append;
18586 char_u yank_type;
18587 long block_len;
18588
18589 block_len = -1;
18590 yank_type = MAUTO;
18591 append = FALSE;
18592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018593 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018596 if (strregname == NULL)
18597 return; /* type error; errmsg already given */
18598 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599 if (regname == 0 || regname == '@')
18600 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018601
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018602 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018604 stropt = get_tv_string_chk(&argvars[2]);
18605 if (stropt == NULL)
18606 return; /* type error */
18607 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018608 switch (*stropt)
18609 {
18610 case 'a': case 'A': /* append */
18611 append = TRUE;
18612 break;
18613 case 'v': case 'c': /* character-wise selection */
18614 yank_type = MCHAR;
18615 break;
18616 case 'V': case 'l': /* line-wise selection */
18617 yank_type = MLINE;
18618 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619 case 'b': case Ctrl_V: /* block-wise selection */
18620 yank_type = MBLOCK;
18621 if (VIM_ISDIGIT(stropt[1]))
18622 {
18623 ++stropt;
18624 block_len = getdigits(&stropt) - 1;
18625 --stropt;
18626 }
18627 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 }
18629 }
18630
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018631 if (argvars[1].v_type == VAR_LIST)
18632 {
18633 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018634 char_u **allocval;
18635 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018636 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018637 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018638 int len = argvars[1].vval.v_list->lv_len;
18639 listitem_T *li;
18640
Bram Moolenaar7d647822014-04-05 21:28:56 +020018641 /* First half: use for pointers to result lines; second half: use for
18642 * pointers to allocated copies. */
18643 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018644 if (lstval == NULL)
18645 return;
18646 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018647 allocval = lstval + len + 2;
18648 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018649
18650 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18651 li = li->li_next)
18652 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018653 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018654 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018655 goto free_lstval;
18656 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018657 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018658 /* Need to make a copy, next get_tv_string_buf_chk() will
18659 * overwrite the string. */
18660 strval = vim_strsave(buf);
18661 if (strval == NULL)
18662 goto free_lstval;
18663 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018664 }
18665 *curval++ = strval;
18666 }
18667 *curval++ = NULL;
18668
18669 write_reg_contents_lst(regname, lstval, -1,
18670 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018671free_lstval:
18672 while (curallocval > allocval)
18673 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018674 vim_free(lstval);
18675 }
18676 else
18677 {
18678 strval = get_tv_string_chk(&argvars[1]);
18679 if (strval == NULL)
18680 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018681 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018683 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018684 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685}
18686
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018687/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018688 * "settabvar()" function
18689 */
18690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018691f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018692{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018693#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018694 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018695 tabpage_T *tp;
18696#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018697 char_u *varname, *tabvarname;
18698 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018699
18700 rettv->vval.v_number = 0;
18701
18702 if (check_restricted() || check_secure())
18703 return;
18704
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018705#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018706 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018707#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018708 varname = get_tv_string_chk(&argvars[1]);
18709 varp = &argvars[2];
18710
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018711 if (varname != NULL && varp != NULL
18712#ifdef FEAT_WINDOWS
18713 && tp != NULL
18714#endif
18715 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018716 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018717#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018718 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018719 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018720#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018721
18722 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18723 if (tabvarname != NULL)
18724 {
18725 STRCPY(tabvarname, "t:");
18726 STRCPY(tabvarname + 2, varname);
18727 set_var(tabvarname, varp, TRUE);
18728 vim_free(tabvarname);
18729 }
18730
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018731#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018732 /* Restore current tabpage */
18733 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018734 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018735#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018736 }
18737}
18738
18739/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018740 * "settabwinvar()" function
18741 */
18742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018743f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018744{
18745 setwinvar(argvars, rettv, 1);
18746}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747
18748/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018749 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018752f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018753{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018754 setwinvar(argvars, rettv, 0);
18755}
18756
18757/*
18758 * "setwinvar()" and "settabwinvar()" functions
18759 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018760
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018762setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018763{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764 win_T *win;
18765#ifdef FEAT_WINDOWS
18766 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018767 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018768 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769#endif
18770 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018773 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774
18775 if (check_restricted() || check_secure())
18776 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018777
18778#ifdef FEAT_WINDOWS
18779 if (off == 1)
18780 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18781 else
18782 tp = curtab;
18783#endif
18784 win = find_win_by_nr(&argvars[off], tp);
18785 varname = get_tv_string_chk(&argvars[off + 1]);
18786 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787
18788 if (win != NULL && varname != NULL && varp != NULL)
18789 {
18790#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018791 need_switch_win = !(tp == curtab && win == curwin);
18792 if (!need_switch_win
18793 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018795 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018796 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018798 long numval;
18799 char_u *strval;
18800 int error = FALSE;
18801
18802 ++varname;
18803 numval = get_tv_number_chk(varp, &error);
18804 strval = get_tv_string_buf_chk(varp, nbuf);
18805 if (!error && strval != NULL)
18806 set_option_value(varname, numval, strval, OPT_LOCAL);
18807 }
18808 else
18809 {
18810 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18811 if (winvarname != NULL)
18812 {
18813 STRCPY(winvarname, "w:");
18814 STRCPY(winvarname + 2, varname);
18815 set_var(winvarname, varp, TRUE);
18816 vim_free(winvarname);
18817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818 }
18819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018821 if (need_switch_win)
18822 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823#endif
18824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825}
18826
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018827#ifdef FEAT_CRYPT
18828/*
18829 * "sha256({string})" function
18830 */
18831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018832f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018833{
18834 char_u *p;
18835
18836 p = get_tv_string(&argvars[0]);
18837 rettv->vval.v_string = vim_strsave(
18838 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18839 rettv->v_type = VAR_STRING;
18840}
18841#endif /* FEAT_CRYPT */
18842
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018844 * "shellescape({string})" function
18845 */
18846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018847f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018848{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018849 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018850 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018851 rettv->v_type = VAR_STRING;
18852}
18853
18854/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018855 * shiftwidth() function
18856 */
18857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018858f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018859{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018860 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018861}
18862
18863/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018864 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018865 */
18866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018867f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018868{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018869 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870
Bram Moolenaar0d660222005-01-07 21:51:51 +000018871 p = get_tv_string(&argvars[0]);
18872 rettv->vval.v_string = vim_strsave(p);
18873 simplify_filename(rettv->vval.v_string); /* simplify in place */
18874 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018875}
18876
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018877#ifdef FEAT_FLOAT
18878/*
18879 * "sin()" function
18880 */
18881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018882f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018883{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018884 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018885
18886 rettv->v_type = VAR_FLOAT;
18887 if (get_float_arg(argvars, &f) == OK)
18888 rettv->vval.v_float = sin(f);
18889 else
18890 rettv->vval.v_float = 0.0;
18891}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018892
18893/*
18894 * "sinh()" function
18895 */
18896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018897f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018898{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018899 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018900
18901 rettv->v_type = VAR_FLOAT;
18902 if (get_float_arg(argvars, &f) == OK)
18903 rettv->vval.v_float = sinh(f);
18904 else
18905 rettv->vval.v_float = 0.0;
18906}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018907#endif
18908
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909static int
18910#ifdef __BORLANDC__
18911 _RTLENTRYF
18912#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018913 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018914static int
18915#ifdef __BORLANDC__
18916 _RTLENTRYF
18917#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018918 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018920/* struct used in the array that's given to qsort() */
18921typedef struct
18922{
18923 listitem_T *item;
18924 int idx;
18925} sortItem_T;
18926
Bram Moolenaar0b962472016-02-22 22:51:33 +010018927/* struct storing information about current sort */
18928typedef struct
18929{
18930 int item_compare_ic;
18931 int item_compare_numeric;
18932 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018933#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018934 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018935#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018936 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018937 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018938 dict_T *item_compare_selfdict;
18939 int item_compare_func_err;
18940 int item_compare_keep_zero;
18941} sortinfo_T;
18942static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018943static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018944#define ITEM_COMPARE_FAIL 999
18945
Bram Moolenaar071d4272004-06-13 20:20:40 +000018946/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018947 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018948 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 static int
18950#ifdef __BORLANDC__
18951_RTLENTRYF
18952#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018953item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018954{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018955 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018956 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018957 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018958 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959 int res;
18960 char_u numbuf1[NUMBUFLEN];
18961 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018962
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018963 si1 = (sortItem_T *)s1;
18964 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018965 tv1 = &si1->item->li_tv;
18966 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018967
Bram Moolenaar0b962472016-02-22 22:51:33 +010018968 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018969 {
18970 long v1 = get_tv_number(tv1);
18971 long v2 = get_tv_number(tv2);
18972
18973 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18974 }
18975
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018976#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018977 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018978 {
18979 float_T v1 = get_tv_float(tv1);
18980 float_T v2 = get_tv_float(tv2);
18981
18982 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18983 }
18984#endif
18985
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018986 /* tv2string() puts quotes around a string and allocates memory. Don't do
18987 * that for string variables. Use a single quote when comparing with a
18988 * non-string to do what the docs promise. */
18989 if (tv1->v_type == VAR_STRING)
18990 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018991 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018992 p1 = (char_u *)"'";
18993 else
18994 p1 = tv1->vval.v_string;
18995 }
18996 else
18997 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18998 if (tv2->v_type == VAR_STRING)
18999 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019000 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019001 p2 = (char_u *)"'";
19002 else
19003 p2 = tv2->vval.v_string;
19004 }
19005 else
19006 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019007 if (p1 == NULL)
19008 p1 = (char_u *)"";
19009 if (p2 == NULL)
19010 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019011 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019012 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019013 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019014 res = STRICMP(p1, p2);
19015 else
19016 res = STRCMP(p1, p2);
19017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019019 {
19020 double n1, n2;
19021 n1 = strtod((char *)p1, (char **)&p1);
19022 n2 = strtod((char *)p2, (char **)&p2);
19023 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19024 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019025
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019026 /* When the result would be zero, compare the item indexes. Makes the
19027 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019028 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019029 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019030
Bram Moolenaar0d660222005-01-07 21:51:51 +000019031 vim_free(tofree1);
19032 vim_free(tofree2);
19033 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034}
19035
19036 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019037#ifdef __BORLANDC__
19038_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019040item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019041{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019042 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019043 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019044 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019045 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019047 char_u *func_name;
19048 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019049
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019050 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019051 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019052 return 0;
19053
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019054 si1 = (sortItem_T *)s1;
19055 si2 = (sortItem_T *)s2;
19056
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019057 if (partial == NULL)
19058 func_name = sortinfo->item_compare_func;
19059 else
19060 func_name = partial->pt_name;
19061
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019062 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019063 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019064 copy_tv(&si1->item->li_tv, &argv[0]);
19065 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066
19067 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019068 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019069 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019070 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071 clear_tv(&argv[0]);
19072 clear_tv(&argv[1]);
19073
19074 if (res == FAIL)
19075 res = ITEM_COMPARE_FAIL;
19076 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019077 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19078 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019079 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019081
19082 /* When the result would be zero, compare the pointers themselves. Makes
19083 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019085 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019086
Bram Moolenaar0d660222005-01-07 21:51:51 +000019087 return res;
19088}
19089
19090/*
19091 * "sort({list})" function
19092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019094do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095{
Bram Moolenaar33570922005-01-25 22:26:29 +000019096 list_T *l;
19097 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019098 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019099 sortinfo_T *old_sortinfo;
19100 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 long len;
19102 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103
Bram Moolenaar0b962472016-02-22 22:51:33 +010019104 /* Pointer to current info struct used in compare function. Save and
19105 * restore the current one for nested calls. */
19106 old_sortinfo = sortinfo;
19107 sortinfo = &info;
19108
Bram Moolenaar0d660222005-01-07 21:51:51 +000019109 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019110 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019111 else
19112 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019114 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019115 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19116 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019117 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019118 rettv->vval.v_list = l;
19119 rettv->v_type = VAR_LIST;
19120 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122 len = list_len(l);
19123 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019125
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 info.item_compare_ic = FALSE;
19127 info.item_compare_numeric = FALSE;
19128 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019129#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019130 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019131#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019133 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019134 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019135 if (argvars[1].v_type != VAR_UNKNOWN)
19136 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019137 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019138 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019139 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019140 else if (argvars[1].v_type == VAR_PARTIAL)
19141 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 else
19143 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019144 int error = FALSE;
19145
19146 i = get_tv_number_chk(&argvars[1], &error);
19147 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019149 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019151 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019153 else if (i != 0)
19154 {
19155 EMSG(_(e_invarg));
19156 goto theend;
19157 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019159 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019160 if (*info.item_compare_func == NUL)
19161 {
19162 /* empty string means default sort */
19163 info.item_compare_func = NULL;
19164 }
19165 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019166 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 info.item_compare_func = NULL;
19168 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019169 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019170 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019171 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 info.item_compare_func = NULL;
19173 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019174 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019175#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019176 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019177 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019178 info.item_compare_func = NULL;
19179 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019180 }
19181#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019182 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019183 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019184 info.item_compare_func = NULL;
19185 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019186 }
19187 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019188 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019189
19190 if (argvars[2].v_type != VAR_UNKNOWN)
19191 {
19192 /* optional third argument: {dict} */
19193 if (argvars[2].v_type != VAR_DICT)
19194 {
19195 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019196 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019197 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019199 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019201
Bram Moolenaar0d660222005-01-07 21:51:51 +000019202 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019203 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019204 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019205 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206
Bram Moolenaar327aa022014-03-25 18:24:23 +010019207 i = 0;
19208 if (sort)
19209 {
19210 /* sort(): ptrs will be the list to sort */
19211 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019212 {
19213 ptrs[i].item = li;
19214 ptrs[i].idx = i;
19215 ++i;
19216 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019217
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 info.item_compare_func_err = FALSE;
19219 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019220 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019221 if ((info.item_compare_func != NULL
19222 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019223 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019224 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019225 EMSG(_("E702: Sort compare function failed"));
19226 else
19227 {
19228 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019229 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019230 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019231 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019232 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019233
Bram Moolenaar0b962472016-02-22 22:51:33 +010019234 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019235 {
19236 /* Clear the List and append the items in sorted order. */
19237 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19238 l->lv_len = 0;
19239 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019240 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019241 }
19242 }
19243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019244 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019245 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019246 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019247
19248 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019249 info.item_compare_func_err = FALSE;
19250 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019251 item_compare_func_ptr = info.item_compare_func != NULL
19252 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019253 ? item_compare2 : item_compare;
19254
19255 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19256 li = li->li_next)
19257 {
19258 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19259 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019260 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019261 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019262 {
19263 EMSG(_("E882: Uniq compare function failed"));
19264 break;
19265 }
19266 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019267
Bram Moolenaar0b962472016-02-22 22:51:33 +010019268 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019269 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019270 while (--i >= 0)
19271 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019272 li = ptrs[i].item->li_next;
19273 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019274 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019275 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019276 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019277 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019278 list_fix_watch(l, li);
19279 listitem_free(li);
19280 l->lv_len--;
19281 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019282 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019283 }
19284
19285 vim_free(ptrs);
19286 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019287theend:
19288 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019289}
19290
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019291/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019292 * "sort({list})" function
19293 */
19294 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019295f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019296{
19297 do_sort_uniq(argvars, rettv, TRUE);
19298}
19299
19300/*
19301 * "uniq({list})" function
19302 */
19303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019304f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019305{
19306 do_sort_uniq(argvars, rettv, FALSE);
19307}
19308
19309/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019310 * "soundfold({word})" function
19311 */
19312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019313f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019314{
19315 char_u *s;
19316
19317 rettv->v_type = VAR_STRING;
19318 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019319#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019320 rettv->vval.v_string = eval_soundfold(s);
19321#else
19322 rettv->vval.v_string = vim_strsave(s);
19323#endif
19324}
19325
19326/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327 * "spellbadword()" function
19328 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019330f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019331{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019332 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019333 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019334 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019336 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019337 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019338
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019339#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019340 if (argvars[0].v_type == VAR_UNKNOWN)
19341 {
19342 /* Find the start and length of the badly spelled word. */
19343 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19344 if (len != 0)
19345 word = ml_get_cursor();
19346 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019347 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019348 {
19349 char_u *str = get_tv_string_chk(&argvars[0]);
19350 int capcol = -1;
19351
19352 if (str != NULL)
19353 {
19354 /* Check the argument for spelling. */
19355 while (*str != NUL)
19356 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019357 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019358 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019359 {
19360 word = str;
19361 break;
19362 }
19363 str += len;
19364 }
19365 }
19366 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019367#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019369 list_append_string(rettv->vval.v_list, word, len);
19370 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019371 attr == HLF_SPB ? "bad" :
19372 attr == HLF_SPR ? "rare" :
19373 attr == HLF_SPL ? "local" :
19374 attr == HLF_SPC ? "caps" :
19375 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019376}
19377
19378/*
19379 * "spellsuggest()" function
19380 */
19381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019382f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019383{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019384#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019385 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019386 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019387 int maxcount;
19388 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019389 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019390 listitem_T *li;
19391 int need_capital = FALSE;
19392#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019393
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019394 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019395 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019396
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019397#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019398 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019399 {
19400 str = get_tv_string(&argvars[0]);
19401 if (argvars[1].v_type != VAR_UNKNOWN)
19402 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019403 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019404 if (maxcount <= 0)
19405 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019406 if (argvars[2].v_type != VAR_UNKNOWN)
19407 {
19408 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19409 if (typeerr)
19410 return;
19411 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019412 }
19413 else
19414 maxcount = 25;
19415
Bram Moolenaar4770d092006-01-12 23:22:24 +000019416 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019417
19418 for (i = 0; i < ga.ga_len; ++i)
19419 {
19420 str = ((char_u **)ga.ga_data)[i];
19421
19422 li = listitem_alloc();
19423 if (li == NULL)
19424 vim_free(str);
19425 else
19426 {
19427 li->li_tv.v_type = VAR_STRING;
19428 li->li_tv.v_lock = 0;
19429 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019430 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019431 }
19432 }
19433 ga_clear(&ga);
19434 }
19435#endif
19436}
19437
Bram Moolenaar0d660222005-01-07 21:51:51 +000019438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019439f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440{
19441 char_u *str;
19442 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019443 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019444 regmatch_T regmatch;
19445 char_u patbuf[NUMBUFLEN];
19446 char_u *save_cpo;
19447 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019448 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019449 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019450 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019451
19452 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19453 save_cpo = p_cpo;
19454 p_cpo = (char_u *)"";
19455
19456 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019457 if (argvars[1].v_type != VAR_UNKNOWN)
19458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019459 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19460 if (pat == NULL)
19461 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019462 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019463 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019464 }
19465 if (pat == NULL || *pat == NUL)
19466 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019467
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019468 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019470 if (typeerr)
19471 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472
Bram Moolenaar0d660222005-01-07 21:51:51 +000019473 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19474 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019476 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019477 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019478 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019479 if (*str == NUL)
19480 match = FALSE; /* empty item at the end */
19481 else
19482 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019483 if (match)
19484 end = regmatch.startp[0];
19485 else
19486 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019487 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19488 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019489 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019490 if (list_append_string(rettv->vval.v_list, str,
19491 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019492 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019493 }
19494 if (!match)
19495 break;
19496 /* Advance to just after the match. */
19497 if (regmatch.endp[0] > str)
19498 col = 0;
19499 else
19500 {
19501 /* Don't get stuck at the same match. */
19502#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019503 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019504#else
19505 col = 1;
19506#endif
19507 }
19508 str = regmatch.endp[0];
19509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510
Bram Moolenaar473de612013-06-08 18:19:48 +020019511 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513
Bram Moolenaar0d660222005-01-07 21:51:51 +000019514 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019515}
19516
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019517#ifdef FEAT_FLOAT
19518/*
19519 * "sqrt()" function
19520 */
19521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019522f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019523{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019524 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019525
19526 rettv->v_type = VAR_FLOAT;
19527 if (get_float_arg(argvars, &f) == OK)
19528 rettv->vval.v_float = sqrt(f);
19529 else
19530 rettv->vval.v_float = 0.0;
19531}
19532
19533/*
19534 * "str2float()" function
19535 */
19536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019537f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019538{
19539 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19540
19541 if (*p == '+')
19542 p = skipwhite(p + 1);
19543 (void)string2float(p, &rettv->vval.v_float);
19544 rettv->v_type = VAR_FLOAT;
19545}
19546#endif
19547
Bram Moolenaar2c932302006-03-18 21:42:09 +000019548/*
19549 * "str2nr()" function
19550 */
19551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019552f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019553{
19554 int base = 10;
19555 char_u *p;
19556 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019557 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019558
19559 if (argvars[1].v_type != VAR_UNKNOWN)
19560 {
19561 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019562 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019563 {
19564 EMSG(_(e_invarg));
19565 return;
19566 }
19567 }
19568
19569 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019570 if (*p == '+')
19571 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019572 switch (base)
19573 {
19574 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19575 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19576 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19577 default: what = 0;
19578 }
19579 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019580 rettv->vval.v_number = n;
19581}
19582
Bram Moolenaar071d4272004-06-13 20:20:40 +000019583#ifdef HAVE_STRFTIME
19584/*
19585 * "strftime({format}[, {time}])" function
19586 */
19587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019588f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589{
19590 char_u result_buf[256];
19591 struct tm *curtime;
19592 time_t seconds;
19593 char_u *p;
19594
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019595 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019597 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019598 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599 seconds = time(NULL);
19600 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019601 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019602 curtime = localtime(&seconds);
19603 /* MSVC returns NULL for an invalid value of seconds. */
19604 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 else
19607 {
19608# ifdef FEAT_MBYTE
19609 vimconv_T conv;
19610 char_u *enc;
19611
19612 conv.vc_type = CONV_NONE;
19613 enc = enc_locale();
19614 convert_setup(&conv, p_enc, enc);
19615 if (conv.vc_type != CONV_NONE)
19616 p = string_convert(&conv, p, NULL);
19617# endif
19618 if (p != NULL)
19619 (void)strftime((char *)result_buf, sizeof(result_buf),
19620 (char *)p, curtime);
19621 else
19622 result_buf[0] = NUL;
19623
19624# ifdef FEAT_MBYTE
19625 if (conv.vc_type != CONV_NONE)
19626 vim_free(p);
19627 convert_setup(&conv, enc, p_enc);
19628 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019629 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019630 else
19631# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633
19634# ifdef FEAT_MBYTE
19635 /* Release conversion descriptors */
19636 convert_setup(&conv, NULL, NULL);
19637 vim_free(enc);
19638# endif
19639 }
19640}
19641#endif
19642
19643/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019644 * "strgetchar()" function
19645 */
19646 static void
19647f_strgetchar(typval_T *argvars, typval_T *rettv)
19648{
19649 char_u *str;
19650 int len;
19651 int error = FALSE;
19652 int charidx;
19653
19654 rettv->vval.v_number = -1;
19655 str = get_tv_string_chk(&argvars[0]);
19656 if (str == NULL)
19657 return;
19658 len = (int)STRLEN(str);
19659 charidx = get_tv_number_chk(&argvars[1], &error);
19660 if (error)
19661 return;
19662#ifdef FEAT_MBYTE
19663 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019664 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019665
19666 while (charidx >= 0 && byteidx < len)
19667 {
19668 if (charidx == 0)
19669 {
19670 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19671 break;
19672 }
19673 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019674 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019675 }
19676 }
19677#else
19678 if (charidx < len)
19679 rettv->vval.v_number = str[charidx];
19680#endif
19681}
19682
19683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 * "stridx()" function
19685 */
19686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019687f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688{
19689 char_u buf[NUMBUFLEN];
19690 char_u *needle;
19691 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019692 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019693 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019694 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019696 needle = get_tv_string_chk(&argvars[1]);
19697 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019698 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019699 if (needle == NULL || haystack == NULL)
19700 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019701
Bram Moolenaar33570922005-01-25 22:26:29 +000019702 if (argvars[2].v_type != VAR_UNKNOWN)
19703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019704 int error = FALSE;
19705
19706 start_idx = get_tv_number_chk(&argvars[2], &error);
19707 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019708 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019709 if (start_idx >= 0)
19710 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 }
19712
19713 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19714 if (pos != NULL)
19715 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716}
19717
19718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019719 * "string()" function
19720 */
19721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019722f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019723{
19724 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019725 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019727 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019728 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19729 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019730 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019731 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733}
19734
19735/*
19736 * "strlen()" function
19737 */
19738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019739f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019741 rettv->vval.v_number = (varnumber_T)(STRLEN(
19742 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019743}
19744
19745/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019746 * "strchars()" function
19747 */
19748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019749f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019750{
19751 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019752 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019753#ifdef FEAT_MBYTE
19754 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019755 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019756#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019757
19758 if (argvars[1].v_type != VAR_UNKNOWN)
19759 skipcc = get_tv_number_chk(&argvars[1], NULL);
19760 if (skipcc < 0 || skipcc > 1)
19761 EMSG(_(e_invarg));
19762 else
19763 {
19764#ifdef FEAT_MBYTE
19765 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19766 while (*s != NUL)
19767 {
19768 func_mb_ptr2char_adv(&s);
19769 ++len;
19770 }
19771 rettv->vval.v_number = len;
19772#else
19773 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19774#endif
19775 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019776}
19777
19778/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019779 * "strdisplaywidth()" function
19780 */
19781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019782f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019783{
19784 char_u *s = get_tv_string(&argvars[0]);
19785 int col = 0;
19786
19787 if (argvars[1].v_type != VAR_UNKNOWN)
19788 col = get_tv_number(&argvars[1]);
19789
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019790 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019791}
19792
19793/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019794 * "strwidth()" function
19795 */
19796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019797f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019798{
19799 char_u *s = get_tv_string(&argvars[0]);
19800
19801 rettv->vval.v_number = (varnumber_T)(
19802#ifdef FEAT_MBYTE
19803 mb_string2cells(s, -1)
19804#else
19805 STRLEN(s)
19806#endif
19807 );
19808}
19809
19810/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019811 * "strcharpart()" function
19812 */
19813 static void
19814f_strcharpart(typval_T *argvars, typval_T *rettv)
19815{
19816#ifdef FEAT_MBYTE
19817 char_u *p;
19818 int nchar;
19819 int nbyte = 0;
19820 int charlen;
19821 int len = 0;
19822 int slen;
19823 int error = FALSE;
19824
19825 p = get_tv_string(&argvars[0]);
19826 slen = (int)STRLEN(p);
19827
19828 nchar = get_tv_number_chk(&argvars[1], &error);
19829 if (!error)
19830 {
19831 if (nchar > 0)
19832 while (nchar > 0 && nbyte < slen)
19833 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019834 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019835 --nchar;
19836 }
19837 else
19838 nbyte = nchar;
19839 if (argvars[2].v_type != VAR_UNKNOWN)
19840 {
19841 charlen = get_tv_number(&argvars[2]);
19842 while (charlen > 0 && nbyte + len < slen)
19843 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019844 int off = nbyte + len;
19845
19846 if (off < 0)
19847 len += 1;
19848 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019849 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019850 --charlen;
19851 }
19852 }
19853 else
19854 len = slen - nbyte; /* default: all bytes that are available. */
19855 }
19856
19857 /*
19858 * Only return the overlap between the specified part and the actual
19859 * string.
19860 */
19861 if (nbyte < 0)
19862 {
19863 len += nbyte;
19864 nbyte = 0;
19865 }
19866 else if (nbyte > slen)
19867 nbyte = slen;
19868 if (len < 0)
19869 len = 0;
19870 else if (nbyte + len > slen)
19871 len = slen - nbyte;
19872
19873 rettv->v_type = VAR_STRING;
19874 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19875#else
19876 f_strpart(argvars, rettv);
19877#endif
19878}
19879
19880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 * "strpart()" function
19882 */
19883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019884f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885{
19886 char_u *p;
19887 int n;
19888 int len;
19889 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019890 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019892 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 slen = (int)STRLEN(p);
19894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019895 n = get_tv_number_chk(&argvars[1], &error);
19896 if (error)
19897 len = 0;
19898 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019900 else
19901 len = slen - n; /* default len: all bytes that are available. */
19902
19903 /*
19904 * Only return the overlap between the specified part and the actual
19905 * string.
19906 */
19907 if (n < 0)
19908 {
19909 len += n;
19910 n = 0;
19911 }
19912 else if (n > slen)
19913 n = slen;
19914 if (len < 0)
19915 len = 0;
19916 else if (n + len > slen)
19917 len = slen - n;
19918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019919 rettv->v_type = VAR_STRING;
19920 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921}
19922
19923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019924 * "strridx()" function
19925 */
19926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019927f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019928{
19929 char_u buf[NUMBUFLEN];
19930 char_u *needle;
19931 char_u *haystack;
19932 char_u *rest;
19933 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019934 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019935
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019936 needle = get_tv_string_chk(&argvars[1]);
19937 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019938
19939 rettv->vval.v_number = -1;
19940 if (needle == NULL || haystack == NULL)
19941 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019942
19943 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019944 if (argvars[2].v_type != VAR_UNKNOWN)
19945 {
19946 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019947 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019948 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019949 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019950 }
19951 else
19952 end_idx = haystack_len;
19953
Bram Moolenaar0d660222005-01-07 21:51:51 +000019954 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019955 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019956 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019957 lastmatch = haystack + end_idx;
19958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019959 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019960 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019961 for (rest = haystack; *rest != '\0'; ++rest)
19962 {
19963 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019964 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019965 break;
19966 lastmatch = rest;
19967 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019968 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019969
19970 if (lastmatch == NULL)
19971 rettv->vval.v_number = -1;
19972 else
19973 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19974}
19975
19976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977 * "strtrans()" function
19978 */
19979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019980f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982 rettv->v_type = VAR_STRING;
19983 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984}
19985
19986/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019987 * "submatch()" function
19988 */
19989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019991{
Bram Moolenaar41571762014-04-02 19:00:58 +020019992 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019993 int no;
19994 int retList = 0;
19995
19996 no = (int)get_tv_number_chk(&argvars[0], &error);
19997 if (error)
19998 return;
19999 error = FALSE;
20000 if (argvars[1].v_type != VAR_UNKNOWN)
20001 retList = get_tv_number_chk(&argvars[1], &error);
20002 if (error)
20003 return;
20004
20005 if (retList == 0)
20006 {
20007 rettv->v_type = VAR_STRING;
20008 rettv->vval.v_string = reg_submatch(no);
20009 }
20010 else
20011 {
20012 rettv->v_type = VAR_LIST;
20013 rettv->vval.v_list = reg_submatch_list(no);
20014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020015}
20016
20017/*
20018 * "substitute()" function
20019 */
20020 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020021f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020022{
20023 char_u patbuf[NUMBUFLEN];
20024 char_u subbuf[NUMBUFLEN];
20025 char_u flagsbuf[NUMBUFLEN];
20026
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020027 char_u *str = get_tv_string_chk(&argvars[0]);
20028 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20029 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20030 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20031
Bram Moolenaar0d660222005-01-07 21:51:51 +000020032 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020033 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20034 rettv->vval.v_string = NULL;
20035 else
20036 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020037}
20038
20039/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020040 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020041 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020043f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044{
20045 int id = 0;
20046#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020047 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 long col;
20049 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020050 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020052 lnum = get_tv_lnum(argvars); /* -1 on type error */
20053 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20054 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020056 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020057 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020058 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059#endif
20060
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062}
20063
20064/*
20065 * "synIDattr(id, what [, mode])" function
20066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020068f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069{
20070 char_u *p = NULL;
20071#ifdef FEAT_SYN_HL
20072 int id;
20073 char_u *what;
20074 char_u *mode;
20075 char_u modebuf[NUMBUFLEN];
20076 int modec;
20077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020078 id = get_tv_number(&argvars[0]);
20079 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020080 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020082 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020083 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020084 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085 modec = 0; /* replace invalid with current */
20086 }
20087 else
20088 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020089#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020090 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 modec = 'g';
20092 else
20093#endif
20094 if (t_colors > 1)
20095 modec = 'c';
20096 else
20097 modec = 't';
20098 }
20099
20100
20101 switch (TOLOWER_ASC(what[0]))
20102 {
20103 case 'b':
20104 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20105 p = highlight_color(id, what, modec);
20106 else /* bold */
20107 p = highlight_has_attr(id, HL_BOLD, modec);
20108 break;
20109
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020110 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020111 p = highlight_color(id, what, modec);
20112 break;
20113
20114 case 'i':
20115 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20116 p = highlight_has_attr(id, HL_INVERSE, modec);
20117 else /* italic */
20118 p = highlight_has_attr(id, HL_ITALIC, modec);
20119 break;
20120
20121 case 'n': /* name */
20122 p = get_highlight_name(NULL, id - 1);
20123 break;
20124
20125 case 'r': /* reverse */
20126 p = highlight_has_attr(id, HL_INVERSE, modec);
20127 break;
20128
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020129 case 's':
20130 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20131 p = highlight_color(id, what, modec);
20132 else /* standout */
20133 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134 break;
20135
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020136 case 'u':
20137 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20138 /* underline */
20139 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20140 else
20141 /* undercurl */
20142 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 break;
20144 }
20145
20146 if (p != NULL)
20147 p = vim_strsave(p);
20148#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020149 rettv->v_type = VAR_STRING;
20150 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020151}
20152
20153/*
20154 * "synIDtrans(id)" function
20155 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020157f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020158{
20159 int id;
20160
20161#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020162 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163
20164 if (id > 0)
20165 id = syn_get_final_id(id);
20166 else
20167#endif
20168 id = 0;
20169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020170 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171}
20172
20173/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020174 * "synconcealed(lnum, col)" function
20175 */
20176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020177f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020178{
20179#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20180 long lnum;
20181 long col;
20182 int syntax_flags = 0;
20183 int cchar;
20184 int matchid = 0;
20185 char_u str[NUMBUFLEN];
20186#endif
20187
20188 rettv->v_type = VAR_LIST;
20189 rettv->vval.v_list = NULL;
20190
20191#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20192 lnum = get_tv_lnum(argvars); /* -1 on type error */
20193 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20194
20195 vim_memset(str, NUL, sizeof(str));
20196
20197 if (rettv_list_alloc(rettv) != FAIL)
20198 {
20199 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20200 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20201 && curwin->w_p_cole > 0)
20202 {
20203 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20204 syntax_flags = get_syntax_info(&matchid);
20205
20206 /* get the conceal character */
20207 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20208 {
20209 cchar = syn_get_sub_char();
20210 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20211 cchar = lcs_conceal;
20212 if (cchar != NUL)
20213 {
20214# ifdef FEAT_MBYTE
20215 if (has_mbyte)
20216 (*mb_char2bytes)(cchar, str);
20217 else
20218# endif
20219 str[0] = cchar;
20220 }
20221 }
20222 }
20223
20224 list_append_number(rettv->vval.v_list,
20225 (syntax_flags & HL_CONCEAL) != 0);
20226 /* -1 to auto-determine strlen */
20227 list_append_string(rettv->vval.v_list, str, -1);
20228 list_append_number(rettv->vval.v_list, matchid);
20229 }
20230#endif
20231}
20232
20233/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020234 * "synstack(lnum, col)" function
20235 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020237f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020238{
20239#ifdef FEAT_SYN_HL
20240 long lnum;
20241 long col;
20242 int i;
20243 int id;
20244#endif
20245
20246 rettv->v_type = VAR_LIST;
20247 rettv->vval.v_list = NULL;
20248
20249#ifdef FEAT_SYN_HL
20250 lnum = get_tv_lnum(argvars); /* -1 on type error */
20251 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20252
20253 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020254 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020255 && rettv_list_alloc(rettv) != FAIL)
20256 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020257 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020258 for (i = 0; ; ++i)
20259 {
20260 id = syn_get_stack_item(i);
20261 if (id < 0)
20262 break;
20263 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20264 break;
20265 }
20266 }
20267#endif
20268}
20269
Bram Moolenaar071d4272004-06-13 20:20:40 +000020270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020271get_cmd_output_as_rettv(
20272 typval_T *argvars,
20273 typval_T *rettv,
20274 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020276 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020278 char_u *infile = NULL;
20279 char_u buf[NUMBUFLEN];
20280 int err = FALSE;
20281 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020282 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020283 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020285 rettv->v_type = VAR_STRING;
20286 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020287 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020288 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020289
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020290 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020291 {
20292 /*
20293 * Write the string to a temp file, to be used for input of the shell
20294 * command.
20295 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020296 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020297 {
20298 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020299 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020300 }
20301
20302 fd = mch_fopen((char *)infile, WRITEBIN);
20303 if (fd == NULL)
20304 {
20305 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020306 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020307 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020308 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020309 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020310 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20311 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020312 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020313 else
20314 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020315 size_t len;
20316
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020317 p = get_tv_string_buf_chk(&argvars[1], buf);
20318 if (p == NULL)
20319 {
20320 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020321 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020322 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020323 len = STRLEN(p);
20324 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020325 err = TRUE;
20326 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020327 if (fclose(fd) != 0)
20328 err = TRUE;
20329 if (err)
20330 {
20331 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020332 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020333 }
20334 }
20335
Bram Moolenaar52a72462014-08-29 15:53:52 +020020336 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20337 * echoes typeahead, that messes up the display. */
20338 if (!msg_silent)
20339 flags += SHELL_COOKED;
20340
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020341 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020343 int len;
20344 listitem_T *li;
20345 char_u *s = NULL;
20346 char_u *start;
20347 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020348 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020349
Bram Moolenaar52a72462014-08-29 15:53:52 +020020350 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020351 if (res == NULL)
20352 goto errret;
20353
20354 list = list_alloc();
20355 if (list == NULL)
20356 goto errret;
20357
20358 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020359 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020360 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020361 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020362 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020363 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020364
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020365 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366 if (s == NULL)
20367 goto errret;
20368
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020369 for (p = s; start < end; ++p, ++start)
20370 *p = *start == NUL ? NL : *start;
20371 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020372
20373 li = listitem_alloc();
20374 if (li == NULL)
20375 {
20376 vim_free(s);
20377 goto errret;
20378 }
20379 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020380 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020381 li->li_tv.vval.v_string = s;
20382 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020384
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020385 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020386 rettv->v_type = VAR_LIST;
20387 rettv->vval.v_list = list;
20388 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020390 else
20391 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020392 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020393#ifdef USE_CR
20394 /* translate <CR> into <NL> */
20395 if (res != NULL)
20396 {
20397 char_u *s;
20398
20399 for (s = res; *s; ++s)
20400 {
20401 if (*s == CAR)
20402 *s = NL;
20403 }
20404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020405#else
20406# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020407 /* translate <CR><NL> into <NL> */
20408 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020409 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020410 char_u *s, *d;
20411
20412 d = res;
20413 for (s = res; *s; ++s)
20414 {
20415 if (s[0] == CAR && s[1] == NL)
20416 ++s;
20417 *d++ = *s;
20418 }
20419 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020421# endif
20422#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020423 rettv->vval.v_string = res;
20424 res = NULL;
20425 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020426
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020427errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020428 if (infile != NULL)
20429 {
20430 mch_remove(infile);
20431 vim_free(infile);
20432 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020433 if (res != NULL)
20434 vim_free(res);
20435 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020436 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020437}
20438
20439/*
20440 * "system()" function
20441 */
20442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020443f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020444{
20445 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20446}
20447
20448/*
20449 * "systemlist()" function
20450 */
20451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020452f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020453{
20454 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455}
20456
20457/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020458 * "tabpagebuflist()" function
20459 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020461f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020462{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020463#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020464 tabpage_T *tp;
20465 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020466
20467 if (argvars[0].v_type == VAR_UNKNOWN)
20468 wp = firstwin;
20469 else
20470 {
20471 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20472 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020473 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020474 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020475 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020476 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020477 for (; wp != NULL; wp = wp->w_next)
20478 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020479 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020480 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020481 }
20482#endif
20483}
20484
20485
20486/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020487 * "tabpagenr()" function
20488 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020490f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020491{
20492 int nr = 1;
20493#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020494 char_u *arg;
20495
20496 if (argvars[0].v_type != VAR_UNKNOWN)
20497 {
20498 arg = get_tv_string_chk(&argvars[0]);
20499 nr = 0;
20500 if (arg != NULL)
20501 {
20502 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020503 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020504 else
20505 EMSG2(_(e_invexpr2), arg);
20506 }
20507 }
20508 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020509 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020510#endif
20511 rettv->vval.v_number = nr;
20512}
20513
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020514
20515#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020516static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020517
20518/*
20519 * Common code for tabpagewinnr() and winnr().
20520 */
20521 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020522get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020523{
20524 win_T *twin;
20525 int nr = 1;
20526 win_T *wp;
20527 char_u *arg;
20528
20529 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20530 if (argvar->v_type != VAR_UNKNOWN)
20531 {
20532 arg = get_tv_string_chk(argvar);
20533 if (arg == NULL)
20534 nr = 0; /* type error; errmsg already given */
20535 else if (STRCMP(arg, "$") == 0)
20536 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20537 else if (STRCMP(arg, "#") == 0)
20538 {
20539 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20540 if (twin == NULL)
20541 nr = 0;
20542 }
20543 else
20544 {
20545 EMSG2(_(e_invexpr2), arg);
20546 nr = 0;
20547 }
20548 }
20549
20550 if (nr > 0)
20551 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20552 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020553 {
20554 if (wp == NULL)
20555 {
20556 /* didn't find it in this tabpage */
20557 nr = 0;
20558 break;
20559 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020560 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020561 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020562 return nr;
20563}
20564#endif
20565
20566/*
20567 * "tabpagewinnr()" function
20568 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020570f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020571{
20572 int nr = 1;
20573#ifdef FEAT_WINDOWS
20574 tabpage_T *tp;
20575
20576 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20577 if (tp == NULL)
20578 nr = 0;
20579 else
20580 nr = get_winnr(tp, &argvars[1]);
20581#endif
20582 rettv->vval.v_number = nr;
20583}
20584
20585
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020586/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020587 * "tagfiles()" function
20588 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020590f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020591{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020592 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020593 tagname_T tn;
20594 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020595
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020596 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020597 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020598 fname = alloc(MAXPATHL);
20599 if (fname == NULL)
20600 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020601
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020602 for (first = TRUE; ; first = FALSE)
20603 if (get_tagfname(&tn, first, fname) == FAIL
20604 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020605 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020606 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020607 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020608}
20609
20610/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020611 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020612 */
20613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020615{
20616 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020617
20618 tag_pattern = get_tv_string(&argvars[0]);
20619
20620 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020621 if (*tag_pattern == NUL)
20622 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020623
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020624 if (rettv_list_alloc(rettv) == OK)
20625 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020626}
20627
20628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 * "tempname()" function
20630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633{
20634 static int x = 'A';
20635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020637 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638
20639 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20640 * names. Skip 'I' and 'O', they are used for shell redirection. */
20641 do
20642 {
20643 if (x == 'Z')
20644 x = '0';
20645 else if (x == '9')
20646 x = 'A';
20647 else
20648 {
20649#ifdef EBCDIC
20650 if (x == 'I')
20651 x = 'J';
20652 else if (x == 'R')
20653 x = 'S';
20654 else
20655#endif
20656 ++x;
20657 }
20658 } while (x == 'I' || x == 'O');
20659}
20660
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020661#ifdef FEAT_FLOAT
20662/*
20663 * "tan()" function
20664 */
20665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020666f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020667{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020668 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020669
20670 rettv->v_type = VAR_FLOAT;
20671 if (get_float_arg(argvars, &f) == OK)
20672 rettv->vval.v_float = tan(f);
20673 else
20674 rettv->vval.v_float = 0.0;
20675}
20676
20677/*
20678 * "tanh()" function
20679 */
20680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020681f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020682{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020683 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020684
20685 rettv->v_type = VAR_FLOAT;
20686 if (get_float_arg(argvars, &f) == OK)
20687 rettv->vval.v_float = tanh(f);
20688 else
20689 rettv->vval.v_float = 0.0;
20690}
20691#endif
20692
Bram Moolenaar574860b2016-05-24 17:33:34 +020020693/*
20694 * "test_garbagecollect_now()" function
20695 */
20696 static void
20697f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20698{
20699 /* This is dangerous, any Lists and Dicts used internally may be freed
20700 * while still in use. */
20701 garbage_collect(TRUE);
20702}
20703
20704#ifdef FEAT_JOB_CHANNEL
20705 static void
20706f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20707{
20708 rettv->v_type = VAR_CHANNEL;
20709 rettv->vval.v_channel = NULL;
20710}
20711#endif
20712
20713 static void
20714f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20715{
20716 rettv->v_type = VAR_DICT;
20717 rettv->vval.v_dict = NULL;
20718}
20719
20720#ifdef FEAT_JOB_CHANNEL
20721 static void
20722f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20723{
20724 rettv->v_type = VAR_JOB;
20725 rettv->vval.v_job = NULL;
20726}
20727#endif
20728
20729 static void
20730f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20731{
20732 rettv->v_type = VAR_LIST;
20733 rettv->vval.v_list = NULL;
20734}
20735
20736 static void
20737f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20738{
20739 rettv->v_type = VAR_PARTIAL;
20740 rettv->vval.v_partial = NULL;
20741}
20742
20743 static void
20744f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20745{
20746 rettv->v_type = VAR_STRING;
20747 rettv->vval.v_string = NULL;
20748}
20749
Bram Moolenaar975b5272016-03-15 23:10:59 +010020750#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20751/*
20752 * Get a callback from "arg". It can be a Funcref or a function name.
20753 * When "arg" is zero return an empty string.
20754 * Return NULL for an invalid argument.
20755 */
20756 char_u *
20757get_callback(typval_T *arg, partial_T **pp)
20758{
20759 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20760 {
20761 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020762 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020763 return (*pp)->pt_name;
20764 }
20765 *pp = NULL;
20766 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20767 return arg->vval.v_string;
20768 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20769 return (char_u *)"";
20770 EMSG(_("E921: Invalid callback argument"));
20771 return NULL;
20772}
20773#endif
20774
20775#ifdef FEAT_TIMERS
20776/*
20777 * "timer_start(time, callback [, options])" function
20778 */
20779 static void
20780f_timer_start(typval_T *argvars, typval_T *rettv)
20781{
20782 long msec = get_tv_number(&argvars[0]);
20783 timer_T *timer;
20784 int repeat = 0;
20785 char_u *callback;
20786 dict_T *dict;
20787
Bram Moolenaar38499922016-04-22 20:46:52 +020020788 if (check_secure())
20789 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020790 if (argvars[2].v_type != VAR_UNKNOWN)
20791 {
20792 if (argvars[2].v_type != VAR_DICT
20793 || (dict = argvars[2].vval.v_dict) == NULL)
20794 {
20795 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20796 return;
20797 }
20798 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20799 repeat = get_dict_number(dict, (char_u *)"repeat");
20800 }
20801
20802 timer = create_timer(msec, repeat);
20803 callback = get_callback(&argvars[1], &timer->tr_partial);
20804 if (callback == NULL)
20805 {
20806 stop_timer(timer);
20807 rettv->vval.v_number = -1;
20808 }
20809 else
20810 {
20811 timer->tr_callback = vim_strsave(callback);
20812 rettv->vval.v_number = timer->tr_id;
20813 }
20814}
20815
20816/*
20817 * "timer_stop(timer)" function
20818 */
20819 static void
20820f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20821{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020822 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020823
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020824 if (argvars[0].v_type != VAR_NUMBER)
20825 {
20826 EMSG(_(e_number_exp));
20827 return;
20828 }
20829 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020830 if (timer != NULL)
20831 stop_timer(timer);
20832}
20833#endif
20834
Bram Moolenaard52d9742005-08-21 22:20:28 +000020835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836 * "tolower(string)" function
20837 */
20838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020839f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020840{
20841 char_u *p;
20842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 p = vim_strsave(get_tv_string(&argvars[0]));
20844 rettv->v_type = VAR_STRING;
20845 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846
20847 if (p != NULL)
20848 while (*p != NUL)
20849 {
20850#ifdef FEAT_MBYTE
20851 int l;
20852
20853 if (enc_utf8)
20854 {
20855 int c, lc;
20856
20857 c = utf_ptr2char(p);
20858 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020859 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020860 /* TODO: reallocate string when byte count changes. */
20861 if (utf_char2len(lc) == l)
20862 utf_char2bytes(lc, p);
20863 p += l;
20864 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020865 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 p += l; /* skip multi-byte character */
20867 else
20868#endif
20869 {
20870 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20871 ++p;
20872 }
20873 }
20874}
20875
20876/*
20877 * "toupper(string)" function
20878 */
20879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020880f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020881{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020882 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020883 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884}
20885
20886/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020887 * "tr(string, fromstr, tostr)" function
20888 */
20889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020890f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020891{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020892 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020893 char_u *fromstr;
20894 char_u *tostr;
20895 char_u *p;
20896#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020897 int inlen;
20898 int fromlen;
20899 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020900 int idx;
20901 char_u *cpstr;
20902 int cplen;
20903 int first = TRUE;
20904#endif
20905 char_u buf[NUMBUFLEN];
20906 char_u buf2[NUMBUFLEN];
20907 garray_T ga;
20908
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020909 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020910 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20911 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020912
20913 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020914 rettv->v_type = VAR_STRING;
20915 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020916 if (fromstr == NULL || tostr == NULL)
20917 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020918 ga_init2(&ga, (int)sizeof(char), 80);
20919
20920#ifdef FEAT_MBYTE
20921 if (!has_mbyte)
20922#endif
20923 /* not multi-byte: fromstr and tostr must be the same length */
20924 if (STRLEN(fromstr) != STRLEN(tostr))
20925 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020926#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020927error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020928#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020929 EMSG2(_(e_invarg2), fromstr);
20930 ga_clear(&ga);
20931 return;
20932 }
20933
20934 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020935 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020936 {
20937#ifdef FEAT_MBYTE
20938 if (has_mbyte)
20939 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020940 inlen = (*mb_ptr2len)(in_str);
20941 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020942 cplen = inlen;
20943 idx = 0;
20944 for (p = fromstr; *p != NUL; p += fromlen)
20945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020946 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020947 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020948 {
20949 for (p = tostr; *p != NUL; p += tolen)
20950 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020951 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020952 if (idx-- == 0)
20953 {
20954 cplen = tolen;
20955 cpstr = p;
20956 break;
20957 }
20958 }
20959 if (*p == NUL) /* tostr is shorter than fromstr */
20960 goto error;
20961 break;
20962 }
20963 ++idx;
20964 }
20965
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020966 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020967 {
20968 /* Check that fromstr and tostr have the same number of
20969 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020970 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020971 first = FALSE;
20972 for (p = tostr; *p != NUL; p += tolen)
20973 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020974 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020975 --idx;
20976 }
20977 if (idx != 0)
20978 goto error;
20979 }
20980
Bram Moolenaarcde88542015-08-11 19:14:00 +020020981 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020982 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020983 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020984
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020985 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020986 }
20987 else
20988#endif
20989 {
20990 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020991 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020992 if (p != NULL)
20993 ga_append(&ga, tostr[p - fromstr]);
20994 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020995 ga_append(&ga, *in_str);
20996 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020997 }
20998 }
20999
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021000 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021001 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021002 ga_append(&ga, NUL);
21003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021004 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021005}
21006
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021007#ifdef FEAT_FLOAT
21008/*
21009 * "trunc({float})" function
21010 */
21011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021012f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021013{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021014 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021015
21016 rettv->v_type = VAR_FLOAT;
21017 if (get_float_arg(argvars, &f) == OK)
21018 /* trunc() is not in C90, use floor() or ceil() instead. */
21019 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21020 else
21021 rettv->vval.v_float = 0.0;
21022}
21023#endif
21024
Bram Moolenaar8299df92004-07-10 09:47:34 +000021025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 * "type(expr)" function
21027 */
21028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021031 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021032
21033 switch (argvars[0].v_type)
21034 {
21035 case VAR_NUMBER: n = 0; break;
21036 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021037 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021038 case VAR_FUNC: n = 2; break;
21039 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021040 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021041 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021042 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021043 if (argvars[0].vval.v_number == VVAL_FALSE
21044 || argvars[0].vval.v_number == VVAL_TRUE)
21045 n = 6;
21046 else
21047 n = 7;
21048 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021049 case VAR_JOB: n = 8; break;
21050 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021051 case VAR_UNKNOWN:
21052 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21053 n = -1;
21054 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021055 }
21056 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057}
21058
21059/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021060 * "undofile(name)" function
21061 */
21062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021063f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021064{
21065 rettv->v_type = VAR_STRING;
21066#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021067 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021068 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021069
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021070 if (*fname == NUL)
21071 {
21072 /* If there is no file name there will be no undo file. */
21073 rettv->vval.v_string = NULL;
21074 }
21075 else
21076 {
21077 char_u *ffname = FullName_save(fname, FALSE);
21078
21079 if (ffname != NULL)
21080 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21081 vim_free(ffname);
21082 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021083 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021084#else
21085 rettv->vval.v_string = NULL;
21086#endif
21087}
21088
21089/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021090 * "undotree()" function
21091 */
21092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021093f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021094{
21095 if (rettv_dict_alloc(rettv) == OK)
21096 {
21097 dict_T *dict = rettv->vval.v_dict;
21098 list_T *list;
21099
Bram Moolenaar730cde92010-06-27 05:18:54 +020021100 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021101 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021102 dict_add_nr_str(dict, "save_last",
21103 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021104 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21105 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021106 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021107
21108 list = list_alloc();
21109 if (list != NULL)
21110 {
21111 u_eval_tree(curbuf->b_u_oldhead, list);
21112 dict_add_list(dict, "entries", list);
21113 }
21114 }
21115}
21116
21117/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021118 * "values(dict)" function
21119 */
21120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021121f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021122{
21123 dict_list(argvars, rettv, 1);
21124}
21125
21126/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 * "virtcol(string)" function
21128 */
21129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021130f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131{
21132 colnr_T vcol = 0;
21133 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021134 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021135
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021136 fp = var2fpos(&argvars[0], FALSE, &fnum);
21137 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21138 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021139 {
21140 getvvcol(curwin, fp, NULL, NULL, &vcol);
21141 ++vcol;
21142 }
21143
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021144 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145}
21146
21147/*
21148 * "visualmode()" function
21149 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021151f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153 char_u str[2];
21154
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021155 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021156 str[0] = curbuf->b_visual_mode_eval;
21157 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159
21160 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021161 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021162 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163}
21164
21165/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021166 * "wildmenumode()" function
21167 */
21168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021169f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021170{
21171#ifdef FEAT_WILDMENU
21172 if (wild_menu_showing)
21173 rettv->vval.v_number = 1;
21174#endif
21175}
21176
21177/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 * "winbufnr(nr)" function
21179 */
21180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021181f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182{
21183 win_T *wp;
21184
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021185 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021188 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021189 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021190}
21191
21192/*
21193 * "wincol()" function
21194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021196f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197{
21198 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021199 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200}
21201
21202/*
21203 * "winheight(nr)" function
21204 */
21205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021206f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207{
21208 win_T *wp;
21209
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021210 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021214 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021215}
21216
21217/*
21218 * "winline()" function
21219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222{
21223 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021224 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021225}
21226
21227/*
21228 * "winnr()" function
21229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021231f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232{
21233 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021234
Bram Moolenaar071d4272004-06-13 20:20:40 +000021235#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021236 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021238 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239}
21240
21241/*
21242 * "winrestcmd()" function
21243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021245f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021246{
21247#ifdef FEAT_WINDOWS
21248 win_T *wp;
21249 int winnr = 1;
21250 garray_T ga;
21251 char_u buf[50];
21252
21253 ga_init2(&ga, (int)sizeof(char), 70);
21254 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21255 {
21256 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21257 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21259 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 ++winnr;
21261 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021262 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021264 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269}
21270
21271/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021272 * "winrestview()" function
21273 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021275f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021276{
21277 dict_T *dict;
21278
21279 if (argvars[0].v_type != VAR_DICT
21280 || (dict = argvars[0].vval.v_dict) == NULL)
21281 EMSG(_(e_invarg));
21282 else
21283 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021284 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21285 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21286 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21287 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021288#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021289 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21290 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021291#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021292 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21293 {
21294 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21295 curwin->w_set_curswant = FALSE;
21296 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021297
Bram Moolenaar82c25852014-05-28 16:47:16 +020021298 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21299 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021300#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021301 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21302 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021303#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021304 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21305 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21306 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21307 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021308
21309 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021310 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021311# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021312 win_new_width(curwin, W_WIDTH(curwin));
21313# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021314 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021315
Bram Moolenaarb851a962014-10-31 15:45:52 +010021316 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021317 curwin->w_topline = 1;
21318 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21319 curwin->w_topline = curbuf->b_ml.ml_line_count;
21320#ifdef FEAT_DIFF
21321 check_topfill(curwin, TRUE);
21322#endif
21323 }
21324}
21325
21326/*
21327 * "winsaveview()" function
21328 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021330f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021331{
21332 dict_T *dict;
21333
Bram Moolenaara800b422010-06-27 01:15:55 +020021334 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021335 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021336 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021337
21338 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21339 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21340#ifdef FEAT_VIRTUALEDIT
21341 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21342#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021343 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021344 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21345
21346 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21347#ifdef FEAT_DIFF
21348 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21349#endif
21350 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21351 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21352}
21353
21354/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021355 * "winwidth(nr)" function
21356 */
21357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021358f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021359{
21360 win_T *wp;
21361
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021362 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021366#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021367 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021369 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021370#endif
21371}
21372
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021374 * "wordcount()" function
21375 */
21376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021377f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021378{
21379 if (rettv_dict_alloc(rettv) == FAIL)
21380 return;
21381 cursor_pos_info(rettv->vval.v_dict);
21382}
21383
21384/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021385 * Write list of strings to file
21386 */
21387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021388write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021389{
21390 listitem_T *li;
21391 int c;
21392 int ret = OK;
21393 char_u *s;
21394
21395 for (li = list->lv_first; li != NULL; li = li->li_next)
21396 {
21397 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21398 {
21399 if (*s == '\n')
21400 c = putc(NUL, fd);
21401 else
21402 c = putc(*s, fd);
21403 if (c == EOF)
21404 {
21405 ret = FAIL;
21406 break;
21407 }
21408 }
21409 if (!binary || li->li_next != NULL)
21410 if (putc('\n', fd) == EOF)
21411 {
21412 ret = FAIL;
21413 break;
21414 }
21415 if (ret == FAIL)
21416 {
21417 EMSG(_(e_write));
21418 break;
21419 }
21420 }
21421 return ret;
21422}
21423
21424/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021425 * "writefile()" function
21426 */
21427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021428f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021429{
21430 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021431 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021432 char_u *fname;
21433 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021434 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021435
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021436 if (check_restricted() || check_secure())
21437 return;
21438
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021439 if (argvars[0].v_type != VAR_LIST)
21440 {
21441 EMSG2(_(e_listarg), "writefile()");
21442 return;
21443 }
21444 if (argvars[0].vval.v_list == NULL)
21445 return;
21446
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021447 if (argvars[2].v_type != VAR_UNKNOWN)
21448 {
21449 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21450 binary = TRUE;
21451 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21452 append = TRUE;
21453 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021454
21455 /* Always open the file in binary mode, library functions have a mind of
21456 * their own about CR-LF conversion. */
21457 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021458 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21459 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021460 {
21461 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21462 ret = -1;
21463 }
21464 else
21465 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021466 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21467 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021468 fclose(fd);
21469 }
21470
21471 rettv->vval.v_number = ret;
21472}
21473
21474/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021475 * "xor(expr, expr)" function
21476 */
21477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021478f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021479{
21480 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21481 ^ get_tv_number_chk(&argvars[1], NULL);
21482}
21483
21484
21485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021486 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021487 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021488 */
21489 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490var2fpos(
21491 typval_T *varp,
21492 int dollar_lnum, /* TRUE when $ is last line */
21493 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021494{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021495 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021497 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498
Bram Moolenaara5525202006-03-02 22:52:09 +000021499 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021500 if (varp->v_type == VAR_LIST)
21501 {
21502 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021503 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021504 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021505 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021506
21507 l = varp->vval.v_list;
21508 if (l == NULL)
21509 return NULL;
21510
21511 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021512 pos.lnum = list_find_nr(l, 0L, &error);
21513 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021514 return NULL; /* invalid line number */
21515
21516 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021517 pos.col = list_find_nr(l, 1L, &error);
21518 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021519 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021520 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021521
21522 /* We accept "$" for the column number: last column. */
21523 li = list_find(l, 1L);
21524 if (li != NULL && li->li_tv.v_type == VAR_STRING
21525 && li->li_tv.vval.v_string != NULL
21526 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21527 pos.col = len + 1;
21528
Bram Moolenaara5525202006-03-02 22:52:09 +000021529 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021530 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021531 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021532 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021533
Bram Moolenaara5525202006-03-02 22:52:09 +000021534#ifdef FEAT_VIRTUALEDIT
21535 /* Get the virtual offset. Defaults to zero. */
21536 pos.coladd = list_find_nr(l, 2L, &error);
21537 if (error)
21538 pos.coladd = 0;
21539#endif
21540
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021541 return &pos;
21542 }
21543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021544 name = get_tv_string_chk(varp);
21545 if (name == NULL)
21546 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021547 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021549 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21550 {
21551 if (VIsual_active)
21552 return &VIsual;
21553 return &curwin->w_cursor;
21554 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021555 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021557 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21559 return NULL;
21560 return pp;
21561 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021562
21563#ifdef FEAT_VIRTUALEDIT
21564 pos.coladd = 0;
21565#endif
21566
Bram Moolenaar477933c2007-07-17 14:32:23 +000021567 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021568 {
21569 pos.col = 0;
21570 if (name[1] == '0') /* "w0": first visible line */
21571 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021572 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021573 pos.lnum = curwin->w_topline;
21574 return &pos;
21575 }
21576 else if (name[1] == '$') /* "w$": last visible line */
21577 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021578 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021579 pos.lnum = curwin->w_botline - 1;
21580 return &pos;
21581 }
21582 }
21583 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021584 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021585 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 {
21587 pos.lnum = curbuf->b_ml.ml_line_count;
21588 pos.col = 0;
21589 }
21590 else
21591 {
21592 pos.lnum = curwin->w_cursor.lnum;
21593 pos.col = (colnr_T)STRLEN(ml_get_curline());
21594 }
21595 return &pos;
21596 }
21597 return NULL;
21598}
21599
21600/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021601 * Convert list in "arg" into a position and optional file number.
21602 * When "fnump" is NULL there is no file number, only 3 items.
21603 * Note that the column is passed on as-is, the caller may want to decrement
21604 * it to use 1 for the first column.
21605 * Return FAIL when conversion is not possible, doesn't check the position for
21606 * validity.
21607 */
21608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021609list2fpos(
21610 typval_T *arg,
21611 pos_T *posp,
21612 int *fnump,
21613 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021614{
21615 list_T *l = arg->vval.v_list;
21616 long i = 0;
21617 long n;
21618
Bram Moolenaar493c1782014-05-28 14:34:46 +020021619 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21620 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021621 if (arg->v_type != VAR_LIST
21622 || l == NULL
21623 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021624 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021625 return FAIL;
21626
21627 if (fnump != NULL)
21628 {
21629 n = list_find_nr(l, i++, NULL); /* fnum */
21630 if (n < 0)
21631 return FAIL;
21632 if (n == 0)
21633 n = curbuf->b_fnum; /* current buffer */
21634 *fnump = n;
21635 }
21636
21637 n = list_find_nr(l, i++, NULL); /* lnum */
21638 if (n < 0)
21639 return FAIL;
21640 posp->lnum = n;
21641
21642 n = list_find_nr(l, i++, NULL); /* col */
21643 if (n < 0)
21644 return FAIL;
21645 posp->col = n;
21646
21647#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021648 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021649 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021650 posp->coladd = 0;
21651 else
21652 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021653#endif
21654
Bram Moolenaar493c1782014-05-28 14:34:46 +020021655 if (curswantp != NULL)
21656 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21657
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021658 return OK;
21659}
21660
21661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 * Get the length of an environment variable name.
21663 * Advance "arg" to the first character after the name.
21664 * Return 0 for error.
21665 */
21666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021667get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021668{
21669 char_u *p;
21670 int len;
21671
21672 for (p = *arg; vim_isIDc(*p); ++p)
21673 ;
21674 if (p == *arg) /* no name found */
21675 return 0;
21676
21677 len = (int)(p - *arg);
21678 *arg = p;
21679 return len;
21680}
21681
21682/*
21683 * Get the length of the name of a function or internal variable.
21684 * "arg" is advanced to the first non-white character after the name.
21685 * Return 0 if something is wrong.
21686 */
21687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021688get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021689{
21690 char_u *p;
21691 int len;
21692
21693 /* Find the end of the name. */
21694 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021695 {
21696 if (*p == ':')
21697 {
21698 /* "s:" is start of "s:var", but "n:" is not and can be used in
21699 * slice "[n:]". Also "xx:" is not a namespace. */
21700 len = (int)(p - *arg);
21701 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21702 || len > 1)
21703 break;
21704 }
21705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706 if (p == *arg) /* no name found */
21707 return 0;
21708
21709 len = (int)(p - *arg);
21710 *arg = skipwhite(p);
21711
21712 return len;
21713}
21714
21715/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021716 * Get the length of the name of a variable or function.
21717 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021718 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021719 * Return -1 if curly braces expansion failed.
21720 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021721 * If the name contains 'magic' {}'s, expand them and return the
21722 * expanded name in an allocated string via 'alias' - caller must free.
21723 */
21724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021725get_name_len(
21726 char_u **arg,
21727 char_u **alias,
21728 int evaluate,
21729 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021730{
21731 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732 char_u *p;
21733 char_u *expr_start;
21734 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735
21736 *alias = NULL; /* default to no alias */
21737
21738 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21739 && (*arg)[2] == (int)KE_SNR)
21740 {
21741 /* hard coded <SNR>, already translated */
21742 *arg += 3;
21743 return get_id_len(arg) + 3;
21744 }
21745 len = eval_fname_script(*arg);
21746 if (len > 0)
21747 {
21748 /* literal "<SID>", "s:" or "<SNR>" */
21749 *arg += len;
21750 }
21751
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021753 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021755 p = find_name_end(*arg, &expr_start, &expr_end,
21756 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 if (expr_start != NULL)
21758 {
21759 char_u *temp_string;
21760
21761 if (!evaluate)
21762 {
21763 len += (int)(p - *arg);
21764 *arg = skipwhite(p);
21765 return len;
21766 }
21767
21768 /*
21769 * Include any <SID> etc in the expanded string:
21770 * Thus the -len here.
21771 */
21772 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21773 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021774 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775 *alias = temp_string;
21776 *arg = skipwhite(p);
21777 return (int)STRLEN(temp_string);
21778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779
21780 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021781 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 EMSG2(_(e_invexpr2), *arg);
21783
21784 return len;
21785}
21786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021787/*
21788 * Find the end of a variable or function name, taking care of magic braces.
21789 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21790 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021791 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021792 * Return a pointer to just after the name. Equal to "arg" if there is no
21793 * valid name.
21794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021796find_name_end(
21797 char_u *arg,
21798 char_u **expr_start,
21799 char_u **expr_end,
21800 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021802 int mb_nest = 0;
21803 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021805 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021807 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021809 *expr_start = NULL;
21810 *expr_end = NULL;
21811 }
21812
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021813 /* Quick check for valid starting character. */
21814 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21815 return arg;
21816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021817 for (p = arg; *p != NUL
21818 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021819 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021820 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021821 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021822 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021823 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021824 if (*p == '\'')
21825 {
21826 /* skip over 'string' to avoid counting [ and ] inside it. */
21827 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21828 ;
21829 if (*p == NUL)
21830 break;
21831 }
21832 else if (*p == '"')
21833 {
21834 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21835 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21836 if (*p == '\\' && p[1] != NUL)
21837 ++p;
21838 if (*p == NUL)
21839 break;
21840 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021841 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21842 {
21843 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021844 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021845 len = (int)(p - arg);
21846 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021847 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021848 break;
21849 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021850
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021851 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021853 if (*p == '[')
21854 ++br_nest;
21855 else if (*p == ']')
21856 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021857 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021859 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021861 if (*p == '{')
21862 {
21863 mb_nest++;
21864 if (expr_start != NULL && *expr_start == NULL)
21865 *expr_start = p;
21866 }
21867 else if (*p == '}')
21868 {
21869 mb_nest--;
21870 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21871 *expr_end = p;
21872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 }
21875
21876 return p;
21877}
21878
21879/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021880 * Expands out the 'magic' {}'s in a variable/function name.
21881 * Note that this can call itself recursively, to deal with
21882 * constructs like foo{bar}{baz}{bam}
21883 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21884 * "in_start" ^
21885 * "expr_start" ^
21886 * "expr_end" ^
21887 * "in_end" ^
21888 *
21889 * Returns a new allocated string, which the caller must free.
21890 * Returns NULL for failure.
21891 */
21892 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021893make_expanded_name(
21894 char_u *in_start,
21895 char_u *expr_start,
21896 char_u *expr_end,
21897 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021898{
21899 char_u c1;
21900 char_u *retval = NULL;
21901 char_u *temp_result;
21902 char_u *nextcmd = NULL;
21903
21904 if (expr_end == NULL || in_end == NULL)
21905 return NULL;
21906 *expr_start = NUL;
21907 *expr_end = NUL;
21908 c1 = *in_end;
21909 *in_end = NUL;
21910
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021911 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021912 if (temp_result != NULL && nextcmd == NULL)
21913 {
21914 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21915 + (in_end - expr_end) + 1));
21916 if (retval != NULL)
21917 {
21918 STRCPY(retval, in_start);
21919 STRCAT(retval, temp_result);
21920 STRCAT(retval, expr_end + 1);
21921 }
21922 }
21923 vim_free(temp_result);
21924
21925 *in_end = c1; /* put char back for error messages */
21926 *expr_start = '{';
21927 *expr_end = '}';
21928
21929 if (retval != NULL)
21930 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021931 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021932 if (expr_start != NULL)
21933 {
21934 /* Further expansion! */
21935 temp_result = make_expanded_name(retval, expr_start,
21936 expr_end, temp_result);
21937 vim_free(retval);
21938 retval = temp_result;
21939 }
21940 }
21941
21942 return retval;
21943}
21944
21945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021947 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 */
21949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021950eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021951{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021952 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21953}
21954
21955/*
21956 * Return TRUE if character "c" can be used as the first character in a
21957 * variable or function name (excluding '{' and '}').
21958 */
21959 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021960eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021961{
21962 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963}
21964
21965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 * Set number v: variable to "val".
21967 */
21968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021969set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021970{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021972}
21973
21974/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021975 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 */
21977 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021978get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021980 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981}
21982
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021983/*
21984 * Get string v: variable value. Uses a static buffer, can only be used once.
21985 */
21986 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021987get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021988{
21989 return get_tv_string(&vimvars[idx].vv_tv);
21990}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021991
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021993 * Get List v: variable value. Caller must take care of reference count when
21994 * needed.
21995 */
21996 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021997get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021998{
21999 return vimvars[idx].vv_list;
22000}
22001
22002/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022003 * Set v:char to character "c".
22004 */
22005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022006set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022007{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022008 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022009
22010#ifdef FEAT_MBYTE
22011 if (has_mbyte)
22012 buf[(*mb_char2bytes)(c, buf)] = NUL;
22013 else
22014#endif
22015 {
22016 buf[0] = c;
22017 buf[1] = NUL;
22018 }
22019 set_vim_var_string(VV_CHAR, buf, -1);
22020}
22021
22022/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022023 * Set v:count to "count" and v:count1 to "count1".
22024 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022025 */
22026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022027set_vcount(
22028 long count,
22029 long count1,
22030 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022032 if (set_prevcount)
22033 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022034 vimvars[VV_COUNT].vv_nr = count;
22035 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036}
22037
22038/*
22039 * Set string v: variable to a copy of "val".
22040 */
22041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022042set_vim_var_string(
22043 int idx,
22044 char_u *val,
22045 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046{
Bram Moolenaara542c682016-01-31 16:28:04 +010022047 clear_tv(&vimvars[idx].vv_di.di_tv);
22048 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022050 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022051 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022052 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022054 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055}
22056
22057/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022058 * Set List v: variable to "val".
22059 */
22060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022061set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022062{
Bram Moolenaara542c682016-01-31 16:28:04 +010022063 clear_tv(&vimvars[idx].vv_di.di_tv);
22064 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022065 vimvars[idx].vv_list = val;
22066 if (val != NULL)
22067 ++val->lv_refcount;
22068}
22069
22070/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022071 * Set Dictionary v: variable to "val".
22072 */
22073 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022074set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022075{
22076 int todo;
22077 hashitem_T *hi;
22078
Bram Moolenaara542c682016-01-31 16:28:04 +010022079 clear_tv(&vimvars[idx].vv_di.di_tv);
22080 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022081 vimvars[idx].vv_dict = val;
22082 if (val != NULL)
22083 {
22084 ++val->dv_refcount;
22085
22086 /* Set readonly */
22087 todo = (int)val->dv_hashtab.ht_used;
22088 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22089 {
22090 if (HASHITEM_EMPTY(hi))
22091 continue;
22092 --todo;
22093 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22094 }
22095 }
22096}
22097
22098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * Set v:register if needed.
22100 */
22101 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022102set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022103{
22104 char_u regname;
22105
22106 if (c == 0 || c == ' ')
22107 regname = '"';
22108 else
22109 regname = c;
22110 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022111 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 set_vim_var_string(VV_REG, &regname, 1);
22113}
22114
22115/*
22116 * Get or set v:exception. If "oldval" == NULL, return the current value.
22117 * Otherwise, restore the value to "oldval" and return NULL.
22118 * Must always be called in pairs to save and restore v:exception! Does not
22119 * take care of memory allocations.
22120 */
22121 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022122v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123{
22124 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022125 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126
Bram Moolenaare9a41262005-01-15 22:18:47 +000022127 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 return NULL;
22129}
22130
22131/*
22132 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22133 * Otherwise, restore the value to "oldval" and return NULL.
22134 * Must always be called in pairs to save and restore v:throwpoint! Does not
22135 * take care of memory allocations.
22136 */
22137 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022138v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022139{
22140 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022141 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142
Bram Moolenaare9a41262005-01-15 22:18:47 +000022143 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022144 return NULL;
22145}
22146
22147#if defined(FEAT_AUTOCMD) || defined(PROTO)
22148/*
22149 * Set v:cmdarg.
22150 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22151 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22152 * Must always be called in pairs!
22153 */
22154 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022155set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156{
22157 char_u *oldval;
22158 char_u *newval;
22159 unsigned len;
22160
Bram Moolenaare9a41262005-01-15 22:18:47 +000022161 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022162 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022164 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022165 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022166 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022167 }
22168
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022169 if (eap->force_bin == FORCE_BIN)
22170 len = 6;
22171 else if (eap->force_bin == FORCE_NOBIN)
22172 len = 8;
22173 else
22174 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022175
22176 if (eap->read_edit)
22177 len += 7;
22178
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022179 if (eap->force_ff != 0)
22180 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22181# ifdef FEAT_MBYTE
22182 if (eap->force_enc != 0)
22183 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022184 if (eap->bad_char != 0)
22185 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022186# endif
22187
22188 newval = alloc(len + 1);
22189 if (newval == NULL)
22190 return NULL;
22191
22192 if (eap->force_bin == FORCE_BIN)
22193 sprintf((char *)newval, " ++bin");
22194 else if (eap->force_bin == FORCE_NOBIN)
22195 sprintf((char *)newval, " ++nobin");
22196 else
22197 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022198
22199 if (eap->read_edit)
22200 STRCAT(newval, " ++edit");
22201
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022202 if (eap->force_ff != 0)
22203 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22204 eap->cmd + eap->force_ff);
22205# ifdef FEAT_MBYTE
22206 if (eap->force_enc != 0)
22207 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22208 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022209 if (eap->bad_char == BAD_KEEP)
22210 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22211 else if (eap->bad_char == BAD_DROP)
22212 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22213 else if (eap->bad_char != 0)
22214 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022215# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022216 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022217 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218}
22219#endif
22220
22221/*
22222 * Get the value of internal variable "name".
22223 * Return OK or FAIL.
22224 */
22225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022226get_var_tv(
22227 char_u *name,
22228 int len, /* length of "name" */
22229 typval_T *rettv, /* NULL when only checking existence */
22230 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22231 int verbose, /* may give error message */
22232 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022233{
22234 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022235 typval_T *tv = NULL;
22236 typval_T atv;
22237 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022239
22240 /* truncate the name, so that we can use strcmp() */
22241 cc = name[len];
22242 name[len] = NUL;
22243
22244 /*
22245 * Check for "b:changedtick".
22246 */
22247 if (STRCMP(name, "b:changedtick") == 0)
22248 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022249 atv.v_type = VAR_NUMBER;
22250 atv.vval.v_number = curbuf->b_changedtick;
22251 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022252 }
22253
22254 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022255 * Check for user-defined variables.
22256 */
22257 else
22258 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022259 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022260 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022261 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022262 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022263 if (dip != NULL)
22264 *dip = v;
22265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022266 }
22267
Bram Moolenaare9a41262005-01-15 22:18:47 +000022268 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022269 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022270 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022271 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022272 ret = FAIL;
22273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022274 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022275 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022276
22277 name[len] = cc;
22278
22279 return ret;
22280}
22281
22282/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022283 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22284 * Also handle function call with Funcref variable: func(expr)
22285 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22286 */
22287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022288handle_subscript(
22289 char_u **arg,
22290 typval_T *rettv,
22291 int evaluate, /* do more than finding the end */
22292 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022293{
22294 int ret = OK;
22295 dict_T *selfdict = NULL;
22296 char_u *s;
22297 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022298 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022299
22300 while (ret == OK
22301 && (**arg == '['
22302 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022303 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22304 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022305 && !vim_iswhite(*(*arg - 1)))
22306 {
22307 if (**arg == '(')
22308 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022309 partial_T *pt = NULL;
22310
Bram Moolenaard9fba312005-06-26 22:34:35 +000022311 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022312 if (evaluate)
22313 {
22314 functv = *rettv;
22315 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022316
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022317 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022318 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022319 {
22320 pt = functv.vval.v_partial;
22321 s = pt->pt_name;
22322 }
22323 else
22324 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022325 }
22326 else
22327 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022328 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022329 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022330 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022331
22332 /* Clear the funcref afterwards, so that deleting it while
22333 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022334 if (evaluate)
22335 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022336
22337 /* Stop the expression evaluation when immediately aborting on
22338 * error, or when an interrupt occurred or an exception was thrown
22339 * but not caught. */
22340 if (aborting())
22341 {
22342 if (ret == OK)
22343 clear_tv(rettv);
22344 ret = FAIL;
22345 }
22346 dict_unref(selfdict);
22347 selfdict = NULL;
22348 }
22349 else /* **arg == '[' || **arg == '.' */
22350 {
22351 dict_unref(selfdict);
22352 if (rettv->v_type == VAR_DICT)
22353 {
22354 selfdict = rettv->vval.v_dict;
22355 if (selfdict != NULL)
22356 ++selfdict->dv_refcount;
22357 }
22358 else
22359 selfdict = NULL;
22360 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22361 {
22362 clear_tv(rettv);
22363 ret = FAIL;
22364 }
22365 }
22366 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022367
Bram Moolenaar1d429612016-05-24 15:44:17 +020022368 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22369 * Don't do this when "Func" is already a partial that was bound
22370 * explicitly (pt_auto is FALSE). */
22371 if (selfdict != NULL
22372 && (rettv->v_type == VAR_FUNC
22373 || (rettv->v_type == VAR_PARTIAL
22374 && (rettv->vval.v_partial->pt_auto
22375 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022376 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022377 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22378 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022379 char_u *tofree = NULL;
22380 ufunc_T *fp;
22381 char_u fname_buf[FLEN_FIXED + 1];
22382 int error;
22383
22384 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022385 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022386 fp = find_func(fname);
22387 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022388
Bram Moolenaar65639032016-03-16 21:40:30 +010022389 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022390 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022391 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22392
22393 if (pt != NULL)
22394 {
22395 pt->pt_refcount = 1;
22396 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022397 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022398 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022399 if (rettv->v_type == VAR_FUNC)
22400 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022401 /* Just a function: Take over the function name and use
22402 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022403 pt->pt_name = rettv->vval.v_string;
22404 }
22405 else
22406 {
22407 partial_T *ret_pt = rettv->vval.v_partial;
22408 int i;
22409
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022410 /* Partial: copy the function name, use selfdict and copy
22411 * args. Can't take over name or args, the partial might
22412 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022413 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022414 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022415 if (ret_pt->pt_argc > 0)
22416 {
22417 pt->pt_argv = (typval_T *)alloc(
22418 sizeof(typval_T) * ret_pt->pt_argc);
22419 if (pt->pt_argv == NULL)
22420 /* out of memory: drop the arguments */
22421 pt->pt_argc = 0;
22422 else
22423 {
22424 pt->pt_argc = ret_pt->pt_argc;
22425 for (i = 0; i < pt->pt_argc; i++)
22426 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22427 }
22428 }
22429 partial_unref(ret_pt);
22430 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022431 rettv->v_type = VAR_PARTIAL;
22432 rettv->vval.v_partial = pt;
22433 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022434 }
22435 }
22436
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022437 dict_unref(selfdict);
22438 return ret;
22439}
22440
22441/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022442 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022443 * value).
22444 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022445 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022446alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022447{
Bram Moolenaar33570922005-01-25 22:26:29 +000022448 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022449}
22450
22451/*
22452 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 * The string "s" must have been allocated, it is consumed.
22454 * Return NULL for out of memory, the variable otherwise.
22455 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022456 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022457alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458{
Bram Moolenaar33570922005-01-25 22:26:29 +000022459 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022461 rettv = alloc_tv();
22462 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022464 rettv->v_type = VAR_STRING;
22465 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 }
22467 else
22468 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022469 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470}
22471
22472/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022473 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022476free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477{
22478 if (varp != NULL)
22479 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 switch (varp->v_type)
22481 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022482 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022483 func_unref(varp->vval.v_string);
22484 /*FALLTHROUGH*/
22485 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 vim_free(varp->vval.v_string);
22487 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022488 case VAR_PARTIAL:
22489 partial_unref(varp->vval.v_partial);
22490 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022491 case VAR_LIST:
22492 list_unref(varp->vval.v_list);
22493 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022494 case VAR_DICT:
22495 dict_unref(varp->vval.v_dict);
22496 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022497 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022499 job_unref(varp->vval.v_job);
22500 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022501#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022502 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022503#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022504 channel_unref(varp->vval.v_channel);
22505 break;
22506#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022507 case VAR_NUMBER:
22508 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022509 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022510 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022511 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 vim_free(varp);
22514 }
22515}
22516
22517/*
22518 * Free the memory for a variable value and set the value to NULL or 0.
22519 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022520 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022521clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522{
22523 if (varp != NULL)
22524 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022525 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022527 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022528 func_unref(varp->vval.v_string);
22529 /*FALLTHROUGH*/
22530 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022531 vim_free(varp->vval.v_string);
22532 varp->vval.v_string = NULL;
22533 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022534 case VAR_PARTIAL:
22535 partial_unref(varp->vval.v_partial);
22536 varp->vval.v_partial = NULL;
22537 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022538 case VAR_LIST:
22539 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022540 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022541 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022542 case VAR_DICT:
22543 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022544 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022545 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022546 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022547 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022548 varp->vval.v_number = 0;
22549 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022550 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022551#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022552 varp->vval.v_float = 0.0;
22553 break;
22554#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022555 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022556#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022557 job_unref(varp->vval.v_job);
22558 varp->vval.v_job = NULL;
22559#endif
22560 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022561 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022562#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022563 channel_unref(varp->vval.v_channel);
22564 varp->vval.v_channel = NULL;
22565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022566 case VAR_UNKNOWN:
22567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022569 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 }
22571}
22572
22573/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022574 * Set the value of a variable to NULL without freeing items.
22575 */
22576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022577init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022578{
22579 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022580 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022581}
22582
22583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 * Get the number value of a variable.
22585 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022586 * For incompatible types, return 0.
22587 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22588 * caller of incompatible types: it sets *denote to TRUE if "denote"
22589 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022591 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022592get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022594 int error = FALSE;
22595
22596 return get_tv_number_chk(varp, &error); /* return 0L on error */
22597}
22598
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022599 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022600get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022601{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022602 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022603
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022604 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022606 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022607 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022608 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022609#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022610 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022611 break;
22612#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022613 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022614 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022615 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022616 break;
22617 case VAR_STRING:
22618 if (varp->vval.v_string != NULL)
22619 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022620 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022621 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022622 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022623 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022624 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022625 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022626 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022627 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022628 case VAR_SPECIAL:
22629 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22630 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022631 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022633 EMSG(_("E910: Using a Job as a Number"));
22634 break;
22635#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022636 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022637#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022638 EMSG(_("E913: Using a Channel as a Number"));
22639 break;
22640#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022641 case VAR_UNKNOWN:
22642 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022643 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022645 if (denote == NULL) /* useful for values that must be unsigned */
22646 n = -1;
22647 else
22648 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650}
22651
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022652#ifdef FEAT_FLOAT
22653 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022654get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022655{
22656 switch (varp->v_type)
22657 {
22658 case VAR_NUMBER:
22659 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022660 case VAR_FLOAT:
22661 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022662 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022663 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022664 EMSG(_("E891: Using a Funcref as a Float"));
22665 break;
22666 case VAR_STRING:
22667 EMSG(_("E892: Using a String as a Float"));
22668 break;
22669 case VAR_LIST:
22670 EMSG(_("E893: Using a List as a Float"));
22671 break;
22672 case VAR_DICT:
22673 EMSG(_("E894: Using a Dictionary as a Float"));
22674 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022675 case VAR_SPECIAL:
22676 EMSG(_("E907: Using a special value as a Float"));
22677 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022678 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022679# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022680 EMSG(_("E911: Using a Job as a Float"));
22681 break;
22682# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022683 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022684# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022685 EMSG(_("E914: Using a Channel as a Float"));
22686 break;
22687# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022688 case VAR_UNKNOWN:
22689 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022690 break;
22691 }
22692 return 0;
22693}
22694#endif
22695
Bram Moolenaar071d4272004-06-13 20:20:40 +000022696/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022697 * Get the lnum from the first argument.
22698 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022699 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700 */
22701 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022702get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703{
Bram Moolenaar33570922005-01-25 22:26:29 +000022704 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705 linenr_T lnum;
22706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022707 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708 if (lnum == 0) /* no valid number, try using line() */
22709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022710 rettv.v_type = VAR_NUMBER;
22711 f_line(argvars, &rettv);
22712 lnum = rettv.vval.v_number;
22713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 }
22715 return lnum;
22716}
22717
22718/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022719 * Get the lnum from the first argument.
22720 * Also accepts "$", then "buf" is used.
22721 * Returns 0 on error.
22722 */
22723 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022724get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022725{
22726 if (argvars[0].v_type == VAR_STRING
22727 && argvars[0].vval.v_string != NULL
22728 && argvars[0].vval.v_string[0] == '$'
22729 && buf != NULL)
22730 return buf->b_ml.ml_line_count;
22731 return get_tv_number_chk(&argvars[0], NULL);
22732}
22733
22734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022735 * Get the string value of a variable.
22736 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022737 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22738 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 * If the String variable has never been set, return an empty string.
22740 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022741 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22742 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022744 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022745get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746{
22747 static char_u mybuf[NUMBUFLEN];
22748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022749 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750}
22751
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022752 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022753get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022755 char_u *res = get_tv_string_buf_chk(varp, buf);
22756
22757 return res != NULL ? res : (char_u *)"";
22758}
22759
Bram Moolenaar7d647822014-04-05 21:28:56 +020022760/*
22761 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22762 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022763 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022764get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022765{
22766 static char_u mybuf[NUMBUFLEN];
22767
22768 return get_tv_string_buf_chk(varp, mybuf);
22769}
22770
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022771 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022772get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022773{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022774 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022776 case VAR_NUMBER:
22777 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22778 return buf;
22779 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022780 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022781 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022782 break;
22783 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022784 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022785 break;
22786 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022787 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022789 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022790#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022791 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022792 break;
22793#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022794 case VAR_STRING:
22795 if (varp->vval.v_string != NULL)
22796 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022797 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022798 case VAR_SPECIAL:
22799 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22800 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022801 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022802#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022803 {
22804 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022805 char *status;
22806
22807 if (job == NULL)
22808 return (char_u *)"no process";
22809 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022810 : job->jv_status == JOB_ENDED ? "dead"
22811 : "run";
22812# ifdef UNIX
22813 vim_snprintf((char *)buf, NUMBUFLEN,
22814 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022815# elif defined(WIN32)
22816 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022817 "process %ld %s",
22818 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022819 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022820# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022821 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022822 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22823# endif
22824 return buf;
22825 }
22826#endif
22827 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022828 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022829#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022830 {
22831 channel_T *channel = varp->vval.v_channel;
22832 char *status = channel_status(channel);
22833
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022834 if (channel == NULL)
22835 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22836 else
22837 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022838 "channel %d %s", channel->ch_id, status);
22839 return buf;
22840 }
22841#endif
22842 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022843 case VAR_UNKNOWN:
22844 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022845 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022847 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848}
22849
22850/*
22851 * Find variable "name" in the list of variables.
22852 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022853 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022854 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022855 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022857 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022858find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022859{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022861 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862
Bram Moolenaara7043832005-01-21 11:56:39 +000022863 ht = find_var_ht(name, &varname);
22864 if (htp != NULL)
22865 *htp = ht;
22866 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022868 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869}
22870
22871/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022872 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022873 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022875 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022876find_var_in_ht(
22877 hashtab_T *ht,
22878 int htname,
22879 char_u *varname,
22880 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022881{
Bram Moolenaar33570922005-01-25 22:26:29 +000022882 hashitem_T *hi;
22883
22884 if (*varname == NUL)
22885 {
22886 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022887 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022888 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022889 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022890 case 'g': return &globvars_var;
22891 case 'v': return &vimvars_var;
22892 case 'b': return &curbuf->b_bufvar;
22893 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022894#ifdef FEAT_WINDOWS
22895 case 't': return &curtab->tp_winvar;
22896#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022897 case 'l': return current_funccal == NULL
22898 ? NULL : &current_funccal->l_vars_var;
22899 case 'a': return current_funccal == NULL
22900 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022901 }
22902 return NULL;
22903 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022904
22905 hi = hash_find(ht, varname);
22906 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022907 {
22908 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022909 * worked find the variable again. Don't auto-load a script if it was
22910 * loaded already, otherwise it would be loaded every time when
22911 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022912 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022913 {
22914 /* Note: script_autoload() may make "hi" invalid. It must either
22915 * be obtained again or not used. */
22916 if (!script_autoload(varname, FALSE) || aborting())
22917 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022918 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022919 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022920 if (HASHITEM_EMPTY(hi))
22921 return NULL;
22922 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022924}
22925
22926/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022928 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022929 * Set "varname" to the start of name without ':'.
22930 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022931 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022932find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022934 hashitem_T *hi;
22935
Bram Moolenaar73627d02015-08-11 15:46:09 +020022936 if (name[0] == NUL)
22937 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 if (name[1] != ':')
22939 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022940 /* The name must not start with a colon or #. */
22941 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 return NULL;
22943 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022944
22945 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022946 hi = hash_find(&compat_hashtab, name);
22947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022948 return &compat_hashtab;
22949
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022951 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022952 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 }
22954 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022955 if (*name == 'g') /* global variable */
22956 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022957 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22958 */
22959 if (vim_strchr(name + 2, ':') != NULL
22960 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022961 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022963 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022964 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022965 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022966#ifdef FEAT_WINDOWS
22967 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022968 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022969#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022970 if (*name == 'v') /* v: variable */
22971 return &vimvarht;
22972 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022973 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022975 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976 if (*name == 's' /* script variable */
22977 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22978 return &SCRIPT_VARS(current_SID);
22979 return NULL;
22980}
22981
22982/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022983 * Get function call environment based on bactrace debug level
22984 */
22985 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022986get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022987{
22988 int i;
22989 funccall_T *funccal;
22990 funccall_T *temp_funccal;
22991
22992 funccal = current_funccal;
22993 if (debug_backtrace_level > 0)
22994 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022995 for (i = 0; i < debug_backtrace_level; i++)
22996 {
22997 temp_funccal = funccal->caller;
22998 if (temp_funccal)
22999 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023000 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023001 /* backtrace level overflow. reset to max */
23002 debug_backtrace_level = i;
23003 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023004 }
23005 return funccal;
23006}
23007
23008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023010 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 * Returns NULL when it doesn't exist.
23012 */
23013 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023014get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015{
Bram Moolenaar33570922005-01-25 22:26:29 +000023016 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023017
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023018 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023019 if (v == NULL)
23020 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023022}
23023
23024/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023025 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023026 * sourcing this script and when executing functions defined in the script.
23027 */
23028 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023029new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023030{
Bram Moolenaara7043832005-01-21 11:56:39 +000023031 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 hashtab_T *ht;
23033 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023034
Bram Moolenaar071d4272004-06-13 20:20:40 +000023035 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23036 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023037 /* Re-allocating ga_data means that an ht_array pointing to
23038 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023039 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023040 for (i = 1; i <= ga_scripts.ga_len; ++i)
23041 {
23042 ht = &SCRIPT_VARS(i);
23043 if (ht->ht_mask == HT_INIT_SIZE - 1)
23044 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023045 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023046 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023047 }
23048
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 while (ga_scripts.ga_len < id)
23050 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023051 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023052 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023053 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 }
23056 }
23057}
23058
23059/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023060 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23061 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023062 */
23063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023064init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065{
Bram Moolenaar33570922005-01-25 22:26:29 +000023066 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023067 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023068 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023069 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023070 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023071 dict_var->di_tv.vval.v_dict = dict;
23072 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023073 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023074 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23075 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023076}
23077
23078/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023079 * Unreference a dictionary initialized by init_var_dict().
23080 */
23081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023082unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023083{
23084 /* Now the dict needs to be freed if no one else is using it, go back to
23085 * normal reference counting. */
23086 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23087 dict_unref(dict);
23088}
23089
23090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023092 * Frees all allocated variables and the value they contain.
23093 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 */
23095 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023096vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023097{
23098 vars_clear_ext(ht, TRUE);
23099}
23100
23101/*
23102 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23103 */
23104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023105vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106{
Bram Moolenaara7043832005-01-21 11:56:39 +000023107 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023108 hashitem_T *hi;
23109 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110
Bram Moolenaar33570922005-01-25 22:26:29 +000023111 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023112 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023113 for (hi = ht->ht_array; todo > 0; ++hi)
23114 {
23115 if (!HASHITEM_EMPTY(hi))
23116 {
23117 --todo;
23118
Bram Moolenaar33570922005-01-25 22:26:29 +000023119 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023120 * ht_array might change then. hash_clear() takes care of it
23121 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023122 v = HI2DI(hi);
23123 if (free_val)
23124 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023125 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023126 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023127 }
23128 }
23129 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023130 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131}
23132
Bram Moolenaara7043832005-01-21 11:56:39 +000023133/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023134 * Delete a variable from hashtab "ht" at item "hi".
23135 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023138delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139{
Bram Moolenaar33570922005-01-25 22:26:29 +000023140 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023141
23142 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023143 clear_tv(&di->di_tv);
23144 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145}
23146
23147/*
23148 * List the value of one internal variable.
23149 */
23150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023151list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023153 char_u *tofree;
23154 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023155 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023156
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023157 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023158 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023159 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023160 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161}
23162
Bram Moolenaar071d4272004-06-13 20:20:40 +000023163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023164list_one_var_a(
23165 char_u *prefix,
23166 char_u *name,
23167 int type,
23168 char_u *string,
23169 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170{
Bram Moolenaar31859182007-08-14 20:41:13 +000023171 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23172 msg_start();
23173 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 if (name != NULL) /* "a:" vars don't have a name stored */
23175 msg_puts(name);
23176 msg_putchar(' ');
23177 msg_advance(22);
23178 if (type == VAR_NUMBER)
23179 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023180 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023181 msg_putchar('*');
23182 else if (type == VAR_LIST)
23183 {
23184 msg_putchar('[');
23185 if (*string == '[')
23186 ++string;
23187 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023188 else if (type == VAR_DICT)
23189 {
23190 msg_putchar('{');
23191 if (*string == '{')
23192 ++string;
23193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 else
23195 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023196
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023198
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023199 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023201 if (*first)
23202 {
23203 msg_clr_eos();
23204 *first = FALSE;
23205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206}
23207
23208/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023209 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023210 * If the variable already exists, the value is updated.
23211 * Otherwise the variable is created.
23212 */
23213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023214set_var(
23215 char_u *name,
23216 typval_T *tv,
23217 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218{
Bram Moolenaar33570922005-01-25 22:26:29 +000023219 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023222
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023223 ht = find_var_ht(name, &varname);
23224 if (ht == NULL || *varname == NUL)
23225 {
23226 EMSG2(_(e_illvar), name);
23227 return;
23228 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023229 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023230
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023231 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23232 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023233 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023234
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023237 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023238 if (var_check_ro(v->di_flags, name, FALSE)
23239 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023240 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023241
23242 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023243 * Handle setting internal v: variables separately where needed to
23244 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023245 */
23246 if (ht == &vimvarht)
23247 {
23248 if (v->di_tv.v_type == VAR_STRING)
23249 {
23250 vim_free(v->di_tv.vval.v_string);
23251 if (copy || tv->v_type != VAR_STRING)
23252 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23253 else
23254 {
23255 /* Take over the string to avoid an extra alloc/free. */
23256 v->di_tv.vval.v_string = tv->vval.v_string;
23257 tv->vval.v_string = NULL;
23258 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023259 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023260 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023261 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023262 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023263 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023264 if (STRCMP(varname, "searchforward") == 0)
23265 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023266#ifdef FEAT_SEARCH_EXTRA
23267 else if (STRCMP(varname, "hlsearch") == 0)
23268 {
23269 no_hlsearch = !v->di_tv.vval.v_number;
23270 redraw_all_later(SOME_VALID);
23271 }
23272#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023273 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023274 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023275 else if (v->di_tv.v_type != tv->v_type)
23276 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023277 }
23278
23279 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 }
23281 else /* add a new variable */
23282 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023283 /* Can't add "v:" variable. */
23284 if (ht == &vimvarht)
23285 {
23286 EMSG2(_(e_illvar), name);
23287 return;
23288 }
23289
Bram Moolenaar92124a32005-06-17 22:03:40 +000023290 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023291 if (!valid_varname(varname))
23292 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023293
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023294 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23295 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023296 if (v == NULL)
23297 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023298 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023299 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023300 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023301 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302 return;
23303 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023304 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023306
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023307 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023308 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023309 else
23310 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023311 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023312 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023313 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315}
23316
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023317/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023318 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023319 * Also give an error message.
23320 */
23321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023322var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023323{
23324 if (flags & DI_FLAGS_RO)
23325 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023326 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023327 return TRUE;
23328 }
23329 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23330 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023331 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023332 return TRUE;
23333 }
23334 return FALSE;
23335}
23336
23337/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023338 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23339 * Also give an error message.
23340 */
23341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023342var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023343{
23344 if (flags & DI_FLAGS_FIX)
23345 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023346 EMSG2(_("E795: Cannot delete variable %s"),
23347 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023348 return TRUE;
23349 }
23350 return FALSE;
23351}
23352
23353/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023354 * Check if a funcref is assigned to a valid variable name.
23355 * Return TRUE and give an error if not.
23356 */
23357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023358var_check_func_name(
23359 char_u *name, /* points to start of variable name */
23360 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023361{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023362 /* Allow for w: b: s: and t:. */
23363 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023364 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23365 ? name[2] : name[0]))
23366 {
23367 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23368 name);
23369 return TRUE;
23370 }
23371 /* Don't allow hiding a function. When "v" is not NULL we might be
23372 * assigning another function to the same var, the type is checked
23373 * below. */
23374 if (new_var && function_exists(name))
23375 {
23376 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23377 name);
23378 return TRUE;
23379 }
23380 return FALSE;
23381}
23382
23383/*
23384 * Check if a variable name is valid.
23385 * Return FALSE and give an error if not.
23386 */
23387 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023389{
23390 char_u *p;
23391
23392 for (p = varname; *p != NUL; ++p)
23393 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23394 && *p != AUTOLOAD_CHAR)
23395 {
23396 EMSG2(_(e_illvar), varname);
23397 return FALSE;
23398 }
23399 return TRUE;
23400}
23401
23402/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023403 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023404 * Also give an error message, using "name" or _("name") when use_gettext is
23405 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023406 */
23407 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023408tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023409{
23410 if (lock & VAR_LOCKED)
23411 {
23412 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023413 name == NULL ? (char_u *)_("Unknown")
23414 : use_gettext ? (char_u *)_(name)
23415 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023416 return TRUE;
23417 }
23418 if (lock & VAR_FIXED)
23419 {
23420 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023421 name == NULL ? (char_u *)_("Unknown")
23422 : use_gettext ? (char_u *)_(name)
23423 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023424 return TRUE;
23425 }
23426 return FALSE;
23427}
23428
23429/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023430 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023431 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023432 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023433 * It is OK for "from" and "to" to point to the same item. This is used to
23434 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023435 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023437copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023438{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023439 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023440 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023441 switch (from->v_type)
23442 {
23443 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023444 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023445 to->vval.v_number = from->vval.v_number;
23446 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023447 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023448#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023449 to->vval.v_float = from->vval.v_float;
23450 break;
23451#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023452 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023453#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023454 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023455 if (to->vval.v_job != NULL)
23456 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023457 break;
23458#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023459 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023460#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023461 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023462 if (to->vval.v_channel != NULL)
23463 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023464 break;
23465#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023466 case VAR_STRING:
23467 case VAR_FUNC:
23468 if (from->vval.v_string == NULL)
23469 to->vval.v_string = NULL;
23470 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023471 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023472 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023473 if (from->v_type == VAR_FUNC)
23474 func_ref(to->vval.v_string);
23475 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023476 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023477 case VAR_PARTIAL:
23478 if (from->vval.v_partial == NULL)
23479 to->vval.v_partial = NULL;
23480 else
23481 {
23482 to->vval.v_partial = from->vval.v_partial;
23483 ++to->vval.v_partial->pt_refcount;
23484 }
23485 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023486 case VAR_LIST:
23487 if (from->vval.v_list == NULL)
23488 to->vval.v_list = NULL;
23489 else
23490 {
23491 to->vval.v_list = from->vval.v_list;
23492 ++to->vval.v_list->lv_refcount;
23493 }
23494 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023495 case VAR_DICT:
23496 if (from->vval.v_dict == NULL)
23497 to->vval.v_dict = NULL;
23498 else
23499 {
23500 to->vval.v_dict = from->vval.v_dict;
23501 ++to->vval.v_dict->dv_refcount;
23502 }
23503 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023504 case VAR_UNKNOWN:
23505 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023506 break;
23507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508}
23509
23510/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023511 * Make a copy of an item.
23512 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023513 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23514 * reference to an already copied list/dict can be used.
23515 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023516 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023518item_copy(
23519 typval_T *from,
23520 typval_T *to,
23521 int deep,
23522 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023523{
23524 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023525 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023526
Bram Moolenaar33570922005-01-25 22:26:29 +000023527 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023528 {
23529 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023530 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023531 }
23532 ++recurse;
23533
23534 switch (from->v_type)
23535 {
23536 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023537 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023538 case VAR_STRING:
23539 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023540 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023541 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023542 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023543 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023544 copy_tv(from, to);
23545 break;
23546 case VAR_LIST:
23547 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023548 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023549 if (from->vval.v_list == NULL)
23550 to->vval.v_list = NULL;
23551 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23552 {
23553 /* use the copy made earlier */
23554 to->vval.v_list = from->vval.v_list->lv_copylist;
23555 ++to->vval.v_list->lv_refcount;
23556 }
23557 else
23558 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23559 if (to->vval.v_list == NULL)
23560 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023561 break;
23562 case VAR_DICT:
23563 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023564 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023565 if (from->vval.v_dict == NULL)
23566 to->vval.v_dict = NULL;
23567 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23568 {
23569 /* use the copy made earlier */
23570 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23571 ++to->vval.v_dict->dv_refcount;
23572 }
23573 else
23574 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23575 if (to->vval.v_dict == NULL)
23576 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023577 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023578 case VAR_UNKNOWN:
23579 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023580 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023581 }
23582 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023583 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023584}
23585
23586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 * ":echo expr1 ..." print each argument separated with a space, add a
23588 * newline at the end.
23589 * ":echon expr1 ..." print each argument plain.
23590 */
23591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023592ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593{
23594 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023595 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023596 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023597 char_u *p;
23598 int needclr = TRUE;
23599 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023600 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023601
23602 if (eap->skip)
23603 ++emsg_skip;
23604 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23605 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023606 /* If eval1() causes an error message the text from the command may
23607 * still need to be cleared. E.g., "echo 22,44". */
23608 need_clr_eos = needclr;
23609
Bram Moolenaar071d4272004-06-13 20:20:40 +000023610 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023611 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023612 {
23613 /*
23614 * Report the invalid expression unless the expression evaluation
23615 * has been cancelled due to an aborting error, an interrupt, or an
23616 * exception.
23617 */
23618 if (!aborting())
23619 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023620 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023621 break;
23622 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023623 need_clr_eos = FALSE;
23624
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625 if (!eap->skip)
23626 {
23627 if (atstart)
23628 {
23629 atstart = FALSE;
23630 /* Call msg_start() after eval1(), evaluating the expression
23631 * may cause a message to appear. */
23632 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023633 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023634 /* Mark the saved text as finishing the line, so that what
23635 * follows is displayed on a new line when scrolling back
23636 * at the more prompt. */
23637 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 }
23641 else if (eap->cmdidx == CMD_echo)
23642 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023643 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023644 if (p != NULL)
23645 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023646 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023647 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023649 if (*p != TAB && needclr)
23650 {
23651 /* remove any text still there from the command */
23652 msg_clr_eos();
23653 needclr = FALSE;
23654 }
23655 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656 }
23657 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023658 {
23659#ifdef FEAT_MBYTE
23660 if (has_mbyte)
23661 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023662 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023663
23664 (void)msg_outtrans_len_attr(p, i, echo_attr);
23665 p += i - 1;
23666 }
23667 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023668#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023669 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023671 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023672 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023674 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023675 arg = skipwhite(arg);
23676 }
23677 eap->nextcmd = check_nextcmd(arg);
23678
23679 if (eap->skip)
23680 --emsg_skip;
23681 else
23682 {
23683 /* remove text that may still be there from the command */
23684 if (needclr)
23685 msg_clr_eos();
23686 if (eap->cmdidx == CMD_echo)
23687 msg_end();
23688 }
23689}
23690
23691/*
23692 * ":echohl {name}".
23693 */
23694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023695ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696{
23697 int id;
23698
23699 id = syn_name2id(eap->arg);
23700 if (id == 0)
23701 echo_attr = 0;
23702 else
23703 echo_attr = syn_id2attr(id);
23704}
23705
23706/*
23707 * ":execute expr1 ..." execute the result of an expression.
23708 * ":echomsg expr1 ..." Print a message
23709 * ":echoerr expr1 ..." Print an error
23710 * Each gets spaces around each argument and a newline at the end for
23711 * echo commands
23712 */
23713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023714ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715{
23716 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023717 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 int ret = OK;
23719 char_u *p;
23720 garray_T ga;
23721 int len;
23722 int save_did_emsg;
23723
23724 ga_init2(&ga, 1, 80);
23725
23726 if (eap->skip)
23727 ++emsg_skip;
23728 while (*arg != NUL && *arg != '|' && *arg != '\n')
23729 {
23730 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023731 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 {
23733 /*
23734 * Report the invalid expression unless the expression evaluation
23735 * has been cancelled due to an aborting error, an interrupt, or an
23736 * exception.
23737 */
23738 if (!aborting())
23739 EMSG2(_(e_invexpr2), p);
23740 ret = FAIL;
23741 break;
23742 }
23743
23744 if (!eap->skip)
23745 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023746 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 len = (int)STRLEN(p);
23748 if (ga_grow(&ga, len + 2) == FAIL)
23749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 ret = FAIL;
23752 break;
23753 }
23754 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023756 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 ga.ga_len += len;
23758 }
23759
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023760 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 arg = skipwhite(arg);
23762 }
23763
23764 if (ret != FAIL && ga.ga_data != NULL)
23765 {
23766 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023769 out_flush();
23770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 else if (eap->cmdidx == CMD_echoerr)
23772 {
23773 /* We don't want to abort following commands, restore did_emsg. */
23774 save_did_emsg = did_emsg;
23775 EMSG((char_u *)ga.ga_data);
23776 if (!force_abort)
23777 did_emsg = save_did_emsg;
23778 }
23779 else if (eap->cmdidx == CMD_execute)
23780 do_cmdline((char_u *)ga.ga_data,
23781 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23782 }
23783
23784 ga_clear(&ga);
23785
23786 if (eap->skip)
23787 --emsg_skip;
23788
23789 eap->nextcmd = check_nextcmd(arg);
23790}
23791
23792/*
23793 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23794 * "arg" points to the "&" or '+' when called, to "option" when returning.
23795 * Returns NULL when no option name found. Otherwise pointer to the char
23796 * after the option name.
23797 */
23798 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023799find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800{
23801 char_u *p = *arg;
23802
23803 ++p;
23804 if (*p == 'g' && p[1] == ':')
23805 {
23806 *opt_flags = OPT_GLOBAL;
23807 p += 2;
23808 }
23809 else if (*p == 'l' && p[1] == ':')
23810 {
23811 *opt_flags = OPT_LOCAL;
23812 p += 2;
23813 }
23814 else
23815 *opt_flags = 0;
23816
23817 if (!ASCII_ISALPHA(*p))
23818 return NULL;
23819 *arg = p;
23820
23821 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23822 p += 4; /* termcap option */
23823 else
23824 while (ASCII_ISALPHA(*p))
23825 ++p;
23826 return p;
23827}
23828
23829/*
23830 * ":function"
23831 */
23832 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023833ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834{
23835 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023836 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 int j;
23838 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023840 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 char_u *name = NULL;
23842 char_u *p;
23843 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023844 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023845 garray_T newargs;
23846 garray_T newlines;
23847 int varargs = FALSE;
23848 int mustend = FALSE;
23849 int flags = 0;
23850 ufunc_T *fp;
23851 int indent;
23852 int nesting;
23853 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023854 dictitem_T *v;
23855 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 static int func_nr = 0; /* number for nameless function */
23857 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023858 hashtab_T *ht;
23859 int todo;
23860 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023861 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023862
23863 /*
23864 * ":function" without argument: list functions.
23865 */
23866 if (ends_excmd(*eap->arg))
23867 {
23868 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023869 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023870 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023871 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023872 {
23873 if (!HASHITEM_EMPTY(hi))
23874 {
23875 --todo;
23876 fp = HI2UF(hi);
23877 if (!isdigit(*fp->uf_name))
23878 list_func_head(fp, FALSE);
23879 }
23880 }
23881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 eap->nextcmd = check_nextcmd(eap->arg);
23883 return;
23884 }
23885
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023886 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023887 * ":function /pat": list functions matching pattern.
23888 */
23889 if (*eap->arg == '/')
23890 {
23891 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23892 if (!eap->skip)
23893 {
23894 regmatch_T regmatch;
23895
23896 c = *p;
23897 *p = NUL;
23898 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23899 *p = c;
23900 if (regmatch.regprog != NULL)
23901 {
23902 regmatch.rm_ic = p_ic;
23903
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023904 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023905 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23906 {
23907 if (!HASHITEM_EMPTY(hi))
23908 {
23909 --todo;
23910 fp = HI2UF(hi);
23911 if (!isdigit(*fp->uf_name)
23912 && vim_regexec(&regmatch, fp->uf_name, 0))
23913 list_func_head(fp, FALSE);
23914 }
23915 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023916 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023917 }
23918 }
23919 if (*p == '/')
23920 ++p;
23921 eap->nextcmd = check_nextcmd(p);
23922 return;
23923 }
23924
23925 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023926 * Get the function name. There are these situations:
23927 * func normal function name
23928 * "name" == func, "fudi.fd_dict" == NULL
23929 * dict.func new dictionary entry
23930 * "name" == NULL, "fudi.fd_dict" set,
23931 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23932 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023933 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023934 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23935 * dict.func existing dict entry that's not a Funcref
23936 * "name" == NULL, "fudi.fd_dict" set,
23937 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023938 * s:func script-local function name
23939 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023941 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023942 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023943 paren = (vim_strchr(p, '(') != NULL);
23944 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023945 {
23946 /*
23947 * Return on an invalid expression in braces, unless the expression
23948 * evaluation has been cancelled due to an aborting error, an
23949 * interrupt, or an exception.
23950 */
23951 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023952 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023953 if (!eap->skip && fudi.fd_newkey != NULL)
23954 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023955 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 else
23959 eap->skip = TRUE;
23960 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023961
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 /* An error in a function call during evaluation of an expression in magic
23963 * braces should not cause the function not to be defined. */
23964 saved_did_emsg = did_emsg;
23965 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966
23967 /*
23968 * ":function func" with only function name: list function.
23969 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023970 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023971 {
23972 if (!ends_excmd(*skipwhite(p)))
23973 {
23974 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023975 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 }
23977 eap->nextcmd = check_nextcmd(p);
23978 if (eap->nextcmd != NULL)
23979 *p = NUL;
23980 if (!eap->skip && !got_int)
23981 {
23982 fp = find_func(name);
23983 if (fp != NULL)
23984 {
23985 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023986 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023988 if (FUNCLINE(fp, j) == NULL)
23989 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023990 msg_putchar('\n');
23991 msg_outnum((long)(j + 1));
23992 if (j < 9)
23993 msg_putchar(' ');
23994 if (j < 99)
23995 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023996 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023997 out_flush(); /* show a line at a time */
23998 ui_breakcheck();
23999 }
24000 if (!got_int)
24001 {
24002 msg_putchar('\n');
24003 msg_puts((char_u *)" endfunction");
24004 }
24005 }
24006 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024007 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024009 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 }
24011
24012 /*
24013 * ":function name(arg1, arg2)" Define function.
24014 */
24015 p = skipwhite(p);
24016 if (*p != '(')
24017 {
24018 if (!eap->skip)
24019 {
24020 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024021 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024022 }
24023 /* attempt to continue by skipping some text */
24024 if (vim_strchr(p, '(') != NULL)
24025 p = vim_strchr(p, '(');
24026 }
24027 p = skipwhite(p + 1);
24028
24029 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24030 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24031
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024032 if (!eap->skip)
24033 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024034 /* Check the name of the function. Unless it's a dictionary function
24035 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024036 if (name != NULL)
24037 arg = name;
24038 else
24039 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024040 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024041 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24042 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024043 {
24044 if (*arg == K_SPECIAL)
24045 j = 3;
24046 else
24047 j = 0;
24048 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24049 : eval_isnamec(arg[j])))
24050 ++j;
24051 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024052 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024053 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024054 /* Disallow using the g: dict. */
24055 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24056 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024057 }
24058
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 /*
24060 * Isolate the arguments: "arg1, arg2, ...)"
24061 */
24062 while (*p != ')')
24063 {
24064 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24065 {
24066 varargs = TRUE;
24067 p += 3;
24068 mustend = TRUE;
24069 }
24070 else
24071 {
24072 arg = p;
24073 while (ASCII_ISALNUM(*p) || *p == '_')
24074 ++p;
24075 if (arg == p || isdigit(*arg)
24076 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24077 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24078 {
24079 if (!eap->skip)
24080 EMSG2(_("E125: Illegal argument: %s"), arg);
24081 break;
24082 }
24083 if (ga_grow(&newargs, 1) == FAIL)
24084 goto erret;
24085 c = *p;
24086 *p = NUL;
24087 arg = vim_strsave(arg);
24088 if (arg == NULL)
24089 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024090
24091 /* Check for duplicate argument name. */
24092 for (i = 0; i < newargs.ga_len; ++i)
24093 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24094 {
24095 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024096 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024097 goto erret;
24098 }
24099
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24101 *p = c;
24102 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 if (*p == ',')
24104 ++p;
24105 else
24106 mustend = TRUE;
24107 }
24108 p = skipwhite(p);
24109 if (mustend && *p != ')')
24110 {
24111 if (!eap->skip)
24112 EMSG2(_(e_invarg2), eap->arg);
24113 break;
24114 }
24115 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024116 if (*p != ')')
24117 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 ++p; /* skip the ')' */
24119
Bram Moolenaare9a41262005-01-15 22:18:47 +000024120 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 for (;;)
24122 {
24123 p = skipwhite(p);
24124 if (STRNCMP(p, "range", 5) == 0)
24125 {
24126 flags |= FC_RANGE;
24127 p += 5;
24128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024129 else if (STRNCMP(p, "dict", 4) == 0)
24130 {
24131 flags |= FC_DICT;
24132 p += 4;
24133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 else if (STRNCMP(p, "abort", 5) == 0)
24135 {
24136 flags |= FC_ABORT;
24137 p += 5;
24138 }
24139 else
24140 break;
24141 }
24142
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024143 /* When there is a line break use what follows for the function body.
24144 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24145 if (*p == '\n')
24146 line_arg = p + 1;
24147 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148 EMSG(_(e_trailing));
24149
24150 /*
24151 * Read the body of the function, until ":endfunction" is found.
24152 */
24153 if (KeyTyped)
24154 {
24155 /* Check if the function already exists, don't let the user type the
24156 * whole function before telling him it doesn't work! For a script we
24157 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 if (!eap->skip && !eap->forceit)
24159 {
24160 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24161 EMSG(_(e_funcdict));
24162 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024163 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024165
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024166 if (!eap->skip && did_emsg)
24167 goto erret;
24168
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 msg_putchar('\n'); /* don't overwrite the function name */
24170 cmdline_row = msg_row;
24171 }
24172
24173 indent = 2;
24174 nesting = 0;
24175 for (;;)
24176 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024177 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024178 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024179 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024180 saved_wait_return = FALSE;
24181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024183 sourcing_lnum_off = sourcing_lnum;
24184
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024185 if (line_arg != NULL)
24186 {
24187 /* Use eap->arg, split up in parts by line breaks. */
24188 theline = line_arg;
24189 p = vim_strchr(theline, '\n');
24190 if (p == NULL)
24191 line_arg += STRLEN(line_arg);
24192 else
24193 {
24194 *p = NUL;
24195 line_arg = p + 1;
24196 }
24197 }
24198 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199 theline = getcmdline(':', 0L, indent);
24200 else
24201 theline = eap->getline(':', eap->cookie, indent);
24202 if (KeyTyped)
24203 lines_left = Rows - 1;
24204 if (theline == NULL)
24205 {
24206 EMSG(_("E126: Missing :endfunction"));
24207 goto erret;
24208 }
24209
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024210 /* Detect line continuation: sourcing_lnum increased more than one. */
24211 if (sourcing_lnum > sourcing_lnum_off + 1)
24212 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24213 else
24214 sourcing_lnum_off = 0;
24215
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216 if (skip_until != NULL)
24217 {
24218 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24219 * don't check for ":endfunc". */
24220 if (STRCMP(theline, skip_until) == 0)
24221 {
24222 vim_free(skip_until);
24223 skip_until = NULL;
24224 }
24225 }
24226 else
24227 {
24228 /* skip ':' and blanks*/
24229 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24230 ;
24231
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024232 /* Check for "endfunction". */
24233 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024235 if (line_arg == NULL)
24236 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 break;
24238 }
24239
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024240 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 * at "end". */
24242 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24243 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024244 else if (STRNCMP(p, "if", 2) == 0
24245 || STRNCMP(p, "wh", 2) == 0
24246 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 || STRNCMP(p, "try", 3) == 0)
24248 indent += 2;
24249
24250 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024251 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024252 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024253 if (*p == '!')
24254 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024255 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024256 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024257 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024259 ++nesting;
24260 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024261 }
24262 }
24263
24264 /* Check for ":append" or ":insert". */
24265 p = skip_range(p, NULL);
24266 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24267 || (p[0] == 'i'
24268 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24269 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24270 skip_until = vim_strsave((char_u *)".");
24271
24272 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24273 arg = skipwhite(skiptowhite(p));
24274 if (arg[0] == '<' && arg[1] =='<'
24275 && ((p[0] == 'p' && p[1] == 'y'
24276 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24277 || (p[0] == 'p' && p[1] == 'e'
24278 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24279 || (p[0] == 't' && p[1] == 'c'
24280 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024281 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24282 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024283 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24284 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024285 || (p[0] == 'm' && p[1] == 'z'
24286 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 ))
24288 {
24289 /* ":python <<" continues until a dot, like ":append" */
24290 p = skipwhite(arg + 2);
24291 if (*p == NUL)
24292 skip_until = vim_strsave((char_u *)".");
24293 else
24294 skip_until = vim_strsave(p);
24295 }
24296 }
24297
24298 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024299 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024300 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024301 if (line_arg == NULL)
24302 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024303 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024304 }
24305
24306 /* Copy the line to newly allocated memory. get_one_sourceline()
24307 * allocates 250 bytes per line, this saves 80% on average. The cost
24308 * is an extra alloc/free. */
24309 p = vim_strsave(theline);
24310 if (p != NULL)
24311 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024312 if (line_arg == NULL)
24313 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024314 theline = p;
24315 }
24316
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024317 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24318
24319 /* Add NULL lines for continuation lines, so that the line count is
24320 * equal to the index in the growarray. */
24321 while (sourcing_lnum_off-- > 0)
24322 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024323
24324 /* Check for end of eap->arg. */
24325 if (line_arg != NULL && *line_arg == NUL)
24326 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024327 }
24328
24329 /* Don't define the function when skipping commands or when an error was
24330 * detected. */
24331 if (eap->skip || did_emsg)
24332 goto erret;
24333
24334 /*
24335 * If there are no errors, add the function
24336 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024337 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024338 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024339 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024340 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024341 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024342 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024343 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024344 goto erret;
24345 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024346
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024347 fp = find_func(name);
24348 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024350 if (!eap->forceit)
24351 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024352 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024353 goto erret;
24354 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024355 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024356 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024357 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024358 name);
24359 goto erret;
24360 }
24361 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024362 ga_clear_strings(&(fp->uf_args));
24363 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024364 vim_free(name);
24365 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024367 }
24368 else
24369 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024370 char numbuf[20];
24371
24372 fp = NULL;
24373 if (fudi.fd_newkey == NULL && !eap->forceit)
24374 {
24375 EMSG(_(e_funcdict));
24376 goto erret;
24377 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024378 if (fudi.fd_di == NULL)
24379 {
24380 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024381 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024382 goto erret;
24383 }
24384 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024385 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024386 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024387
24388 /* Give the function a sequential number. Can only be used with a
24389 * Funcref! */
24390 vim_free(name);
24391 sprintf(numbuf, "%d", ++func_nr);
24392 name = vim_strsave((char_u *)numbuf);
24393 if (name == NULL)
24394 goto erret;
24395 }
24396
24397 if (fp == NULL)
24398 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024399 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024400 {
24401 int slen, plen;
24402 char_u *scriptname;
24403
24404 /* Check that the autoload name matches the script name. */
24405 j = FAIL;
24406 if (sourcing_name != NULL)
24407 {
24408 scriptname = autoload_name(name);
24409 if (scriptname != NULL)
24410 {
24411 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024412 plen = (int)STRLEN(p);
24413 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024414 if (slen > plen && fnamecmp(p,
24415 sourcing_name + slen - plen) == 0)
24416 j = OK;
24417 vim_free(scriptname);
24418 }
24419 }
24420 if (j == FAIL)
24421 {
24422 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24423 goto erret;
24424 }
24425 }
24426
24427 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428 if (fp == NULL)
24429 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024430
24431 if (fudi.fd_dict != NULL)
24432 {
24433 if (fudi.fd_di == NULL)
24434 {
24435 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024436 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024437 if (fudi.fd_di == NULL)
24438 {
24439 vim_free(fp);
24440 goto erret;
24441 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024442 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24443 {
24444 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024445 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024446 goto erret;
24447 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024448 }
24449 else
24450 /* overwrite existing dict entry */
24451 clear_tv(&fudi.fd_di->di_tv);
24452 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024453 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024454 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024455 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024456
24457 /* behave like "dict" was used */
24458 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024459 }
24460
Bram Moolenaar071d4272004-06-13 20:20:40 +000024461 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024462 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024463 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24464 {
24465 vim_free(fp);
24466 goto erret;
24467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024468 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024469 fp->uf_args = newargs;
24470 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024471#ifdef FEAT_PROFILE
24472 fp->uf_tml_count = NULL;
24473 fp->uf_tml_total = NULL;
24474 fp->uf_tml_self = NULL;
24475 fp->uf_profiling = FALSE;
24476 if (prof_def_func())
24477 func_do_profile(fp);
24478#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024479 fp->uf_varargs = varargs;
24480 fp->uf_flags = flags;
24481 fp->uf_calls = 0;
24482 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024483 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484
24485erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024486 ga_clear_strings(&newargs);
24487 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024488ret_free:
24489 vim_free(skip_until);
24490 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024491 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024492 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024493 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024494}
24495
24496/*
24497 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024498 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024499 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024500 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024501 * TFN_INT: internal function name OK
24502 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024503 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504 * Advances "pp" to just after the function name (if no error).
24505 */
24506 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024507trans_function_name(
24508 char_u **pp,
24509 int skip, /* only find the end, don't evaluate */
24510 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024511 funcdict_T *fdp, /* return: info about dictionary used */
24512 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024514 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024515 char_u *start;
24516 char_u *end;
24517 int lead;
24518 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024519 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024520 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024521
24522 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024523 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024524 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024525
24526 /* Check for hard coded <SNR>: already translated function ID (from a user
24527 * command). */
24528 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24529 && (*pp)[2] == (int)KE_SNR)
24530 {
24531 *pp += 3;
24532 len = get_id_len(pp) + 3;
24533 return vim_strnsave(start, len);
24534 }
24535
24536 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24537 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024538 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024539 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024540 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024541
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024542 /* Note that TFN_ flags use the same values as GLV_ flags. */
24543 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024544 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024545 if (end == start)
24546 {
24547 if (!skip)
24548 EMSG(_("E129: Function name required"));
24549 goto theend;
24550 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024551 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024552 {
24553 /*
24554 * Report an invalid expression in braces, unless the expression
24555 * evaluation has been cancelled due to an aborting error, an
24556 * interrupt, or an exception.
24557 */
24558 if (!aborting())
24559 {
24560 if (end != NULL)
24561 EMSG2(_(e_invarg2), start);
24562 }
24563 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024564 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024565 goto theend;
24566 }
24567
24568 if (lv.ll_tv != NULL)
24569 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024570 if (fdp != NULL)
24571 {
24572 fdp->fd_dict = lv.ll_dict;
24573 fdp->fd_newkey = lv.ll_newkey;
24574 lv.ll_newkey = NULL;
24575 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024577 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24578 {
24579 name = vim_strsave(lv.ll_tv->vval.v_string);
24580 *pp = end;
24581 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024582 else if (lv.ll_tv->v_type == VAR_PARTIAL
24583 && lv.ll_tv->vval.v_partial != NULL)
24584 {
24585 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24586 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024587 if (partial != NULL)
24588 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024589 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024590 else
24591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024592 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24593 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024594 EMSG(_(e_funcref));
24595 else
24596 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024597 name = NULL;
24598 }
24599 goto theend;
24600 }
24601
24602 if (lv.ll_name == NULL)
24603 {
24604 /* Error found, but continue after the function name. */
24605 *pp = end;
24606 goto theend;
24607 }
24608
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024609 /* Check if the name is a Funcref. If so, use the value. */
24610 if (lv.ll_exp_name != NULL)
24611 {
24612 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024613 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024614 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024615 if (name == lv.ll_exp_name)
24616 name = NULL;
24617 }
24618 else
24619 {
24620 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024621 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024622 if (name == *pp)
24623 name = NULL;
24624 }
24625 if (name != NULL)
24626 {
24627 name = vim_strsave(name);
24628 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024629 if (STRNCMP(name, "<SNR>", 5) == 0)
24630 {
24631 /* Change "<SNR>" to the byte sequence. */
24632 name[0] = K_SPECIAL;
24633 name[1] = KS_EXTRA;
24634 name[2] = (int)KE_SNR;
24635 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24636 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024637 goto theend;
24638 }
24639
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024640 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024641 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024642 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024643 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24644 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24645 {
24646 /* When there was "s:" already or the name expanded to get a
24647 * leading "s:" then remove it. */
24648 lv.ll_name += 2;
24649 len -= 2;
24650 lead = 2;
24651 }
24652 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024653 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024654 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024655 /* skip over "s:" and "g:" */
24656 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024657 lv.ll_name += 2;
24658 len = (int)(end - lv.ll_name);
24659 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024660
24661 /*
24662 * Copy the function name to allocated memory.
24663 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24664 * Accept <SNR>123_name() outside a script.
24665 */
24666 if (skip)
24667 lead = 0; /* do nothing */
24668 else if (lead > 0)
24669 {
24670 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024671 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24672 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024673 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024674 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024675 if (current_SID <= 0)
24676 {
24677 EMSG(_(e_usingsid));
24678 goto theend;
24679 }
24680 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24681 lead += (int)STRLEN(sid_buf);
24682 }
24683 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024684 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024685 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024686 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024687 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024688 goto theend;
24689 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024690 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024691 {
24692 char_u *cp = vim_strchr(lv.ll_name, ':');
24693
24694 if (cp != NULL && cp < end)
24695 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024696 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024697 goto theend;
24698 }
24699 }
24700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024701 name = alloc((unsigned)(len + lead + 1));
24702 if (name != NULL)
24703 {
24704 if (lead > 0)
24705 {
24706 name[0] = K_SPECIAL;
24707 name[1] = KS_EXTRA;
24708 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024709 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024710 STRCPY(name + 3, sid_buf);
24711 }
24712 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024713 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024714 }
24715 *pp = end;
24716
24717theend:
24718 clear_lval(&lv);
24719 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024720}
24721
24722/*
24723 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24724 * Return 2 if "p" starts with "s:".
24725 * Return 0 otherwise.
24726 */
24727 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024728eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024730 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24731 * the standard library function. */
24732 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24733 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024734 return 5;
24735 if (p[0] == 's' && p[1] == ':')
24736 return 2;
24737 return 0;
24738}
24739
24740/*
24741 * Return TRUE if "p" starts with "<SID>" or "s:".
24742 * Only works if eval_fname_script() returned non-zero for "p"!
24743 */
24744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024745eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024746{
24747 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24748}
24749
24750/*
24751 * List the head of the function: "name(arg1, arg2)".
24752 */
24753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024754list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024755{
24756 int j;
24757
24758 msg_start();
24759 if (indent)
24760 MSG_PUTS(" ");
24761 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024762 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024763 {
24764 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024765 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024766 }
24767 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024768 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024769 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024771 {
24772 if (j)
24773 MSG_PUTS(", ");
24774 msg_puts(FUNCARG(fp, j));
24775 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024777 {
24778 if (j)
24779 MSG_PUTS(", ");
24780 MSG_PUTS("...");
24781 }
24782 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024783 if (fp->uf_flags & FC_ABORT)
24784 MSG_PUTS(" abort");
24785 if (fp->uf_flags & FC_RANGE)
24786 MSG_PUTS(" range");
24787 if (fp->uf_flags & FC_DICT)
24788 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024789 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024790 if (p_verbose > 0)
24791 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024792}
24793
24794/*
24795 * Find a function by name, return pointer to it in ufuncs.
24796 * Return NULL for unknown function.
24797 */
24798 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024799find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024800{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024801 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024802
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024803 hi = hash_find(&func_hashtab, name);
24804 if (!HASHITEM_EMPTY(hi))
24805 return HI2UF(hi);
24806 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807}
24808
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024809#if defined(EXITFREE) || defined(PROTO)
24810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024811free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024812{
24813 hashitem_T *hi;
24814
24815 /* Need to start all over every time, because func_free() may change the
24816 * hash table. */
24817 while (func_hashtab.ht_used > 0)
24818 for (hi = func_hashtab.ht_array; ; ++hi)
24819 if (!HASHITEM_EMPTY(hi))
24820 {
24821 func_free(HI2UF(hi));
24822 break;
24823 }
24824}
24825#endif
24826
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024828translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024829{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024830 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024831 return find_internal_func(name) >= 0;
24832 return find_func(name) != NULL;
24833}
24834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024835/*
24836 * Return TRUE if a function "name" exists.
24837 */
24838 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024839function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024840{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024841 char_u *nm = name;
24842 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024843 int n = FALSE;
24844
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024845 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024846 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024847 nm = skipwhite(nm);
24848
24849 /* Only accept "funcname", "funcname ", "funcname (..." and
24850 * "funcname(...", not "funcname!...". */
24851 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024852 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024853 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024854 return n;
24855}
24856
Bram Moolenaara1544c02013-05-30 12:35:52 +020024857 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024858get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024859{
24860 char_u *nm = name;
24861 char_u *p;
24862
Bram Moolenaar65639032016-03-16 21:40:30 +010024863 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024864
24865 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024866 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024867 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024868
Bram Moolenaara1544c02013-05-30 12:35:52 +020024869 vim_free(p);
24870 return NULL;
24871}
24872
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024873/*
24874 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024875 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24876 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024877 */
24878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024879builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024880{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024881 char_u *p;
24882
24883 if (!ASCII_ISLOWER(name[0]))
24884 return FALSE;
24885 p = vim_strchr(name, AUTOLOAD_CHAR);
24886 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024887}
24888
Bram Moolenaar05159a02005-02-26 23:04:13 +000024889#if defined(FEAT_PROFILE) || defined(PROTO)
24890/*
24891 * Start profiling function "fp".
24892 */
24893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024894func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024895{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024896 int len = fp->uf_lines.ga_len;
24897
24898 if (len == 0)
24899 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024900 fp->uf_tm_count = 0;
24901 profile_zero(&fp->uf_tm_self);
24902 profile_zero(&fp->uf_tm_total);
24903 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024904 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024905 if (fp->uf_tml_total == NULL)
24906 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024907 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024908 if (fp->uf_tml_self == NULL)
24909 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024910 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024911 fp->uf_tml_idx = -1;
24912 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24913 || fp->uf_tml_self == NULL)
24914 return; /* out of memory */
24915
24916 fp->uf_profiling = TRUE;
24917}
24918
24919/*
24920 * Dump the profiling results for all functions in file "fd".
24921 */
24922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024923func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024924{
24925 hashitem_T *hi;
24926 int todo;
24927 ufunc_T *fp;
24928 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024929 ufunc_T **sorttab;
24930 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024931
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024932 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024933 if (todo == 0)
24934 return; /* nothing to dump */
24935
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024936 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024937
Bram Moolenaar05159a02005-02-26 23:04:13 +000024938 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24939 {
24940 if (!HASHITEM_EMPTY(hi))
24941 {
24942 --todo;
24943 fp = HI2UF(hi);
24944 if (fp->uf_profiling)
24945 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024946 if (sorttab != NULL)
24947 sorttab[st_len++] = fp;
24948
Bram Moolenaar05159a02005-02-26 23:04:13 +000024949 if (fp->uf_name[0] == K_SPECIAL)
24950 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24951 else
24952 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24953 if (fp->uf_tm_count == 1)
24954 fprintf(fd, "Called 1 time\n");
24955 else
24956 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24957 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24958 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24959 fprintf(fd, "\n");
24960 fprintf(fd, "count total (s) self (s)\n");
24961
24962 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24963 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024964 if (FUNCLINE(fp, i) == NULL)
24965 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024966 prof_func_line(fd, fp->uf_tml_count[i],
24967 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024968 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24969 }
24970 fprintf(fd, "\n");
24971 }
24972 }
24973 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024974
24975 if (sorttab != NULL && st_len > 0)
24976 {
24977 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24978 prof_total_cmp);
24979 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24980 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24981 prof_self_cmp);
24982 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24983 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024984
24985 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024986}
Bram Moolenaar73830342005-02-28 22:48:19 +000024987
24988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024989prof_sort_list(
24990 FILE *fd,
24991 ufunc_T **sorttab,
24992 int st_len,
24993 char *title,
24994 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024995{
24996 int i;
24997 ufunc_T *fp;
24998
24999 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25000 fprintf(fd, "count total (s) self (s) function\n");
25001 for (i = 0; i < 20 && i < st_len; ++i)
25002 {
25003 fp = sorttab[i];
25004 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25005 prefer_self);
25006 if (fp->uf_name[0] == K_SPECIAL)
25007 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25008 else
25009 fprintf(fd, " %s()\n", fp->uf_name);
25010 }
25011 fprintf(fd, "\n");
25012}
25013
25014/*
25015 * Print the count and times for one function or function line.
25016 */
25017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025018prof_func_line(
25019 FILE *fd,
25020 int count,
25021 proftime_T *total,
25022 proftime_T *self,
25023 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025024{
25025 if (count > 0)
25026 {
25027 fprintf(fd, "%5d ", count);
25028 if (prefer_self && profile_equal(total, self))
25029 fprintf(fd, " ");
25030 else
25031 fprintf(fd, "%s ", profile_msg(total));
25032 if (!prefer_self && profile_equal(total, self))
25033 fprintf(fd, " ");
25034 else
25035 fprintf(fd, "%s ", profile_msg(self));
25036 }
25037 else
25038 fprintf(fd, " ");
25039}
25040
25041/*
25042 * Compare function for total time sorting.
25043 */
25044 static int
25045#ifdef __BORLANDC__
25046_RTLENTRYF
25047#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025048prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025049{
25050 ufunc_T *p1, *p2;
25051
25052 p1 = *(ufunc_T **)s1;
25053 p2 = *(ufunc_T **)s2;
25054 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25055}
25056
25057/*
25058 * Compare function for self time sorting.
25059 */
25060 static int
25061#ifdef __BORLANDC__
25062_RTLENTRYF
25063#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025064prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025065{
25066 ufunc_T *p1, *p2;
25067
25068 p1 = *(ufunc_T **)s1;
25069 p2 = *(ufunc_T **)s2;
25070 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25071}
25072
Bram Moolenaar05159a02005-02-26 23:04:13 +000025073#endif
25074
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025075/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025076 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025077 * Return TRUE if a package was loaded.
25078 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025080script_autoload(
25081 char_u *name,
25082 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025083{
25084 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025085 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025086 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025087 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025088
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025089 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025090 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025091 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025092 return FALSE;
25093
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025094 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025095
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025096 /* Find the name in the list of previously loaded package names. Skip
25097 * "autoload/", it's always the same. */
25098 for (i = 0; i < ga_loaded.ga_len; ++i)
25099 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25100 break;
25101 if (!reload && i < ga_loaded.ga_len)
25102 ret = FALSE; /* was loaded already */
25103 else
25104 {
25105 /* Remember the name if it wasn't loaded already. */
25106 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25107 {
25108 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25109 tofree = NULL;
25110 }
25111
25112 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025113 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025114 ret = TRUE;
25115 }
25116
25117 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025118 return ret;
25119}
25120
25121/*
25122 * Return the autoload script name for a function or variable name.
25123 * Returns NULL when out of memory.
25124 */
25125 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025126autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025127{
25128 char_u *p;
25129 char_u *scriptname;
25130
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025131 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025132 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25133 if (scriptname == NULL)
25134 return FALSE;
25135 STRCPY(scriptname, "autoload/");
25136 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025137 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025138 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025139 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025140 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025141 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025142}
25143
Bram Moolenaar071d4272004-06-13 20:20:40 +000025144#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25145
25146/*
25147 * Function given to ExpandGeneric() to obtain the list of user defined
25148 * function names.
25149 */
25150 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025151get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025152{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025153 static long_u done;
25154 static hashitem_T *hi;
25155 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025156
25157 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025158 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025159 done = 0;
25160 hi = func_hashtab.ht_array;
25161 }
25162 if (done < func_hashtab.ht_used)
25163 {
25164 if (done++ > 0)
25165 ++hi;
25166 while (HASHITEM_EMPTY(hi))
25167 ++hi;
25168 fp = HI2UF(hi);
25169
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025170 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025171 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025172
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025173 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25174 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175
25176 cat_func_name(IObuff, fp);
25177 if (xp->xp_context != EXPAND_USER_FUNC)
25178 {
25179 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025180 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025181 STRCAT(IObuff, ")");
25182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 return IObuff;
25184 }
25185 return NULL;
25186}
25187
25188#endif /* FEAT_CMDL_COMPL */
25189
25190/*
25191 * Copy the function name of "fp" to buffer "buf".
25192 * "buf" must be able to hold the function name plus three bytes.
25193 * Takes care of script-local function names.
25194 */
25195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025196cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025198 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199 {
25200 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025201 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025202 }
25203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025204 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025205}
25206
25207/*
25208 * ":delfunction {name}"
25209 */
25210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025211ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025213 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025214 char_u *p;
25215 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025216 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025217
25218 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025219 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025220 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025222 {
25223 if (fudi.fd_dict != NULL && !eap->skip)
25224 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025225 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025227 if (!ends_excmd(*skipwhite(p)))
25228 {
25229 vim_free(name);
25230 EMSG(_(e_trailing));
25231 return;
25232 }
25233 eap->nextcmd = check_nextcmd(p);
25234 if (eap->nextcmd != NULL)
25235 *p = NUL;
25236
25237 if (!eap->skip)
25238 fp = find_func(name);
25239 vim_free(name);
25240
25241 if (!eap->skip)
25242 {
25243 if (fp == NULL)
25244 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025245 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246 return;
25247 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025248 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 {
25250 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25251 return;
25252 }
25253
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025254 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025256 /* Delete the dict item that refers to the function, it will
25257 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025258 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025260 else
25261 func_free(fp);
25262 }
25263}
25264
25265/*
25266 * Free a function and remove it from the list of functions.
25267 */
25268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025269func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025270{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025271 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025272
25273 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025274 ga_clear_strings(&(fp->uf_args));
25275 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025276#ifdef FEAT_PROFILE
25277 vim_free(fp->uf_tml_count);
25278 vim_free(fp->uf_tml_total);
25279 vim_free(fp->uf_tml_self);
25280#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025281
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025282 /* remove the function from the function hashtable */
25283 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25284 if (HASHITEM_EMPTY(hi))
25285 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025286 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025287 hash_remove(&func_hashtab, hi);
25288
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025289 vim_free(fp);
25290}
25291
25292/*
25293 * Unreference a Function: decrement the reference count and free it when it
25294 * becomes zero. Only for numbered functions.
25295 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025297func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025298{
25299 ufunc_T *fp;
25300
25301 if (name != NULL && isdigit(*name))
25302 {
25303 fp = find_func(name);
25304 if (fp == NULL)
25305 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025306 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025307 {
25308 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025309 * when "uf_calls" becomes zero. */
25310 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025311 func_free(fp);
25312 }
25313 }
25314}
25315
25316/*
25317 * Count a reference to a Function.
25318 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025320func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025321{
25322 ufunc_T *fp;
25323
25324 if (name != NULL && isdigit(*name))
25325 {
25326 fp = find_func(name);
25327 if (fp == NULL)
25328 EMSG2(_(e_intern2), "func_ref()");
25329 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025330 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 }
25332}
25333
25334/*
25335 * Call a user function.
25336 */
25337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025338call_user_func(
25339 ufunc_T *fp, /* pointer to function */
25340 int argcount, /* nr of args */
25341 typval_T *argvars, /* arguments */
25342 typval_T *rettv, /* return value */
25343 linenr_T firstline, /* first line of range */
25344 linenr_T lastline, /* last line of range */
25345 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346{
Bram Moolenaar33570922005-01-25 22:26:29 +000025347 char_u *save_sourcing_name;
25348 linenr_T save_sourcing_lnum;
25349 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025350 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025351 int save_did_emsg;
25352 static int depth = 0;
25353 dictitem_T *v;
25354 int fixvar_idx = 0; /* index in fixvar[] */
25355 int i;
25356 int ai;
25357 char_u numbuf[NUMBUFLEN];
25358 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025359 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360#ifdef FEAT_PROFILE
25361 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025362 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364
25365 /* If depth of calling is getting too high, don't execute the function */
25366 if (depth >= p_mfd)
25367 {
25368 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025369 rettv->v_type = VAR_NUMBER;
25370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371 return;
25372 }
25373 ++depth;
25374
25375 line_breakcheck(); /* check for CTRL-C hit */
25376
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025377 fc = (funccall_T *)alloc(sizeof(funccall_T));
25378 fc->caller = current_funccal;
25379 current_funccal = fc;
25380 fc->func = fp;
25381 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025382 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025383 fc->linenr = 0;
25384 fc->returned = FALSE;
25385 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025387 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25388 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389
Bram Moolenaar33570922005-01-25 22:26:29 +000025390 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025391 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025392 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25393 * each argument variable and saves a lot of time.
25394 */
25395 /*
25396 * Init l: variables.
25397 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025398 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025399 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025400 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025401 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25402 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025403 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025404 name = v->di_key;
25405 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025406 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025407 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025408 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025409 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025410 v->di_tv.vval.v_dict = selfdict;
25411 ++selfdict->dv_refcount;
25412 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025413
Bram Moolenaar33570922005-01-25 22:26:29 +000025414 /*
25415 * Init a: variables.
25416 * Set a:0 to "argcount".
25417 * Set a:000 to a list with room for the "..." arguments.
25418 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025419 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025420 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025421 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025422 /* Use "name" to avoid a warning from some compiler that checks the
25423 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025424 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025425 name = v->di_key;
25426 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025427 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025428 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025429 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025430 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025431 v->di_tv.vval.v_list = &fc->l_varlist;
25432 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25433 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25434 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025435
25436 /*
25437 * Set a:firstline to "firstline" and a:lastline to "lastline".
25438 * Set a:name to named arguments.
25439 * Set a:N to the "..." arguments.
25440 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025441 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025442 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025443 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025444 (varnumber_T)lastline);
25445 for (i = 0; i < argcount; ++i)
25446 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025447 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025448 if (ai < 0)
25449 /* named argument a:name */
25450 name = FUNCARG(fp, i);
25451 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025452 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025453 /* "..." argument a:1, a:2, etc. */
25454 sprintf((char *)numbuf, "%d", ai + 1);
25455 name = numbuf;
25456 }
25457 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25458 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025459 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025460 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25461 }
25462 else
25463 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025464 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25465 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025466 if (v == NULL)
25467 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025468 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025469 }
25470 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025471 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025472
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025473 /* Note: the values are copied directly to avoid alloc/free.
25474 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025475 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025476 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025477
25478 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25479 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025480 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25481 fc->l_listitems[ai].li_tv = argvars[i];
25482 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025483 }
25484 }
25485
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 /* Don't redraw while executing the function. */
25487 ++RedrawingDisabled;
25488 save_sourcing_name = sourcing_name;
25489 save_sourcing_lnum = sourcing_lnum;
25490 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025491 /* need space for function name + ("function " + 3) or "[number]" */
25492 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25493 + STRLEN(fp->uf_name) + 20;
25494 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025495 if (sourcing_name != NULL)
25496 {
25497 if (save_sourcing_name != NULL
25498 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025499 sprintf((char *)sourcing_name, "%s[%d]..",
25500 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 else
25502 STRCPY(sourcing_name, "function ");
25503 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25504
25505 if (p_verbose >= 12)
25506 {
25507 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025508 verbose_enter_scroll();
25509
Bram Moolenaar555b2802005-05-19 21:08:39 +000025510 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511 if (p_verbose >= 14)
25512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025514 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025515 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025516 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025517
25518 msg_puts((char_u *)"(");
25519 for (i = 0; i < argcount; ++i)
25520 {
25521 if (i > 0)
25522 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025523 if (argvars[i].v_type == VAR_NUMBER)
25524 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025525 else
25526 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025527 /* Do not want errors such as E724 here. */
25528 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025529 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025530 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025531 if (s != NULL)
25532 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025533 if (vim_strsize(s) > MSG_BUF_CLEN)
25534 {
25535 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25536 s = buf;
25537 }
25538 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025539 vim_free(tofree);
25540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541 }
25542 }
25543 msg_puts((char_u *)")");
25544 }
25545 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025546
25547 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025548 --no_wait_return;
25549 }
25550 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025551#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025552 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025553 {
25554 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25555 func_do_profile(fp);
25556 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025557 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025558 {
25559 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025560 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025561 profile_zero(&fp->uf_tm_children);
25562 }
25563 script_prof_save(&wait_start);
25564 }
25565#endif
25566
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025568 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569 save_did_emsg = did_emsg;
25570 did_emsg = FALSE;
25571
25572 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025573 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25575
25576 --RedrawingDisabled;
25577
25578 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025579 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025581 clear_tv(rettv);
25582 rettv->v_type = VAR_NUMBER;
25583 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 }
25585
Bram Moolenaar05159a02005-02-26 23:04:13 +000025586#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025587 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025588 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025589 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025590 profile_end(&call_start);
25591 profile_sub_wait(&wait_start, &call_start);
25592 profile_add(&fp->uf_tm_total, &call_start);
25593 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025594 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025595 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25597 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025598 }
25599 }
25600#endif
25601
Bram Moolenaar071d4272004-06-13 20:20:40 +000025602 /* when being verbose, mention the return value */
25603 if (p_verbose >= 12)
25604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025606 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025609 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025610 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025611 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025612 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025613 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025615 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025616 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025617 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025618 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025619
Bram Moolenaar555b2802005-05-19 21:08:39 +000025620 /* The value may be very long. Skip the middle part, so that we
25621 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025622 * truncate it at the end. Don't want errors such as E724 here. */
25623 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025624 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025625 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025626 if (s != NULL)
25627 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025628 if (vim_strsize(s) > MSG_BUF_CLEN)
25629 {
25630 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25631 s = buf;
25632 }
25633 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025634 vim_free(tofree);
25635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025636 }
25637 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025638
25639 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640 --no_wait_return;
25641 }
25642
25643 vim_free(sourcing_name);
25644 sourcing_name = save_sourcing_name;
25645 sourcing_lnum = save_sourcing_lnum;
25646 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025647#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025648 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025649 script_prof_restore(&wait_start);
25650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025651
25652 if (p_verbose >= 12 && sourcing_name != NULL)
25653 {
25654 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025655 verbose_enter_scroll();
25656
Bram Moolenaar555b2802005-05-19 21:08:39 +000025657 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025658 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025659
25660 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025661 --no_wait_return;
25662 }
25663
25664 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025665 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025666 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025667
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025668 /* If the a:000 list and the l: and a: dicts are not referenced we can
25669 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025670 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25671 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25672 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25673 {
25674 free_funccal(fc, FALSE);
25675 }
25676 else
25677 {
25678 hashitem_T *hi;
25679 listitem_T *li;
25680 int todo;
25681
25682 /* "fc" is still in use. This can happen when returning "a:000" or
25683 * assigning "l:" to a global variable.
25684 * Link "fc" in the list for garbage collection later. */
25685 fc->caller = previous_funccal;
25686 previous_funccal = fc;
25687
25688 /* Make a copy of the a: variables, since we didn't do that above. */
25689 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25690 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25691 {
25692 if (!HASHITEM_EMPTY(hi))
25693 {
25694 --todo;
25695 v = HI2DI(hi);
25696 copy_tv(&v->di_tv, &v->di_tv);
25697 }
25698 }
25699
25700 /* Make a copy of the a:000 items, since we didn't do that above. */
25701 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25702 copy_tv(&li->li_tv, &li->li_tv);
25703 }
25704}
25705
25706/*
25707 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025708 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025709 */
25710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025711can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025712{
25713 return (fc->l_varlist.lv_copyID != copyID
25714 && fc->l_vars.dv_copyID != copyID
25715 && fc->l_avars.dv_copyID != copyID);
25716}
25717
25718/*
25719 * Free "fc" and what it contains.
25720 */
25721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025722free_funccal(
25723 funccall_T *fc,
25724 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025725{
25726 listitem_T *li;
25727
25728 /* The a: variables typevals may not have been allocated, only free the
25729 * allocated variables. */
25730 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25731
25732 /* free all l: variables */
25733 vars_clear(&fc->l_vars.dv_hashtab);
25734
25735 /* Free the a:000 variables if they were allocated. */
25736 if (free_val)
25737 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25738 clear_tv(&li->li_tv);
25739
25740 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025741}
25742
25743/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025744 * Add a number variable "name" to dict "dp" with value "nr".
25745 */
25746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025747add_nr_var(
25748 dict_T *dp,
25749 dictitem_T *v,
25750 char *name,
25751 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025752{
25753 STRCPY(v->di_key, name);
25754 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25755 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25756 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025757 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025758 v->di_tv.vval.v_number = nr;
25759}
25760
25761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 * ":return [expr]"
25763 */
25764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025765ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766{
25767 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025768 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 int returning = FALSE;
25770
25771 if (current_funccal == NULL)
25772 {
25773 EMSG(_("E133: :return not inside a function"));
25774 return;
25775 }
25776
25777 if (eap->skip)
25778 ++emsg_skip;
25779
25780 eap->nextcmd = NULL;
25781 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025782 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 {
25784 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025785 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025787 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 }
25789 /* It's safer to return also on error. */
25790 else if (!eap->skip)
25791 {
25792 /*
25793 * Return unless the expression evaluation has been cancelled due to an
25794 * aborting error, an interrupt, or an exception.
25795 */
25796 if (!aborting())
25797 returning = do_return(eap, FALSE, TRUE, NULL);
25798 }
25799
25800 /* When skipping or the return gets pending, advance to the next command
25801 * in this line (!returning). Otherwise, ignore the rest of the line.
25802 * Following lines will be ignored by get_func_line(). */
25803 if (returning)
25804 eap->nextcmd = NULL;
25805 else if (eap->nextcmd == NULL) /* no argument */
25806 eap->nextcmd = check_nextcmd(arg);
25807
25808 if (eap->skip)
25809 --emsg_skip;
25810}
25811
25812/*
25813 * Return from a function. Possibly makes the return pending. Also called
25814 * for a pending return at the ":endtry" or after returning from an extra
25815 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025816 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025817 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818 * FALSE when the return gets pending.
25819 */
25820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025821do_return(
25822 exarg_T *eap,
25823 int reanimate,
25824 int is_cmd,
25825 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826{
25827 int idx;
25828 struct condstack *cstack = eap->cstack;
25829
25830 if (reanimate)
25831 /* Undo the return. */
25832 current_funccal->returned = FALSE;
25833
25834 /*
25835 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25836 * not in its finally clause (which then is to be executed next) is found.
25837 * In this case, make the ":return" pending for execution at the ":endtry".
25838 * Otherwise, return normally.
25839 */
25840 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25841 if (idx >= 0)
25842 {
25843 cstack->cs_pending[idx] = CSTP_RETURN;
25844
25845 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025846 /* A pending return again gets pending. "rettv" points to an
25847 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025849 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025850 else
25851 {
25852 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025853 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025854 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025855 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025856
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025857 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 {
25859 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025860 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025861 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862 else
25863 EMSG(_(e_outofmem));
25864 }
25865 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025866 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025867
25868 if (reanimate)
25869 {
25870 /* The pending return value could be overwritten by a ":return"
25871 * without argument in a finally clause; reset the default
25872 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025873 current_funccal->rettv->v_type = VAR_NUMBER;
25874 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025875 }
25876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025877 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 }
25879 else
25880 {
25881 current_funccal->returned = TRUE;
25882
25883 /* If the return is carried out now, store the return value. For
25884 * a return immediately after reanimation, the value is already
25885 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025886 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025888 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025889 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025891 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 }
25893 }
25894
25895 return idx < 0;
25896}
25897
25898/*
25899 * Free the variable with a pending return value.
25900 */
25901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025902discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903{
Bram Moolenaar33570922005-01-25 22:26:29 +000025904 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025905}
25906
25907/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025908 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025909 * is an allocated string. Used by report_pending() for verbose messages.
25910 */
25911 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025912get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025914 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025915 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025916 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025917
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025918 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025919 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025920 if (s == NULL)
25921 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025922
25923 STRCPY(IObuff, ":return ");
25924 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25925 if (STRLEN(s) + 8 >= IOSIZE)
25926 STRCPY(IObuff + IOSIZE - 4, "...");
25927 vim_free(tofree);
25928 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025929}
25930
25931/*
25932 * Get next function line.
25933 * Called by do_cmdline() to get the next line.
25934 * Returns allocated string, or NULL for end of function.
25935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025937get_func_line(
25938 int c UNUSED,
25939 void *cookie,
25940 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941{
Bram Moolenaar33570922005-01-25 22:26:29 +000025942 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025943 ufunc_T *fp = fcp->func;
25944 char_u *retval;
25945 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946
25947 /* If breakpoints have been added/deleted need to check for it. */
25948 if (fcp->dbg_tick != debug_tick)
25949 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025950 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951 sourcing_lnum);
25952 fcp->dbg_tick = debug_tick;
25953 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025954#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025955 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025956 func_line_end(cookie);
25957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958
Bram Moolenaar05159a02005-02-26 23:04:13 +000025959 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025960 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25961 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025962 retval = NULL;
25963 else
25964 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025965 /* Skip NULL lines (continuation lines). */
25966 while (fcp->linenr < gap->ga_len
25967 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25968 ++fcp->linenr;
25969 if (fcp->linenr >= gap->ga_len)
25970 retval = NULL;
25971 else
25972 {
25973 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25974 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025975#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025976 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025977 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025978#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 }
25981
25982 /* Did we encounter a breakpoint? */
25983 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25984 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025985 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025987 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988 sourcing_lnum);
25989 fcp->dbg_tick = debug_tick;
25990 }
25991
25992 return retval;
25993}
25994
Bram Moolenaar05159a02005-02-26 23:04:13 +000025995#if defined(FEAT_PROFILE) || defined(PROTO)
25996/*
25997 * Called when starting to read a function line.
25998 * "sourcing_lnum" must be correct!
25999 * When skipping lines it may not actually be executed, but we won't find out
26000 * until later and we need to store the time now.
26001 */
26002 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026003func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026004{
26005 funccall_T *fcp = (funccall_T *)cookie;
26006 ufunc_T *fp = fcp->func;
26007
26008 if (fp->uf_profiling && sourcing_lnum >= 1
26009 && sourcing_lnum <= fp->uf_lines.ga_len)
26010 {
26011 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026012 /* Skip continuation lines. */
26013 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26014 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026015 fp->uf_tml_execed = FALSE;
26016 profile_start(&fp->uf_tml_start);
26017 profile_zero(&fp->uf_tml_children);
26018 profile_get_wait(&fp->uf_tml_wait);
26019 }
26020}
26021
26022/*
26023 * Called when actually executing a function line.
26024 */
26025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026026func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026027{
26028 funccall_T *fcp = (funccall_T *)cookie;
26029 ufunc_T *fp = fcp->func;
26030
26031 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26032 fp->uf_tml_execed = TRUE;
26033}
26034
26035/*
26036 * Called when done with a function line.
26037 */
26038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026039func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026040{
26041 funccall_T *fcp = (funccall_T *)cookie;
26042 ufunc_T *fp = fcp->func;
26043
26044 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26045 {
26046 if (fp->uf_tml_execed)
26047 {
26048 ++fp->uf_tml_count[fp->uf_tml_idx];
26049 profile_end(&fp->uf_tml_start);
26050 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026051 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026052 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26053 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026054 }
26055 fp->uf_tml_idx = -1;
26056 }
26057}
26058#endif
26059
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060/*
26061 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026062 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063 */
26064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026065func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026066{
Bram Moolenaar33570922005-01-25 22:26:29 +000026067 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068
26069 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26070 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026071 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072 || fcp->returned);
26073}
26074
26075/*
26076 * return TRUE if cookie indicates a function which "abort"s on errors.
26077 */
26078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026079func_has_abort(
26080 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026082 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083}
26084
26085#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26086typedef enum
26087{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026088 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26089 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26090 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091} var_flavour_T;
26092
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026093static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094
26095 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026096var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097{
26098 char_u *p = varname;
26099
26100 if (ASCII_ISUPPER(*p))
26101 {
26102 while (*(++p))
26103 if (ASCII_ISLOWER(*p))
26104 return VAR_FLAVOUR_SESSION;
26105 return VAR_FLAVOUR_VIMINFO;
26106 }
26107 else
26108 return VAR_FLAVOUR_DEFAULT;
26109}
26110#endif
26111
26112#if defined(FEAT_VIMINFO) || defined(PROTO)
26113/*
26114 * Restore global vars that start with a capital from the viminfo file
26115 */
26116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026117read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118{
26119 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026120 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026121 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026122 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026123
26124 if (!writing && (find_viminfo_parameter('!') != NULL))
26125 {
26126 tab = vim_strchr(virp->vir_line + 1, '\t');
26127 if (tab != NULL)
26128 {
26129 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026130 switch (*tab)
26131 {
26132 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026133#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026134 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026135#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026136 case 'D': type = VAR_DICT; break;
26137 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026138 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140
26141 tab = vim_strchr(tab, '\t');
26142 if (tab != NULL)
26143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026144 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026145 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026146 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026148#ifdef FEAT_FLOAT
26149 else if (type == VAR_FLOAT)
26150 (void)string2float(tab + 1, &tv.vval.v_float);
26151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026153 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026154 if (type == VAR_DICT || type == VAR_LIST)
26155 {
26156 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26157
26158 if (etv == NULL)
26159 /* Failed to parse back the dict or list, use it as a
26160 * string. */
26161 tv.v_type = VAR_STRING;
26162 else
26163 {
26164 vim_free(tv.vval.v_string);
26165 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026166 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026167 }
26168 }
26169
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026170 /* when in a function use global variables */
26171 save_funccal = current_funccal;
26172 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026173 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026174 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026175
26176 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026177 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026178 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180 }
26181 }
26182 }
26183
26184 return viminfo_readline(virp);
26185}
26186
26187/*
26188 * Write global vars that start with a capital to the viminfo file
26189 */
26190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026191write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192{
Bram Moolenaar33570922005-01-25 22:26:29 +000026193 hashitem_T *hi;
26194 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026195 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026196 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026197 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026198 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026199 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200
26201 if (find_viminfo_parameter('!') == NULL)
26202 return;
26203
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026204 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026205
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026206 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026207 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026209 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026211 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026212 this_var = HI2DI(hi);
26213 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026214 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026215 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026216 {
26217 case VAR_STRING: s = "STR"; break;
26218 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026219 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026220 case VAR_DICT: s = "DIC"; break;
26221 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026222 case VAR_SPECIAL: s = "XPL"; break;
26223
26224 case VAR_UNKNOWN:
26225 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026226 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026227 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026228 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026229 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026230 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026231 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026232 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026233 if (p != NULL)
26234 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026235 vim_free(tofree);
26236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026237 }
26238 }
26239}
26240#endif
26241
26242#if defined(FEAT_SESSION) || defined(PROTO)
26243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026244store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245{
Bram Moolenaar33570922005-01-25 22:26:29 +000026246 hashitem_T *hi;
26247 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026248 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 char_u *p, *t;
26250
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026251 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026252 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026253 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026254 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026255 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026256 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026257 this_var = HI2DI(hi);
26258 if ((this_var->di_tv.v_type == VAR_NUMBER
26259 || this_var->di_tv.v_type == VAR_STRING)
26260 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026261 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026262 /* Escape special characters with a backslash. Turn a LF and
26263 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026264 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026265 (char_u *)"\\\"\n\r");
26266 if (p == NULL) /* out of memory */
26267 break;
26268 for (t = p; *t != NUL; ++t)
26269 if (*t == '\n')
26270 *t = 'n';
26271 else if (*t == '\r')
26272 *t = 'r';
26273 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026274 this_var->di_key,
26275 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26276 : ' ',
26277 p,
26278 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26279 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026280 || put_eol(fd) == FAIL)
26281 {
26282 vim_free(p);
26283 return FAIL;
26284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026285 vim_free(p);
26286 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026287#ifdef FEAT_FLOAT
26288 else if (this_var->di_tv.v_type == VAR_FLOAT
26289 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26290 {
26291 float_T f = this_var->di_tv.vval.v_float;
26292 int sign = ' ';
26293
26294 if (f < 0)
26295 {
26296 f = -f;
26297 sign = '-';
26298 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026299 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026300 this_var->di_key, sign, f) < 0)
26301 || put_eol(fd) == FAIL)
26302 return FAIL;
26303 }
26304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 }
26306 }
26307 return OK;
26308}
26309#endif
26310
Bram Moolenaar661b1822005-07-28 22:36:45 +000026311/*
26312 * Display script name where an item was last set.
26313 * Should only be invoked when 'verbose' is non-zero.
26314 */
26315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026316last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026317{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026318 char_u *p;
26319
Bram Moolenaar661b1822005-07-28 22:36:45 +000026320 if (scriptID != 0)
26321 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026322 p = home_replace_save(NULL, get_scriptname(scriptID));
26323 if (p != NULL)
26324 {
26325 verbose_enter();
26326 MSG_PUTS(_("\n\tLast set from "));
26327 MSG_PUTS(p);
26328 vim_free(p);
26329 verbose_leave();
26330 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026331 }
26332}
26333
Bram Moolenaard812df62008-11-09 12:46:09 +000026334/*
26335 * List v:oldfiles in a nice way.
26336 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026337 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026338ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026339{
26340 list_T *l = vimvars[VV_OLDFILES].vv_list;
26341 listitem_T *li;
26342 int nr = 0;
26343
26344 if (l == NULL)
26345 msg((char_u *)_("No old files"));
26346 else
26347 {
26348 msg_start();
26349 msg_scroll = TRUE;
26350 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26351 {
26352 msg_outnum((long)++nr);
26353 MSG_PUTS(": ");
26354 msg_outtrans(get_tv_string(&li->li_tv));
26355 msg_putchar('\n');
26356 out_flush(); /* output one line at a time */
26357 ui_breakcheck();
26358 }
26359 /* Assume "got_int" was set to truncate the listing. */
26360 got_int = FALSE;
26361
26362#ifdef FEAT_BROWSE_CMD
26363 if (cmdmod.browse)
26364 {
26365 quit_more = FALSE;
26366 nr = prompt_for_number(FALSE);
26367 msg_starthere();
26368 if (nr > 0)
26369 {
26370 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26371 (long)nr);
26372
26373 if (p != NULL)
26374 {
26375 p = expand_env_save(p);
26376 eap->arg = p;
26377 eap->cmdidx = CMD_edit;
26378 cmdmod.browse = FALSE;
26379 do_exedit(eap, NULL);
26380 vim_free(p);
26381 }
26382 }
26383 }
26384#endif
26385 }
26386}
26387
Bram Moolenaar53744302015-07-17 17:38:22 +020026388/* reset v:option_new, v:option_old and v:option_type */
26389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026390reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026391{
26392 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26393 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26394 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26395}
26396
26397
Bram Moolenaar071d4272004-06-13 20:20:40 +000026398#endif /* FEAT_EVAL */
26399
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026401#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026402
26403#ifdef WIN3264
26404/*
26405 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26406 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026407static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26408static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26409static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026410
26411/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026412 * Get the short path (8.3) for the filename in "fnamep".
26413 * Only works for a valid file name.
26414 * When the path gets longer "fnamep" is changed and the allocated buffer
26415 * is put in "bufp".
26416 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26417 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 */
26419 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026420get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026421{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026422 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 char_u *newbuf;
26424
26425 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026426 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 if (l > len - 1)
26428 {
26429 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026430 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026431 newbuf = vim_strnsave(*fnamep, l);
26432 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026433 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434
26435 vim_free(*bufp);
26436 *fnamep = *bufp = newbuf;
26437
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026438 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026439 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026440 }
26441
26442 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026443 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026444}
26445
26446/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026447 * Get the short path (8.3) for the filename in "fname". The converted
26448 * path is returned in "bufp".
26449 *
26450 * Some of the directories specified in "fname" may not exist. This function
26451 * will shorten the existing directories at the beginning of the path and then
26452 * append the remaining non-existing path.
26453 *
26454 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026455 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026456 * bufp - Pointer to an allocated buffer for the filename.
26457 * fnamelen - Length of the filename pointed to by fname
26458 *
26459 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026460 */
26461 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026462shortpath_for_invalid_fname(
26463 char_u **fname,
26464 char_u **bufp,
26465 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026466{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026467 char_u *short_fname, *save_fname, *pbuf_unused;
26468 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026470 int old_len, len;
26471 int new_len, sfx_len;
26472 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026473
26474 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026475 old_len = *fnamelen;
26476 save_fname = vim_strnsave(*fname, old_len);
26477 pbuf_unused = NULL;
26478 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026480 endp = save_fname + old_len - 1; /* Find the end of the copy */
26481 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026482
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026483 /*
26484 * Try shortening the supplied path till it succeeds by removing one
26485 * directory at a time from the tail of the path.
26486 */
26487 len = 0;
26488 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026489 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026490 /* go back one path-separator */
26491 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26492 --endp;
26493 if (endp <= save_fname)
26494 break; /* processed the complete path */
26495
26496 /*
26497 * Replace the path separator with a NUL and try to shorten the
26498 * resulting path.
26499 */
26500 ch = *endp;
26501 *endp = 0;
26502 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026503 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026504 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26505 {
26506 retval = FAIL;
26507 goto theend;
26508 }
26509 *endp = ch; /* preserve the string */
26510
26511 if (len > 0)
26512 break; /* successfully shortened the path */
26513
26514 /* failed to shorten the path. Skip the path separator */
26515 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026516 }
26517
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026518 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026519 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026520 /*
26521 * Succeeded in shortening the path. Now concatenate the shortened
26522 * path with the remaining path at the tail.
26523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026525 /* Compute the length of the new path. */
26526 sfx_len = (int)(save_endp - endp) + 1;
26527 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026528
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026529 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026531 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026533 /* There is not enough space in the currently allocated string,
26534 * copy it to a buffer big enough. */
26535 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026537 {
26538 retval = FAIL;
26539 goto theend;
26540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026541 }
26542 else
26543 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026544 /* Transfer short_fname to the main buffer (it's big enough),
26545 * unless get_short_pathname() did its work in-place. */
26546 *fname = *bufp = save_fname;
26547 if (short_fname != save_fname)
26548 vim_strncpy(save_fname, short_fname, len);
26549 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026550 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026551
26552 /* concat the not-shortened part of the path */
26553 vim_strncpy(*fname + len, endp, sfx_len);
26554 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026555 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026556
26557theend:
26558 vim_free(pbuf_unused);
26559 vim_free(save_fname);
26560
26561 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562}
26563
26564/*
26565 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026566 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 */
26568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026569shortpath_for_partial(
26570 char_u **fnamep,
26571 char_u **bufp,
26572 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026573{
26574 int sepcount, len, tflen;
26575 char_u *p;
26576 char_u *pbuf, *tfname;
26577 int hasTilde;
26578
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026579 /* Count up the path separators from the RHS.. so we know which part
26580 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026581 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026582 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026583 if (vim_ispathsep(*p))
26584 ++sepcount;
26585
26586 /* Need full path first (use expand_env() to remove a "~/") */
26587 hasTilde = (**fnamep == '~');
26588 if (hasTilde)
26589 pbuf = tfname = expand_env_save(*fnamep);
26590 else
26591 pbuf = tfname = FullName_save(*fnamep, FALSE);
26592
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026593 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026594
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026595 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26596 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026597
26598 if (len == 0)
26599 {
26600 /* Don't have a valid filename, so shorten the rest of the
26601 * path if we can. This CAN give us invalid 8.3 filenames, but
26602 * there's not a lot of point in guessing what it might be.
26603 */
26604 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026605 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26606 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607 }
26608
26609 /* Count the paths backward to find the beginning of the desired string. */
26610 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026611 {
26612#ifdef FEAT_MBYTE
26613 if (has_mbyte)
26614 p -= mb_head_off(tfname, p);
26615#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026616 if (vim_ispathsep(*p))
26617 {
26618 if (sepcount == 0 || (hasTilde && sepcount == 1))
26619 break;
26620 else
26621 sepcount --;
26622 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026624 if (hasTilde)
26625 {
26626 --p;
26627 if (p >= tfname)
26628 *p = '~';
26629 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026630 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026631 }
26632 else
26633 ++p;
26634
26635 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26636 vim_free(*bufp);
26637 *fnamelen = (int)STRLEN(p);
26638 *bufp = pbuf;
26639 *fnamep = p;
26640
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026641 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026642}
26643#endif /* WIN3264 */
26644
26645/*
26646 * Adjust a filename, according to a string of modifiers.
26647 * *fnamep must be NUL terminated when called. When returning, the length is
26648 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026649 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026650 * When there is an error, *fnamep is set to NULL.
26651 */
26652 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026653modify_fname(
26654 char_u *src, /* string with modifiers */
26655 int *usedlen, /* characters after src that are used */
26656 char_u **fnamep, /* file name so far */
26657 char_u **bufp, /* buffer for allocated file name or NULL */
26658 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026659{
26660 int valid = 0;
26661 char_u *tail;
26662 char_u *s, *p, *pbuf;
26663 char_u dirname[MAXPATHL];
26664 int c;
26665 int has_fullname = 0;
26666#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026667 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026668 int has_shortname = 0;
26669#endif
26670
26671repeat:
26672 /* ":p" - full path/file_name */
26673 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26674 {
26675 has_fullname = 1;
26676
26677 valid |= VALID_PATH;
26678 *usedlen += 2;
26679
26680 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26681 if ((*fnamep)[0] == '~'
26682#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26683 && ((*fnamep)[1] == '/'
26684# ifdef BACKSLASH_IN_FILENAME
26685 || (*fnamep)[1] == '\\'
26686# endif
26687 || (*fnamep)[1] == NUL)
26688
26689#endif
26690 )
26691 {
26692 *fnamep = expand_env_save(*fnamep);
26693 vim_free(*bufp); /* free any allocated file name */
26694 *bufp = *fnamep;
26695 if (*fnamep == NULL)
26696 return -1;
26697 }
26698
26699 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026700 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026701 {
26702 if (vim_ispathsep(*p)
26703 && p[1] == '.'
26704 && (p[2] == NUL
26705 || vim_ispathsep(p[2])
26706 || (p[2] == '.'
26707 && (p[3] == NUL || vim_ispathsep(p[3])))))
26708 break;
26709 }
26710
26711 /* FullName_save() is slow, don't use it when not needed. */
26712 if (*p != NUL || !vim_isAbsName(*fnamep))
26713 {
26714 *fnamep = FullName_save(*fnamep, *p != NUL);
26715 vim_free(*bufp); /* free any allocated file name */
26716 *bufp = *fnamep;
26717 if (*fnamep == NULL)
26718 return -1;
26719 }
26720
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026721#ifdef WIN3264
26722# if _WIN32_WINNT >= 0x0500
26723 if (vim_strchr(*fnamep, '~') != NULL)
26724 {
26725 /* Expand 8.3 filename to full path. Needed to make sure the same
26726 * file does not have two different names.
26727 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26728 p = alloc(_MAX_PATH + 1);
26729 if (p != NULL)
26730 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026731 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026732 {
26733 vim_free(*bufp);
26734 *bufp = *fnamep = p;
26735 }
26736 else
26737 vim_free(p);
26738 }
26739 }
26740# endif
26741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026742 /* Append a path separator to a directory. */
26743 if (mch_isdir(*fnamep))
26744 {
26745 /* Make room for one or two extra characters. */
26746 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26747 vim_free(*bufp); /* free any allocated file name */
26748 *bufp = *fnamep;
26749 if (*fnamep == NULL)
26750 return -1;
26751 add_pathsep(*fnamep);
26752 }
26753 }
26754
26755 /* ":." - path relative to the current directory */
26756 /* ":~" - path relative to the home directory */
26757 /* ":8" - shortname path - postponed till after */
26758 while (src[*usedlen] == ':'
26759 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26760 {
26761 *usedlen += 2;
26762 if (c == '8')
26763 {
26764#ifdef WIN3264
26765 has_shortname = 1; /* Postpone this. */
26766#endif
26767 continue;
26768 }
26769 pbuf = NULL;
26770 /* Need full path first (use expand_env() to remove a "~/") */
26771 if (!has_fullname)
26772 {
26773 if (c == '.' && **fnamep == '~')
26774 p = pbuf = expand_env_save(*fnamep);
26775 else
26776 p = pbuf = FullName_save(*fnamep, FALSE);
26777 }
26778 else
26779 p = *fnamep;
26780
26781 has_fullname = 0;
26782
26783 if (p != NULL)
26784 {
26785 if (c == '.')
26786 {
26787 mch_dirname(dirname, MAXPATHL);
26788 s = shorten_fname(p, dirname);
26789 if (s != NULL)
26790 {
26791 *fnamep = s;
26792 if (pbuf != NULL)
26793 {
26794 vim_free(*bufp); /* free any allocated file name */
26795 *bufp = pbuf;
26796 pbuf = NULL;
26797 }
26798 }
26799 }
26800 else
26801 {
26802 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26803 /* Only replace it when it starts with '~' */
26804 if (*dirname == '~')
26805 {
26806 s = vim_strsave(dirname);
26807 if (s != NULL)
26808 {
26809 *fnamep = s;
26810 vim_free(*bufp);
26811 *bufp = s;
26812 }
26813 }
26814 }
26815 vim_free(pbuf);
26816 }
26817 }
26818
26819 tail = gettail(*fnamep);
26820 *fnamelen = (int)STRLEN(*fnamep);
26821
26822 /* ":h" - head, remove "/file_name", can be repeated */
26823 /* Don't remove the first "/" or "c:\" */
26824 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26825 {
26826 valid |= VALID_HEAD;
26827 *usedlen += 2;
26828 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026829 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026830 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026831 *fnamelen = (int)(tail - *fnamep);
26832#ifdef VMS
26833 if (*fnamelen > 0)
26834 *fnamelen += 1; /* the path separator is part of the path */
26835#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026836 if (*fnamelen == 0)
26837 {
26838 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26839 p = vim_strsave((char_u *)".");
26840 if (p == NULL)
26841 return -1;
26842 vim_free(*bufp);
26843 *bufp = *fnamep = tail = p;
26844 *fnamelen = 1;
26845 }
26846 else
26847 {
26848 while (tail > s && !after_pathsep(s, tail))
26849 mb_ptr_back(*fnamep, tail);
26850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026851 }
26852
26853 /* ":8" - shortname */
26854 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26855 {
26856 *usedlen += 2;
26857#ifdef WIN3264
26858 has_shortname = 1;
26859#endif
26860 }
26861
26862#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026863 /*
26864 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026865 */
26866 if (has_shortname)
26867 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026868 /* Copy the string if it is shortened by :h and when it wasn't copied
26869 * yet, because we are going to change it in place. Avoids changing
26870 * the buffer name for "%:8". */
26871 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026872 {
26873 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026874 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026875 return -1;
26876 vim_free(*bufp);
26877 *bufp = *fnamep = p;
26878 }
26879
26880 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026881 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026882 if (!has_fullname && !vim_isAbsName(*fnamep))
26883 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026884 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026885 return -1;
26886 }
26887 else
26888 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026889 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026890
Bram Moolenaardc935552011-08-17 15:23:23 +020026891 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026892 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026893 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026894 return -1;
26895
26896 if (l == 0)
26897 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026898 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026899 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026900 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026901 return -1;
26902 }
26903 *fnamelen = l;
26904 }
26905 }
26906#endif /* WIN3264 */
26907
26908 /* ":t" - tail, just the basename */
26909 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26910 {
26911 *usedlen += 2;
26912 *fnamelen -= (int)(tail - *fnamep);
26913 *fnamep = tail;
26914 }
26915
26916 /* ":e" - extension, can be repeated */
26917 /* ":r" - root, without extension, can be repeated */
26918 while (src[*usedlen] == ':'
26919 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26920 {
26921 /* find a '.' in the tail:
26922 * - for second :e: before the current fname
26923 * - otherwise: The last '.'
26924 */
26925 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26926 s = *fnamep - 2;
26927 else
26928 s = *fnamep + *fnamelen - 1;
26929 for ( ; s > tail; --s)
26930 if (s[0] == '.')
26931 break;
26932 if (src[*usedlen + 1] == 'e') /* :e */
26933 {
26934 if (s > tail)
26935 {
26936 *fnamelen += (int)(*fnamep - (s + 1));
26937 *fnamep = s + 1;
26938#ifdef VMS
26939 /* cut version from the extension */
26940 s = *fnamep + *fnamelen - 1;
26941 for ( ; s > *fnamep; --s)
26942 if (s[0] == ';')
26943 break;
26944 if (s > *fnamep)
26945 *fnamelen = s - *fnamep;
26946#endif
26947 }
26948 else if (*fnamep <= tail)
26949 *fnamelen = 0;
26950 }
26951 else /* :r */
26952 {
26953 if (s > tail) /* remove one extension */
26954 *fnamelen = (int)(s - *fnamep);
26955 }
26956 *usedlen += 2;
26957 }
26958
26959 /* ":s?pat?foo?" - substitute */
26960 /* ":gs?pat?foo?" - global substitute */
26961 if (src[*usedlen] == ':'
26962 && (src[*usedlen + 1] == 's'
26963 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26964 {
26965 char_u *str;
26966 char_u *pat;
26967 char_u *sub;
26968 int sep;
26969 char_u *flags;
26970 int didit = FALSE;
26971
26972 flags = (char_u *)"";
26973 s = src + *usedlen + 2;
26974 if (src[*usedlen + 1] == 'g')
26975 {
26976 flags = (char_u *)"g";
26977 ++s;
26978 }
26979
26980 sep = *s++;
26981 if (sep)
26982 {
26983 /* find end of pattern */
26984 p = vim_strchr(s, sep);
26985 if (p != NULL)
26986 {
26987 pat = vim_strnsave(s, (int)(p - s));
26988 if (pat != NULL)
26989 {
26990 s = p + 1;
26991 /* find end of substitution */
26992 p = vim_strchr(s, sep);
26993 if (p != NULL)
26994 {
26995 sub = vim_strnsave(s, (int)(p - s));
26996 str = vim_strnsave(*fnamep, *fnamelen);
26997 if (sub != NULL && str != NULL)
26998 {
26999 *usedlen = (int)(p + 1 - src);
27000 s = do_string_sub(str, pat, sub, flags);
27001 if (s != NULL)
27002 {
27003 *fnamep = s;
27004 *fnamelen = (int)STRLEN(s);
27005 vim_free(*bufp);
27006 *bufp = s;
27007 didit = TRUE;
27008 }
27009 }
27010 vim_free(sub);
27011 vim_free(str);
27012 }
27013 vim_free(pat);
27014 }
27015 }
27016 /* after using ":s", repeat all the modifiers */
27017 if (didit)
27018 goto repeat;
27019 }
27020 }
27021
Bram Moolenaar26df0922014-02-23 23:39:13 +010027022 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27023 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027024 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027025 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027026 if (c != NUL)
27027 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027028 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027029 if (c != NUL)
27030 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027031 if (p == NULL)
27032 return -1;
27033 vim_free(*bufp);
27034 *bufp = *fnamep = p;
27035 *fnamelen = (int)STRLEN(p);
27036 *usedlen += 2;
27037 }
27038
Bram Moolenaar071d4272004-06-13 20:20:40 +000027039 return valid;
27040}
27041
27042/*
27043 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27044 * "flags" can be "g" to do a global substitute.
27045 * Returns an allocated string, NULL for error.
27046 */
27047 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027048do_string_sub(
27049 char_u *str,
27050 char_u *pat,
27051 char_u *sub,
27052 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027053{
27054 int sublen;
27055 regmatch_T regmatch;
27056 int i;
27057 int do_all;
27058 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027059 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027060 garray_T ga;
27061 char_u *ret;
27062 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027063 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027064
27065 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27066 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027067 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027068
27069 ga_init2(&ga, 1, 200);
27070
27071 do_all = (flags[0] == 'g');
27072
27073 regmatch.rm_ic = p_ic;
27074 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27075 if (regmatch.regprog != NULL)
27076 {
27077 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027078 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027079 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27080 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027081 /* Skip empty match except for first match. */
27082 if (regmatch.startp[0] == regmatch.endp[0])
27083 {
27084 if (zero_width == regmatch.startp[0])
27085 {
27086 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027087 i = MB_PTR2LEN(tail);
27088 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27089 (size_t)i);
27090 ga.ga_len += i;
27091 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027092 continue;
27093 }
27094 zero_width = regmatch.startp[0];
27095 }
27096
Bram Moolenaar071d4272004-06-13 20:20:40 +000027097 /*
27098 * Get some space for a temporary buffer to do the substitution
27099 * into. It will contain:
27100 * - The text up to where the match is.
27101 * - The substituted text.
27102 * - The text after the match.
27103 */
27104 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027105 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027106 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27107 {
27108 ga_clear(&ga);
27109 break;
27110 }
27111
27112 /* copy the text up to where the match is */
27113 i = (int)(regmatch.startp[0] - tail);
27114 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27115 /* add the substituted text */
27116 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27117 + ga.ga_len + i, TRUE, TRUE, FALSE);
27118 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027119 tail = regmatch.endp[0];
27120 if (*tail == NUL)
27121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027122 if (!do_all)
27123 break;
27124 }
27125
27126 if (ga.ga_data != NULL)
27127 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27128
Bram Moolenaar473de612013-06-08 18:19:48 +020027129 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027130 }
27131
27132 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27133 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027134 if (p_cpo == empty_option)
27135 p_cpo = save_cpo;
27136 else
27137 /* Darn, evaluating {sub} expression changed the value. */
27138 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027139
27140 return ret;
27141}
27142
27143#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */