blob: e7712916e8d83664d11ae1de32b6f5fca20f5e4f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200101#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200102static char *e_stringreq = N_("E928: String required");
Bram Moolenaar21decdd2016-04-22 10:00:58 +0200103#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +0000104static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000105static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
106static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
107static char *e_funcdict = N_("E717: Dictionary entry already exists");
108static char *e_funcref = N_("E718: Funcref required");
109static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
110static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000111static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000112static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200113#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200114static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000116
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100117#define NAMESPACE_CHAR (char_u *)"abglstvw"
118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
128/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 * When recursively copying lists and dicts we need to remember which ones we
130 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 */
133static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134#define COPYID_INC 2
135#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000136
Bram Moolenaar8502c702014-06-17 12:51:16 +0200137/* Abort conversion to string after a recursion error. */
138static int did_echo_string_emsg = FALSE;
139
Bram Moolenaard9fba312005-06-26 22:34:35 +0000140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100159#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
160
161/* Values for get_lval() flags argument: */
162#define GLV_QUIET TFN_QUIET /* no error messages */
163#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000164
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165/*
166 * Structure to hold info for a user function.
167 */
168typedef struct ufunc ufunc_T;
169
170struct ufunc
171{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000172 int uf_varargs; /* variable nr of arguments */
173 int uf_flags;
174 int uf_calls; /* nr of active calls */
175 garray_T uf_args; /* arguments */
176 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000177#ifdef FEAT_PROFILE
178 int uf_profiling; /* TRUE when func is being profiled */
179 /* profiling the function as a whole */
180 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T uf_tm_total; /* time spent in function + children */
182 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tm_children; /* time spent in children this call */
184 /* profiling the function per line */
185 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000186 proftime_T *uf_tml_total; /* time spent in a line + children */
187 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000188 proftime_T uf_tml_start; /* start time for current line */
189 proftime_T uf_tml_children; /* time spent in children for this line */
190 proftime_T uf_tml_wait; /* start wait time for current line */
191 int uf_tml_idx; /* index of line being timed; -1 if none */
192 int uf_tml_execed; /* line being timed was executed */
193#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000194 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 int uf_refcount; /* for numbered function: reference count */
197 char_u uf_name[1]; /* name of function (actually longer); can
198 start with <SNR>123_ (<SNR> is K_SPECIAL
199 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200};
201
202/* function flags */
203#define FC_ABORT 1 /* abort function on error */
204#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000205#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
207/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000208 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000210static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000212/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000213static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
214
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100215/* List heads for garbage collection. Although there can be a reference loop
216 * from partial to dict to partial, we don't need to keep track of the partial,
217 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000218static dict_T *first_dict = NULL; /* list of all dicts */
219static list_T *first_list = NULL; /* list of all lists */
220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000221/* From user function to hashitem and back. */
222static ufunc_T dumuf;
223#define UF2HIKEY(fp) ((fp)->uf_name)
224#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
225#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
226
227#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
228#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229
Bram Moolenaar33570922005-01-25 22:26:29 +0000230#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
231#define VAR_SHORT_LEN 20 /* short variable name length */
232#define FIXVAR_CNT 12 /* number of fixed variables */
233
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000235typedef struct funccall_S funccall_T;
236
237struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238{
239 ufunc_T *func; /* function being called */
240 int linenr; /* next line to be executed */
241 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000242 struct /* fixed variables for arguments */
243 {
244 dictitem_T var; /* variable (without room for name) */
245 char_u room[VAR_SHORT_LEN]; /* room for the name */
246 } fixvar[FIXVAR_CNT];
247 dict_T l_vars; /* l: local function variables */
248 dictitem_T l_vars_var; /* variable for l: scope */
249 dict_T l_avars; /* a: argument variables */
250 dictitem_T l_avars_var; /* variable for a: scope */
251 list_T l_varlist; /* list for a:000 */
252 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
253 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254 linenr_T breakpoint; /* next line with breakpoint or zero */
255 int dbg_tick; /* debug_tick when breakpoint was set */
256 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000257#ifdef FEAT_PROFILE
258 proftime_T prof_child; /* time spent in a child */
259#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000260 funccall_T *caller; /* calling function or NULL */
261};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264 * Info used by a ":for" loop.
265 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000266typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267{
268 int fi_semicolon; /* TRUE if ending in '; var]' */
269 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 listwatch_T fi_lw; /* keep an eye on the item used. */
271 list_T *fi_list; /* list being used */
272} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000273
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000274/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000275 * Struct used by trans_function_name()
276 */
277typedef struct
278{
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000280 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 dictitem_T *fd_di; /* Dictionary item used */
282} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000283
Bram Moolenaara7043832005-01-21 11:56:39 +0000284
285/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000286 * Array to hold the value of v: variables.
287 * The value is in a dictitem, so that it can also be used in the v: scope.
288 * The reason to use this table anyway is for very quick access to the
289 * variables with the VV_ defines.
290 */
291#include "version.h"
292
293/* values for vv_flags: */
294#define VV_COMPAT 1 /* compatible, also used without "v:" */
295#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000296#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000297
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100298#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +0000299
300static struct vimvar
301{
302 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100303 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000304 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
305} vimvars[VV_LEN] =
306{
307 /*
308 * The order here must match the VV_ defines in vim.h!
309 * Initializing a union does not work, leave tv.vval empty to get zero's.
310 */
311 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("count1", VAR_NUMBER), VV_RO},
313 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
314 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
315 {VV_NAME("warningmsg", VAR_STRING), 0},
316 {VV_NAME("statusmsg", VAR_STRING), 0},
317 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
319 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
320 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
321 {VV_NAME("termresponse", VAR_STRING), VV_RO},
322 {VV_NAME("fname", VAR_STRING), VV_RO},
323 {VV_NAME("lang", VAR_STRING), VV_RO},
324 {VV_NAME("lc_time", VAR_STRING), VV_RO},
325 {VV_NAME("ctype", VAR_STRING), VV_RO},
326 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
327 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
328 {VV_NAME("fname_in", VAR_STRING), VV_RO},
329 {VV_NAME("fname_out", VAR_STRING), VV_RO},
330 {VV_NAME("fname_new", VAR_STRING), VV_RO},
331 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
332 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
333 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
336 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
337 {VV_NAME("progname", VAR_STRING), VV_RO},
338 {VV_NAME("servername", VAR_STRING), VV_RO},
339 {VV_NAME("dying", VAR_NUMBER), VV_RO},
340 {VV_NAME("exception", VAR_STRING), VV_RO},
341 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
342 {VV_NAME("register", VAR_STRING), VV_RO},
343 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
344 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000345 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
346 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000347 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000348 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
349 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000350 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
353 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
354 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000355 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000356 {VV_NAME("swapname", VAR_STRING), VV_RO},
357 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000358 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200359 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000360 {VV_NAME("mouse_win", VAR_NUMBER), 0},
361 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
362 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000363 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100365 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000366 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200367 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200368 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200369 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200370 {VV_NAME("option_new", VAR_STRING), VV_RO},
371 {VV_NAME("option_old", VAR_STRING), VV_RO},
372 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100373 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 {VV_NAME("false", VAR_SPECIAL), VV_RO},
375 {VV_NAME("true", VAR_SPECIAL), VV_RO},
376 {VV_NAME("null", VAR_SPECIAL), VV_RO},
377 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100378 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200379 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaar33570922005-01-25 22:26:29 +0000380};
381
382/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_type vv_di.di_tv.v_type
384#define vv_nr vv_di.di_tv.vval.v_number
385#define vv_float vv_di.di_tv.vval.v_float
386#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000387#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200388#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000389#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200391static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000392#define vimvarht vimvardict.dv_hashtab
393
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100394static void prepare_vimvar(int idx, typval_T *save_tv);
395static void restore_vimvar(int idx, typval_T *save_tv);
396static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
397static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
398static char_u *skip_var_one(char_u *arg);
399static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
400static void list_glob_vars(int *first);
401static void list_buf_vars(int *first);
402static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000403#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100404static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000405#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100406static void list_vim_vars(int *first);
407static void list_script_vars(int *first);
408static void list_func_vars(int *first);
409static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
410static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
411static int check_changedtick(char_u *arg);
412static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
413static void clear_lval(lval_T *lp);
414static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
415static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
416static void list_fix_watch(list_T *l, listitem_T *item);
417static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
418static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
419static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
420static void item_lock(typval_T *tv, int deep, int lock);
421static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000422
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100423static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
424static int eval1(char_u **arg, typval_T *rettv, int evaluate);
425static int eval2(char_u **arg, typval_T *rettv, int evaluate);
426static int eval3(char_u **arg, typval_T *rettv, int evaluate);
427static int eval4(char_u **arg, typval_T *rettv, int evaluate);
428static int eval5(char_u **arg, typval_T *rettv, int evaluate);
429static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
430static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000431
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100432static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
433static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
434static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
435static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
436static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200437static void list_free_contents(list_T *l);
438static void list_free_list(list_T *l);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static long list_len(list_T *l);
440static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
441static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
442static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
443static long list_find_nr(list_T *l, long idx, int *errorp);
444static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100445static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
446static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
447static list_T *list_copy(list_T *orig, int deep, int copyID);
448static char_u *list2string(typval_T *tv, int copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
451static int free_unref_items(int copyID);
452static dictitem_T *dictitem_copy(dictitem_T *org);
453static void dictitem_remove(dict_T *dict, dictitem_T *item);
454static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
455static long dict_len(dict_T *d);
456static char_u *dict2string(typval_T *tv, int copyID);
457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
458static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *string_quote(char_u *str, int function);
460static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
461static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100462static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
463static int get_func_tv(char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void emsg_funcname(char *ermsg, char_u *name);
465static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000466
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200467static void dict_free_contents(dict_T *d);
468static void dict_free_dict(dict_T *d);
469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
475static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200486static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +0200487static void f_assert_notequal(typval_T *argvars, typval_T *rettv);
488static void f_assert_notmatch(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100489static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000490#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100491static void f_asin(typval_T *argvars, typval_T *rettv);
492static void f_atan(typval_T *argvars, typval_T *rettv);
493static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100495static void f_browse(typval_T *argvars, typval_T *rettv);
496static void f_browsedir(typval_T *argvars, typval_T *rettv);
497static void f_bufexists(typval_T *argvars, typval_T *rettv);
498static void f_buflisted(typval_T *argvars, typval_T *rettv);
499static void f_bufloaded(typval_T *argvars, typval_T *rettv);
500static void f_bufname(typval_T *argvars, typval_T *rettv);
501static void f_bufnr(typval_T *argvars, typval_T *rettv);
502static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
503static void f_byte2line(typval_T *argvars, typval_T *rettv);
504static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
505static void f_byteidx(typval_T *argvars, typval_T *rettv);
506static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
507static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000508#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100509static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000510#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100511#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100513static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100515static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100516static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100517static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100518static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
520static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100521static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100522static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100523static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
524static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100525static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100526static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_changenr(typval_T *argvars, typval_T *rettv);
529static void f_char2nr(typval_T *argvars, typval_T *rettv);
530static void f_cindent(typval_T *argvars, typval_T *rettv);
531static void f_clearmatches(typval_T *argvars, typval_T *rettv);
532static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000533#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_complete(typval_T *argvars, typval_T *rettv);
535static void f_complete_add(typval_T *argvars, typval_T *rettv);
536static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_confirm(typval_T *argvars, typval_T *rettv);
539static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_cos(typval_T *argvars, typval_T *rettv);
542static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000543#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100544static void f_count(typval_T *argvars, typval_T *rettv);
545static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
546static void f_cursor(typval_T *argsvars, typval_T *rettv);
547static void f_deepcopy(typval_T *argvars, typval_T *rettv);
548static void f_delete(typval_T *argvars, typval_T *rettv);
549static void f_did_filetype(typval_T *argvars, typval_T *rettv);
550static void f_diff_filler(typval_T *argvars, typval_T *rettv);
551static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100552static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_empty(typval_T *argvars, typval_T *rettv);
554static void f_escape(typval_T *argvars, typval_T *rettv);
555static void f_eval(typval_T *argvars, typval_T *rettv);
556static void f_eventhandler(typval_T *argvars, typval_T *rettv);
557static void f_executable(typval_T *argvars, typval_T *rettv);
558static void f_exepath(typval_T *argvars, typval_T *rettv);
559static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200562#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100563static void f_expand(typval_T *argvars, typval_T *rettv);
564static void f_extend(typval_T *argvars, typval_T *rettv);
565static void f_feedkeys(typval_T *argvars, typval_T *rettv);
566static void f_filereadable(typval_T *argvars, typval_T *rettv);
567static void f_filewritable(typval_T *argvars, typval_T *rettv);
568static void f_filter(typval_T *argvars, typval_T *rettv);
569static void f_finddir(typval_T *argvars, typval_T *rettv);
570static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000571#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100572static void f_float2nr(typval_T *argvars, typval_T *rettv);
573static void f_floor(typval_T *argvars, typval_T *rettv);
574static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000575#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100576static void f_fnameescape(typval_T *argvars, typval_T *rettv);
577static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
578static void f_foldclosed(typval_T *argvars, typval_T *rettv);
579static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
580static void f_foldlevel(typval_T *argvars, typval_T *rettv);
581static void f_foldtext(typval_T *argvars, typval_T *rettv);
582static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
583static void f_foreground(typval_T *argvars, typval_T *rettv);
584static void f_function(typval_T *argvars, typval_T *rettv);
585static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200586static void f_garbagecollect_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100587static void f_get(typval_T *argvars, typval_T *rettv);
588static void f_getbufline(typval_T *argvars, typval_T *rettv);
589static void f_getbufvar(typval_T *argvars, typval_T *rettv);
590static void f_getchar(typval_T *argvars, typval_T *rettv);
591static void f_getcharmod(typval_T *argvars, typval_T *rettv);
592static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
593static void f_getcmdline(typval_T *argvars, typval_T *rettv);
594static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
595static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
596static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
597static void f_getcwd(typval_T *argvars, typval_T *rettv);
598static void f_getfontname(typval_T *argvars, typval_T *rettv);
599static void f_getfperm(typval_T *argvars, typval_T *rettv);
600static void f_getfsize(typval_T *argvars, typval_T *rettv);
601static void f_getftime(typval_T *argvars, typval_T *rettv);
602static void f_getftype(typval_T *argvars, typval_T *rettv);
603static void f_getline(typval_T *argvars, typval_T *rettv);
604static void f_getmatches(typval_T *argvars, typval_T *rettv);
605static void f_getpid(typval_T *argvars, typval_T *rettv);
606static void f_getcurpos(typval_T *argvars, typval_T *rettv);
607static void f_getpos(typval_T *argvars, typval_T *rettv);
608static void f_getqflist(typval_T *argvars, typval_T *rettv);
609static void f_getreg(typval_T *argvars, typval_T *rettv);
610static void f_getregtype(typval_T *argvars, typval_T *rettv);
611static void f_gettabvar(typval_T *argvars, typval_T *rettv);
612static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
613static void f_getwinposx(typval_T *argvars, typval_T *rettv);
614static void f_getwinposy(typval_T *argvars, typval_T *rettv);
615static void f_getwinvar(typval_T *argvars, typval_T *rettv);
616static void f_glob(typval_T *argvars, typval_T *rettv);
617static void f_globpath(typval_T *argvars, typval_T *rettv);
618static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
619static void f_has(typval_T *argvars, typval_T *rettv);
620static void f_has_key(typval_T *argvars, typval_T *rettv);
621static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
622static void f_hasmapto(typval_T *argvars, typval_T *rettv);
623static void f_histadd(typval_T *argvars, typval_T *rettv);
624static void f_histdel(typval_T *argvars, typval_T *rettv);
625static void f_histget(typval_T *argvars, typval_T *rettv);
626static void f_histnr(typval_T *argvars, typval_T *rettv);
627static void f_hlID(typval_T *argvars, typval_T *rettv);
628static void f_hlexists(typval_T *argvars, typval_T *rettv);
629static void f_hostname(typval_T *argvars, typval_T *rettv);
630static void f_iconv(typval_T *argvars, typval_T *rettv);
631static void f_indent(typval_T *argvars, typval_T *rettv);
632static void f_index(typval_T *argvars, typval_T *rettv);
633static void f_input(typval_T *argvars, typval_T *rettv);
634static void f_inputdialog(typval_T *argvars, typval_T *rettv);
635static void f_inputlist(typval_T *argvars, typval_T *rettv);
636static void f_inputrestore(typval_T *argvars, typval_T *rettv);
637static void f_inputsave(typval_T *argvars, typval_T *rettv);
638static void f_inputsecret(typval_T *argvars, typval_T *rettv);
639static void f_insert(typval_T *argvars, typval_T *rettv);
640static void f_invert(typval_T *argvars, typval_T *rettv);
641static void f_isdirectory(typval_T *argvars, typval_T *rettv);
642static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100643#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
644static void f_isnan(typval_T *argvars, typval_T *rettv);
645#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100646static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100647#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100648static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100649static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100650static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100651static void f_job_start(typval_T *argvars, typval_T *rettv);
652static void f_job_stop(typval_T *argvars, typval_T *rettv);
653static void f_job_status(typval_T *argvars, typval_T *rettv);
654#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100656static void f_js_decode(typval_T *argvars, typval_T *rettv);
657static void f_js_encode(typval_T *argvars, typval_T *rettv);
658static void f_json_decode(typval_T *argvars, typval_T *rettv);
659static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100660static void f_keys(typval_T *argvars, typval_T *rettv);
661static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
662static void f_len(typval_T *argvars, typval_T *rettv);
663static void f_libcall(typval_T *argvars, typval_T *rettv);
664static void f_libcallnr(typval_T *argvars, typval_T *rettv);
665static void f_line(typval_T *argvars, typval_T *rettv);
666static void f_line2byte(typval_T *argvars, typval_T *rettv);
667static void f_lispindent(typval_T *argvars, typval_T *rettv);
668static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100670static void f_log(typval_T *argvars, typval_T *rettv);
671static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000672#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200675#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100676static void f_map(typval_T *argvars, typval_T *rettv);
677static void f_maparg(typval_T *argvars, typval_T *rettv);
678static void f_mapcheck(typval_T *argvars, typval_T *rettv);
679static void f_match(typval_T *argvars, typval_T *rettv);
680static void f_matchadd(typval_T *argvars, typval_T *rettv);
681static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
682static void f_matcharg(typval_T *argvars, typval_T *rettv);
683static void f_matchdelete(typval_T *argvars, typval_T *rettv);
684static void f_matchend(typval_T *argvars, typval_T *rettv);
685static void f_matchlist(typval_T *argvars, typval_T *rettv);
686static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200687static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100688static void f_max(typval_T *argvars, typval_T *rettv);
689static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100696#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100697static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
698static void f_nr2char(typval_T *argvars, typval_T *rettv);
699static void f_or(typval_T *argvars, typval_T *rettv);
700static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100703#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000706#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
708static void f_printf(typval_T *argvars, typval_T *rettv);
709static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200712#endif
713#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200715#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_range(typval_T *argvars, typval_T *rettv);
717static void f_readfile(typval_T *argvars, typval_T *rettv);
718static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100719#ifdef FEAT_FLOAT
720static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
721#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_reltimestr(typval_T *argvars, typval_T *rettv);
723static void f_remote_expr(typval_T *argvars, typval_T *rettv);
724static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
725static void f_remote_peek(typval_T *argvars, typval_T *rettv);
726static void f_remote_read(typval_T *argvars, typval_T *rettv);
727static void f_remote_send(typval_T *argvars, typval_T *rettv);
728static void f_remove(typval_T *argvars, typval_T *rettv);
729static void f_rename(typval_T *argvars, typval_T *rettv);
730static void f_repeat(typval_T *argvars, typval_T *rettv);
731static void f_resolve(typval_T *argvars, typval_T *rettv);
732static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000735#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100736static void f_screenattr(typval_T *argvars, typval_T *rettv);
737static void f_screenchar(typval_T *argvars, typval_T *rettv);
738static void f_screencol(typval_T *argvars, typval_T *rettv);
739static void f_screenrow(typval_T *argvars, typval_T *rettv);
740static void f_search(typval_T *argvars, typval_T *rettv);
741static void f_searchdecl(typval_T *argvars, typval_T *rettv);
742static void f_searchpair(typval_T *argvars, typval_T *rettv);
743static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
744static void f_searchpos(typval_T *argvars, typval_T *rettv);
745static void f_server2client(typval_T *argvars, typval_T *rettv);
746static void f_serverlist(typval_T *argvars, typval_T *rettv);
747static void f_setbufvar(typval_T *argvars, typval_T *rettv);
748static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
749static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100750static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_setline(typval_T *argvars, typval_T *rettv);
752static void f_setloclist(typval_T *argvars, typval_T *rettv);
753static void f_setmatches(typval_T *argvars, typval_T *rettv);
754static void f_setpos(typval_T *argvars, typval_T *rettv);
755static void f_setqflist(typval_T *argvars, typval_T *rettv);
756static void f_setreg(typval_T *argvars, typval_T *rettv);
757static void f_settabvar(typval_T *argvars, typval_T *rettv);
758static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
759static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100762#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_shellescape(typval_T *argvars, typval_T *rettv);
764static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
765static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_sin(typval_T *argvars, typval_T *rettv);
768static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000769#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_sort(typval_T *argvars, typval_T *rettv);
771static void f_soundfold(typval_T *argvars, typval_T *rettv);
772static void f_spellbadword(typval_T *argvars, typval_T *rettv);
773static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
774static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000775#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100776static void f_sqrt(typval_T *argvars, typval_T *rettv);
777static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_str2nr(typval_T *argvars, typval_T *rettv);
780static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100782static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000783#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200784static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100785static void f_stridx(typval_T *argvars, typval_T *rettv);
786static void f_string(typval_T *argvars, typval_T *rettv);
787static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200788static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100789static void f_strpart(typval_T *argvars, typval_T *rettv);
790static void f_strridx(typval_T *argvars, typval_T *rettv);
791static void f_strtrans(typval_T *argvars, typval_T *rettv);
792static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
793static void f_strwidth(typval_T *argvars, typval_T *rettv);
794static void f_submatch(typval_T *argvars, typval_T *rettv);
795static void f_substitute(typval_T *argvars, typval_T *rettv);
796static void f_synID(typval_T *argvars, typval_T *rettv);
797static void f_synIDattr(typval_T *argvars, typval_T *rettv);
798static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
799static void f_synstack(typval_T *argvars, typval_T *rettv);
800static void f_synconcealed(typval_T *argvars, typval_T *rettv);
801static void f_system(typval_T *argvars, typval_T *rettv);
802static void f_systemlist(typval_T *argvars, typval_T *rettv);
803static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
804static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
805static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
806static void f_taglist(typval_T *argvars, typval_T *rettv);
807static void f_tagfiles(typval_T *argvars, typval_T *rettv);
808static void f_tempname(typval_T *argvars, typval_T *rettv);
809static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200810#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_tan(typval_T *argvars, typval_T *rettv);
812static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200813#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100814#ifdef FEAT_TIMERS
815static void f_timer_start(typval_T *argvars, typval_T *rettv);
816static void f_timer_stop(typval_T *argvars, typval_T *rettv);
817#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100818static void f_tolower(typval_T *argvars, typval_T *rettv);
819static void f_toupper(typval_T *argvars, typval_T *rettv);
820static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000821#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000823#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static void f_type(typval_T *argvars, typval_T *rettv);
825static void f_undofile(typval_T *argvars, typval_T *rettv);
826static void f_undotree(typval_T *argvars, typval_T *rettv);
827static void f_uniq(typval_T *argvars, typval_T *rettv);
828static void f_values(typval_T *argvars, typval_T *rettv);
829static void f_virtcol(typval_T *argvars, typval_T *rettv);
830static void f_visualmode(typval_T *argvars, typval_T *rettv);
831static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100832static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100833static void f_win_getid(typval_T *argvars, typval_T *rettv);
834static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
835static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
836static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static void f_winbufnr(typval_T *argvars, typval_T *rettv);
838static void f_wincol(typval_T *argvars, typval_T *rettv);
839static void f_winheight(typval_T *argvars, typval_T *rettv);
840static void f_winline(typval_T *argvars, typval_T *rettv);
841static void f_winnr(typval_T *argvars, typval_T *rettv);
842static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
843static void f_winrestview(typval_T *argvars, typval_T *rettv);
844static void f_winsaveview(typval_T *argvars, typval_T *rettv);
845static void f_winwidth(typval_T *argvars, typval_T *rettv);
846static void f_writefile(typval_T *argvars, typval_T *rettv);
847static void f_wordcount(typval_T *argvars, typval_T *rettv);
848static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000849
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
851static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
852static int get_env_len(char_u **arg);
853static int get_id_len(char_u **arg);
854static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
855static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000856#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
857#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
858 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100859static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
860static int eval_isnamec(int c);
861static int eval_isnamec1(int c);
862static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
863static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100864static typval_T *alloc_string_tv(char_u *string);
865static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100866#ifdef FEAT_FLOAT
867static float_T get_tv_float(typval_T *varp);
868#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static linenr_T get_tv_lnum(typval_T *argvars);
870static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100871static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
872static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
873static hashtab_T *find_var_ht(char_u *name, char_u **varname);
874static funccall_T *get_funccal(void);
875static void vars_clear_ext(hashtab_T *ht, int free_val);
876static void delete_var(hashtab_T *ht, hashitem_T *hi);
877static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
878static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
879static void set_var(char_u *name, typval_T *varp, int copy);
880static int var_check_ro(int flags, char_u *name, int use_gettext);
881static int var_check_fixed(int flags, char_u *name, int use_gettext);
882static int var_check_func_name(char_u *name, int new_var);
883static int valid_varname(char_u *varname);
884static int tv_check_lock(int lock, char_u *name, int use_gettext);
885static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
886static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100887static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int eval_fname_script(char_u *p);
889static int eval_fname_sid(char_u *p);
890static void list_func_head(ufunc_T *fp, int indent);
891static ufunc_T *find_func(char_u *name);
892static int function_exists(char_u *name);
893static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100895static void func_do_profile(ufunc_T *fp);
896static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
897static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000898static int
899# ifdef __BORLANDC__
900 _RTLENTRYF
901# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100902 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000903static int
904# ifdef __BORLANDC__
905 _RTLENTRYF
906# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000908#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100909static int script_autoload(char_u *name, int reload);
910static char_u *autoload_name(char_u *name);
911static void cat_func_name(char_u *buf, ufunc_T *fp);
912static void func_free(ufunc_T *fp);
913static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
914static int can_free_funccal(funccall_T *fc, int copyID) ;
915static void free_funccal(funccall_T *fc, int free_val);
916static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
917static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
918static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
919static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
920static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
921static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
922static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
923static int write_list(FILE *fd, list_T *list, int binary);
924static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200926
927#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100928static int compare_func_name(const void *s1, const void *s2);
929static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200930#endif
931
Bram Moolenaar33570922005-01-25 22:26:29 +0000932/*
933 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000934 */
935 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100936eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000937{
Bram Moolenaar33570922005-01-25 22:26:29 +0000938 int i;
939 struct vimvar *p;
940
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200941 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
942 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200943 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000944 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946
947 for (i = 0; i < VV_LEN; ++i)
948 {
949 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200950 if (STRLEN(p->vv_name) > 16)
951 {
952 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
953 getout(1);
954 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 STRCPY(p->vv_di.di_key, p->vv_name);
956 if (p->vv_flags & VV_RO)
957 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
958 else if (p->vv_flags & VV_RO_SBX)
959 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
960 else
961 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000962
963 /* add to v: scope dict, unless the value is not always available */
964 if (p->vv_type != VAR_UNKNOWN)
965 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000966 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000967 /* add to compat scope dict */
968 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000969 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100970 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
971
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000972 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100973 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200974 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100975 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100976
977 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
978 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
979 set_vim_var_nr(VV_NONE, VVAL_NONE);
980 set_vim_var_nr(VV_NULL, VVAL_NULL);
981
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200982 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200983
984#ifdef EBCDIC
985 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100986 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200987 */
988 sortFunctions();
989#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000990}
991
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992#if defined(EXITFREE) || defined(PROTO)
993 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100994eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995{
996 int i;
997 struct vimvar *p;
998
999 for (i = 0; i < VV_LEN; ++i)
1000 {
1001 p = &vimvars[i];
1002 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001003 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001004 vim_free(p->vv_str);
1005 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001006 }
1007 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1008 {
1009 list_unref(p->vv_list);
1010 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001011 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001012 }
1013 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001014 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001015 hash_clear(&compat_hashtab);
1016
Bram Moolenaard9fba312005-06-26 22:34:35 +00001017 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001018# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001019 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001020# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001021
1022 /* global variables */
1023 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001024
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001025 /* autoloaded script names */
1026 ga_clear_strings(&ga_loaded);
1027
Bram Moolenaarcca74132013-09-25 21:00:28 +02001028 /* Script-local variables. First clear all the variables and in a second
1029 * loop free the scriptvar_T, because a variable in one script might hold
1030 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001031 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001032 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001033 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001034 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001035 ga_clear(&ga_scripts);
1036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001037 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001038 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001039
1040 /* functions */
1041 free_all_functions();
1042 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001043}
1044#endif
1045
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001046/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 * Return the name of the executed function.
1048 */
1049 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001050func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001052 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
1054
1055/*
1056 * Return the address holding the next breakpoint line for a funccall cookie.
1057 */
1058 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001059func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar33570922005-01-25 22:26:29 +00001061 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/*
1065 * Return the address holding the debug tick for a funccall cookie.
1066 */
1067 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001068func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069{
Bram Moolenaar33570922005-01-25 22:26:29 +00001070 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071}
1072
1073/*
1074 * Return the nesting level for a funccall cookie.
1075 */
1076 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001077func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078{
Bram Moolenaar33570922005-01-25 22:26:29 +00001079 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080}
1081
1082/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001083funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001085/* pointer to list of previously used funccal, still around because some
1086 * item in it is still being used. */
1087funccall_T *previous_funccal = NULL;
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089/*
1090 * Return TRUE when a function was ended by a ":return" command.
1091 */
1092 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001093current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094{
1095 return current_funccal->returned;
1096}
1097
1098
1099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 * Set an internal variable to a string value. Creates the variable if it does
1101 * not already exist.
1102 */
1103 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001104set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001106 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001107 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109 val = vim_strsave(value);
1110 if (val != NULL)
1111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001112 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001113 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001115 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 }
1118 }
1119}
1120
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001122static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123static char_u *redir_endp = NULL;
1124static char_u *redir_varname = NULL;
1125
1126/*
1127 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001128 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129 * Returns OK if successfully completed the setup. FAIL otherwise.
1130 */
1131 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001132var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133{
1134 int save_emsg;
1135 int err;
1136 typval_T tv;
1137
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001138 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001139 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 {
1141 EMSG(_(e_invarg));
1142 return FAIL;
1143 }
1144
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 redir_varname = vim_strsave(name);
1147 if (redir_varname == NULL)
1148 return FAIL;
1149
1150 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1151 if (redir_lval == NULL)
1152 {
1153 var_redir_stop();
1154 return FAIL;
1155 }
1156
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157 /* The output is stored in growarray "redir_ga" until redirection ends. */
1158 ga_init2(&redir_ga, (int)sizeof(char), 500);
1159
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001160 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001161 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001162 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1164 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001165 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (redir_endp != NULL && *redir_endp != NUL)
1167 /* Trailing characters are present after the variable name */
1168 EMSG(_(e_trailing));
1169 else
1170 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 var_redir_stop();
1173 return FAIL;
1174 }
1175
1176 /* check if we can write to the variable: set it to or append an empty
1177 * string */
1178 save_emsg = did_emsg;
1179 did_emsg = FALSE;
1180 tv.v_type = VAR_STRING;
1181 tv.vval.v_string = (char_u *)"";
1182 if (append)
1183 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1184 else
1185 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001186 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001188 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 if (err)
1190 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001191 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 var_redir_stop();
1193 return FAIL;
1194 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195
1196 return OK;
1197}
1198
1199/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001200 * Append "value[value_len]" to the variable set by var_redir_start().
1201 * The actual appending is postponed until redirection ends, because the value
1202 * appended may in fact be the string we write to, changing it may cause freed
1203 * memory to be used:
1204 * :redir => foo
1205 * :let foo
1206 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001209var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001211 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001212
1213 if (redir_lval == NULL)
1214 return;
1215
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001216 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001217 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001219 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001222 {
1223 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001224 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001225 }
1226 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001227 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228}
1229
1230/*
1231 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001232 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001233 */
1234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001235var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001236{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237 typval_T tv;
1238
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001239 if (redir_lval != NULL)
1240 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 /* If there was no error: assign the text to the variable. */
1242 if (redir_endp != NULL)
1243 {
1244 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1245 tv.v_type = VAR_STRING;
1246 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001247 /* Call get_lval() again, if it's inside a Dict or List it may
1248 * have changed. */
1249 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001250 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001251 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1252 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1253 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001254 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001255
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001256 /* free the collected output */
1257 vim_free(redir_ga.ga_data);
1258 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001259
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001260 vim_free(redir_lval);
1261 redir_lval = NULL;
1262 }
1263 vim_free(redir_varname);
1264 redir_varname = NULL;
1265}
1266
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267# if defined(FEAT_MBYTE) || defined(PROTO)
1268 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001269eval_charconvert(
1270 char_u *enc_from,
1271 char_u *enc_to,
1272 char_u *fname_from,
1273 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1278 set_vim_var_string(VV_CC_TO, enc_to, -1);
1279 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1280 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1281 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1282 err = TRUE;
1283 set_vim_var_string(VV_CC_FROM, NULL, -1);
1284 set_vim_var_string(VV_CC_TO, NULL, -1);
1285 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1286 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1287
1288 if (err)
1289 return FAIL;
1290 return OK;
1291}
1292# endif
1293
1294# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001296eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297{
1298 int err = FALSE;
1299
1300 set_vim_var_string(VV_FNAME_IN, fname, -1);
1301 set_vim_var_string(VV_CMDARG, args, -1);
1302 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1303 err = TRUE;
1304 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1305 set_vim_var_string(VV_CMDARG, NULL, -1);
1306
1307 if (err)
1308 {
1309 mch_remove(fname);
1310 return FAIL;
1311 }
1312 return OK;
1313}
1314# endif
1315
1316# if defined(FEAT_DIFF) || defined(PROTO)
1317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001318eval_diff(
1319 char_u *origfile,
1320 char_u *newfile,
1321 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322{
1323 int err = FALSE;
1324
1325 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1326 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1327 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1328 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1329 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1330 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1331 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1332}
1333
1334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335eval_patch(
1336 char_u *origfile,
1337 char_u *difffile,
1338 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339{
1340 int err;
1341
1342 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1343 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1344 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1345 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1346 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1347 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1348 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1349}
1350# endif
1351
1352/*
1353 * Top level evaluation function, returning a boolean.
1354 * Sets "error" to TRUE if there was an error.
1355 * Return TRUE or FALSE.
1356 */
1357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001358eval_to_bool(
1359 char_u *arg,
1360 int *error,
1361 char_u **nextcmd,
1362 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 int retval = FALSE;
1366
1367 if (skip)
1368 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001369 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 else
1372 {
1373 *error = FALSE;
1374 if (!skip)
1375 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001376 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001377 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 }
1379 }
1380 if (skip)
1381 --emsg_skip;
1382
1383 return retval;
1384}
1385
1386/*
1387 * Top level evaluation function, returning a string. If "skip" is TRUE,
1388 * only parsing to "nextcmd" is done, without reporting errors. Return
1389 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1390 */
1391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001392eval_to_string_skip(
1393 char_u *arg,
1394 char_u **nextcmd,
1395 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396{
Bram Moolenaar33570922005-01-25 22:26:29 +00001397 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 char_u *retval;
1399
1400 if (skip)
1401 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 retval = NULL;
1404 else
1405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 retval = vim_strsave(get_tv_string(&tv));
1407 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 if (skip)
1410 --emsg_skip;
1411
1412 return retval;
1413}
1414
1415/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001416 * Skip over an expression at "*pp".
1417 * Return FAIL for an error, OK otherwise.
1418 */
1419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001423
1424 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001426}
1427
1428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001430 * When "convert" is TRUE convert a List into a sequence of lines and convert
1431 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 * Return pointer to allocated memory, or NULL for failure.
1433 */
1434 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001435eval_to_string(
1436 char_u *arg,
1437 char_u **nextcmd,
1438 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439{
Bram Moolenaar33570922005-01-25 22:26:29 +00001440 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001443#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001444 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001447 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 retval = NULL;
1449 else
1450 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001451 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 {
1453 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001454 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001455 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001456 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001457 if (tv.vval.v_list->lv_len > 0)
1458 ga_append(&ga, NL);
1459 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001460 ga_append(&ga, NUL);
1461 retval = (char_u *)ga.ga_data;
1462 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001463#ifdef FEAT_FLOAT
1464 else if (convert && tv.v_type == VAR_FLOAT)
1465 {
1466 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1467 retval = vim_strsave(numbuf);
1468 }
1469#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 else
1471 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001472 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474
1475 return retval;
1476}
1477
1478/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001479 * Call eval_to_string() without using current local variables and using
1480 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 */
1482 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_string_safe(
1484 char_u *arg,
1485 char_u **nextcmd,
1486 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
1488 char_u *retval;
1489 void *save_funccalp;
1490
1491 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001492 if (use_sandbox)
1493 ++sandbox;
1494 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001495 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001496 if (use_sandbox)
1497 --sandbox;
1498 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 restore_funccal(save_funccalp);
1500 return retval;
1501}
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503/*
1504 * Top level evaluation function, returning a number.
1505 * Evaluates "expr" silently.
1506 * Returns -1 for an error.
1507 */
1508 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510{
Bram Moolenaar33570922005-01-25 22:26:29 +00001511 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001513 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514
1515 ++emsg_off;
1516
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 retval = -1;
1519 else
1520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001521 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001522 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 }
1524 --emsg_off;
1525
1526 return retval;
1527}
1528
Bram Moolenaara40058a2005-07-11 22:42:07 +00001529/*
1530 * Prepare v: variable "idx" to be used.
1531 * Save the current typeval in "save_tv".
1532 * When not used yet add the variable to the v: hashtable.
1533 */
1534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001535prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001536{
1537 *save_tv = vimvars[idx].vv_tv;
1538 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1539 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1540}
1541
1542/*
1543 * Restore v: variable "idx" to typeval "save_tv".
1544 * When no longer defined, remove the variable from the v: hashtable.
1545 */
1546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001548{
1549 hashitem_T *hi;
1550
Bram Moolenaara40058a2005-07-11 22:42:07 +00001551 vimvars[idx].vv_tv = *save_tv;
1552 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1553 {
1554 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1555 if (HASHITEM_EMPTY(hi))
1556 EMSG2(_(e_intern2), "restore_vimvar()");
1557 else
1558 hash_remove(&vimvarht, hi);
1559 }
1560}
1561
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001562#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001563/*
1564 * Evaluate an expression to a list with suggestions.
1565 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001566 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 */
1568 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570{
1571 typval_T save_val;
1572 typval_T rettv;
1573 list_T *list = NULL;
1574 char_u *p = skipwhite(expr);
1575
1576 /* Set "v:val" to the bad word. */
1577 prepare_vimvar(VV_VAL, &save_val);
1578 vimvars[VV_VAL].vv_type = VAR_STRING;
1579 vimvars[VV_VAL].vv_str = badword;
1580 if (p_verbose == 0)
1581 ++emsg_off;
1582
1583 if (eval1(&p, &rettv, TRUE) == OK)
1584 {
1585 if (rettv.v_type != VAR_LIST)
1586 clear_tv(&rettv);
1587 else
1588 list = rettv.vval.v_list;
1589 }
1590
1591 if (p_verbose == 0)
1592 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593 restore_vimvar(VV_VAL, &save_val);
1594
1595 return list;
1596}
1597
1598/*
1599 * "list" is supposed to contain two items: a word and a number. Return the
1600 * word in "pp" and the number as the return value.
1601 * Return -1 if anything isn't right.
1602 * Used to get the good word and score from the eval_spell_expr() result.
1603 */
1604 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001605get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001606{
1607 listitem_T *li;
1608
1609 li = list->lv_first;
1610 if (li == NULL)
1611 return -1;
1612 *pp = get_tv_string(&li->li_tv);
1613
1614 li = li->li_next;
1615 if (li == NULL)
1616 return -1;
1617 return get_tv_number(&li->li_tv);
1618}
1619#endif
1620
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001621/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001622 * Top level evaluation function.
1623 * Returns an allocated typval_T with the result.
1624 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001625 */
1626 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001628{
1629 typval_T *tv;
1630
1631 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001633 {
1634 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001635 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001636 }
1637
1638 return tv;
1639}
1640
1641
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001644 * Uses argv[argc] for the function arguments. Only Number and String
1645 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001646 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001649call_vim_function(
1650 char_u *func,
1651 int argc,
1652 char_u **argv,
1653 int safe, /* use the sandbox */
1654 int str_arg_only, /* all arguments are strings */
1655 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656{
Bram Moolenaar33570922005-01-25 22:26:29 +00001657 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 long n;
1659 int len;
1660 int i;
1661 int doesrange;
1662 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001665 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
1669 for (i = 0; i < argc; i++)
1670 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001671 /* Pass a NULL or empty argument as an empty string */
1672 if (argv[i] == NULL || *argv[i] == NUL)
1673 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001674 argvars[i].v_type = VAR_STRING;
1675 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001676 continue;
1677 }
1678
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001679 if (str_arg_only)
1680 len = 0;
1681 else
1682 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001683 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 if (len != 0 && len == (int)STRLEN(argv[i]))
1685 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001686 argvars[i].v_type = VAR_NUMBER;
1687 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
1689 else
1690 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001691 argvars[i].v_type = VAR_STRING;
1692 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 }
1694 }
1695
1696 if (safe)
1697 {
1698 save_funccalp = save_funccal();
1699 ++sandbox;
1700 }
1701
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001702 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1703 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001705 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 if (safe)
1707 {
1708 --sandbox;
1709 restore_funccal(save_funccalp);
1710 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001711 vim_free(argvars);
1712
1713 if (ret == FAIL)
1714 clear_tv(rettv);
1715
1716 return ret;
1717}
1718
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001719/*
1720 * Call vimL function "func" and return the result as a number.
1721 * Returns -1 when calling the function fails.
1722 * Uses argv[argc] for the function arguments.
1723 */
1724 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001725call_func_retnr(
1726 char_u *func,
1727 int argc,
1728 char_u **argv,
1729 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001730{
1731 typval_T rettv;
1732 long retval;
1733
1734 /* All arguments are passed as strings, no conversion to number. */
1735 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1736 return -1;
1737
1738 retval = get_tv_number_chk(&rettv, NULL);
1739 clear_tv(&rettv);
1740 return retval;
1741}
1742
1743#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1744 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1745
Bram Moolenaar4f688582007-07-24 12:34:30 +00001746# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001748 * Call vimL function "func" and return the result as a string.
1749 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 * Uses argv[argc] for the function arguments.
1751 */
1752 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001753call_func_retstr(
1754 char_u *func,
1755 int argc,
1756 char_u **argv,
1757 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758{
1759 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001760 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001762 /* All arguments are passed as strings, no conversion to number. */
1763 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764 return NULL;
1765
1766 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001767 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 return retval;
1769}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001770# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001772/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001773 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001775 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001776 */
1777 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001778call_func_retlist(
1779 char_u *func,
1780 int argc,
1781 char_u **argv,
1782 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001783{
1784 typval_T rettv;
1785
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001786 /* All arguments are passed as strings, no conversion to number. */
1787 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 return NULL;
1789
1790 if (rettv.v_type != VAR_LIST)
1791 {
1792 clear_tv(&rettv);
1793 return NULL;
1794 }
1795
1796 return rettv.vval.v_list;
1797}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
1799
1800/*
1801 * Save the current function call pointer, and set it to NULL.
1802 * Used when executing autocommands and for ":source".
1803 */
1804 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 current_funccal = NULL;
1810 return (void *)fc;
1811}
1812
1813 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001814restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001816 funccall_T *fc = (funccall_T *)vfc;
1817
1818 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819}
1820
Bram Moolenaar05159a02005-02-26 23:04:13 +00001821#if defined(FEAT_PROFILE) || defined(PROTO)
1822/*
1823 * Prepare profiling for entering a child or something else that is not
1824 * counted for the script/function itself.
1825 * Should always be called in pair with prof_child_exit().
1826 */
1827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828prof_child_enter(
1829 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001830{
1831 funccall_T *fc = current_funccal;
1832
1833 if (fc != NULL && fc->func->uf_profiling)
1834 profile_start(&fc->prof_child);
1835 script_prof_save(tm);
1836}
1837
1838/*
1839 * Take care of time spent in a child.
1840 * Should always be called after prof_child_enter().
1841 */
1842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843prof_child_exit(
1844 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001845{
1846 funccall_T *fc = current_funccal;
1847
1848 if (fc != NULL && fc->func->uf_profiling)
1849 {
1850 profile_end(&fc->prof_child);
1851 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1852 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1853 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1854 }
1855 script_prof_restore(tm);
1856}
1857#endif
1858
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860#ifdef FEAT_FOLDING
1861/*
1862 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1863 * it in "*cp". Doesn't give error messages.
1864 */
1865 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001866eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867{
Bram Moolenaar33570922005-01-25 22:26:29 +00001868 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 int retval;
1870 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001871 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1872 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873
1874 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001875 if (use_sandbox)
1876 ++sandbox;
1877 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 retval = 0;
1881 else
1882 {
1883 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 if (tv.v_type == VAR_NUMBER)
1885 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001886 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 retval = 0;
1888 else
1889 {
1890 /* If the result is a string, check if there is a non-digit before
1891 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (!VIM_ISDIGIT(*s) && *s != '-')
1894 *cp = *s++;
1895 retval = atol((char *)s);
1896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 }
1899 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001900 if (use_sandbox)
1901 --sandbox;
1902 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 return retval;
1905}
1906#endif
1907
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001909 * ":let" list all variable values
1910 * ":let var1 var2" list variable values
1911 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 * ":let var += expr" assignment command.
1913 * ":let var -= expr" assignment command.
1914 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 */
1917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001918ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919{
1920 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 int var_count = 0;
1925 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001926 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001927 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929
Bram Moolenaardb552d602006-03-23 22:59:57 +00001930 argend = skip_var_list(arg, &var_count, &semicolon);
1931 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001932 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001933 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1934 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001935 expr = skipwhite(argend);
1936 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1937 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001939 /*
1940 * ":let" without "=": list variables
1941 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 if (*arg == '[')
1943 EMSG(_(e_invarg));
1944 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001946 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001947 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001949 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001950 list_glob_vars(&first);
1951 list_buf_vars(&first);
1952 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001954 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001955#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 list_script_vars(&first);
1957 list_func_vars(&first);
1958 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 eap->nextcmd = check_nextcmd(arg);
1961 }
1962 else
1963 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 op[0] = '=';
1965 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001966 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001968 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1969 op[0] = *expr; /* +=, -= or .= */
1970 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001971 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001972 else
1973 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (eap->skip)
1976 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001977 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (eap->skip)
1979 {
1980 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001981 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 --emsg_skip;
1983 }
1984 else if (i != FAIL)
1985 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001987 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 }
1991}
1992
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993/*
1994 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1995 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001996 * When "nextchars" is not NULL it points to a string with characters that
1997 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1998 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 * Returns OK or FAIL;
2000 */
2001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002002ex_let_vars(
2003 char_u *arg_start,
2004 typval_T *tv,
2005 int copy, /* copy values from "tv", don't move */
2006 int semicolon, /* from skip_var_list() */
2007 int var_count, /* from skip_var_list() */
2008 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009{
2010 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002011 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002013 listitem_T *item;
2014 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015
2016 if (*arg != '[')
2017 {
2018 /*
2019 * ":let var = expr" or ":for var in list"
2020 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002021 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 return FAIL;
2023 return OK;
2024 }
2025
2026 /*
2027 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2028 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002029 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 {
2031 EMSG(_(e_listreq));
2032 return FAIL;
2033 }
2034
2035 i = list_len(l);
2036 if (semicolon == 0 && var_count < i)
2037 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002038 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 return FAIL;
2040 }
2041 if (var_count - semicolon > i)
2042 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002043 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 return FAIL;
2045 }
2046
2047 item = l->lv_first;
2048 while (*arg != ']')
2049 {
2050 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 item = item->li_next;
2053 if (arg == NULL)
2054 return FAIL;
2055
2056 arg = skipwhite(arg);
2057 if (*arg == ';')
2058 {
2059 /* Put the rest of the list (may be empty) in the var after ';'.
2060 * Create a new list for this. */
2061 l = list_alloc();
2062 if (l == NULL)
2063 return FAIL;
2064 while (item != NULL)
2065 {
2066 list_append_tv(l, &item->li_tv);
2067 item = item->li_next;
2068 }
2069
2070 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002071 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072 ltv.vval.v_list = l;
2073 l->lv_refcount = 1;
2074
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2076 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002077 clear_tv(&ltv);
2078 if (arg == NULL)
2079 return FAIL;
2080 break;
2081 }
2082 else if (*arg != ',' && *arg != ']')
2083 {
2084 EMSG2(_(e_intern2), "ex_let_vars()");
2085 return FAIL;
2086 }
2087 }
2088
2089 return OK;
2090}
2091
2092/*
2093 * Skip over assignable variable "var" or list of variables "[var, var]".
2094 * Used for ":let varvar = expr" and ":for varvar in expr".
2095 * For "[var, var]" increment "*var_count" for each variable.
2096 * for "[var, var; var]" set "semicolon".
2097 * Return NULL for an error.
2098 */
2099 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002100skip_var_list(
2101 char_u *arg,
2102 int *var_count,
2103 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002104{
2105 char_u *p, *s;
2106
2107 if (*arg == '[')
2108 {
2109 /* "[var, var]": find the matching ']'. */
2110 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002111 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112 {
2113 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2114 s = skip_var_one(p);
2115 if (s == p)
2116 {
2117 EMSG2(_(e_invarg2), p);
2118 return NULL;
2119 }
2120 ++*var_count;
2121
2122 p = skipwhite(s);
2123 if (*p == ']')
2124 break;
2125 else if (*p == ';')
2126 {
2127 if (*semicolon == 1)
2128 {
2129 EMSG(_("Double ; in list of variables"));
2130 return NULL;
2131 }
2132 *semicolon = 1;
2133 }
2134 else if (*p != ',')
2135 {
2136 EMSG2(_(e_invarg2), p);
2137 return NULL;
2138 }
2139 }
2140 return p + 1;
2141 }
2142 else
2143 return skip_var_one(arg);
2144}
2145
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002146/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002147 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002148 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002149 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002150 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002152{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002153 if (*arg == '@' && arg[1] != NUL)
2154 return arg + 2;
2155 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2156 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002157}
2158
Bram Moolenaara7043832005-01-21 11:56:39 +00002159/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 * List variables for hashtab "ht" with prefix "prefix".
2161 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164list_hashtable_vars(
2165 hashtab_T *ht,
2166 char_u *prefix,
2167 int empty,
2168 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002169{
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 hashitem_T *hi;
2171 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 int todo;
2173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002174 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002175 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2176 {
2177 if (!HASHITEM_EMPTY(hi))
2178 {
2179 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 di = HI2DI(hi);
2181 if (empty || di->di_tv.v_type != VAR_STRING
2182 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002184 }
2185 }
2186}
2187
2188/*
2189 * List global variables.
2190 */
2191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002192list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002193{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
2197/*
2198 * List buffer variables.
2199 */
2200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002201list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002202{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002203 char_u numbuf[NUMBUFLEN];
2204
Bram Moolenaar429fa852013-04-15 12:27:36 +02002205 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002206 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207
2208 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002209 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2210 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002211}
2212
2213/*
2214 * List window variables.
2215 */
2216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002217list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002218{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002219 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002220 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002223#ifdef FEAT_WINDOWS
2224/*
2225 * List tab page variables.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002229{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002230 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002232}
2233#endif
2234
Bram Moolenaara7043832005-01-21 11:56:39 +00002235/*
2236 * List Vim variables.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242}
2243
2244/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002245 * List script-local variables, if there is a script.
2246 */
2247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002248list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002249{
2250 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2252 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002253}
2254
2255/*
2256 * List function variables, if there is a function.
2257 */
2258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002259list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002260{
2261 if (current_funccal != NULL)
2262 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002263 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002264}
2265
2266/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 * List variables in "arg".
2268 */
2269 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002270list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271{
2272 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 char_u *name_start;
2276 char_u *arg_subsc;
2277 char_u *tofree;
2278 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279
2280 while (!ends_excmd(*arg) && !got_int)
2281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002284 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2286 {
2287 emsg_severe = TRUE;
2288 EMSG(_(e_trailing));
2289 break;
2290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 /* get_name_len() takes care of expanding curly braces */
2295 name_start = name = arg;
2296 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2297 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 /* This is mainly to keep test 49 working: when expanding
2300 * curly braces fails overrule the exception error message. */
2301 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 emsg_severe = TRUE;
2304 EMSG2(_(e_invarg2), arg);
2305 break;
2306 }
2307 error = TRUE;
2308 }
2309 else
2310 {
2311 if (tofree != NULL)
2312 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002313 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002314 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 else
2316 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 /* handle d.key, l[idx], f(expr) */
2318 arg_subsc = arg;
2319 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002322 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002324 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002327 case 'g': list_glob_vars(first); break;
2328 case 'b': list_buf_vars(first); break;
2329 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002330#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002331 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002332#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002333 case 'v': list_vim_vars(first); break;
2334 case 's': list_script_vars(first); break;
2335 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336 default:
2337 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002338 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
2340 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002341 {
2342 char_u numbuf[NUMBUFLEN];
2343 char_u *tf;
2344 int c;
2345 char_u *s;
2346
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002347 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002348 c = *arg;
2349 *arg = NUL;
2350 list_one_var_a((char_u *)"",
2351 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002352 tv.v_type,
2353 s == NULL ? (char_u *)"" : s,
2354 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002355 *arg = c;
2356 vim_free(tf);
2357 }
2358 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002360 }
2361 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002362
2363 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002365
2366 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
2368
2369 return arg;
2370}
2371
2372/*
2373 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2374 * Returns a pointer to the char just after the var name.
2375 * Returns NULL if there is an error.
2376 */
2377 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002378ex_let_one(
2379 char_u *arg, /* points to variable name */
2380 typval_T *tv, /* value to assign to variable */
2381 int copy, /* copy value from "tv" */
2382 char_u *endchars, /* valid chars after variable name or NULL */
2383 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384{
2385 int c1;
2386 char_u *name;
2387 char_u *p;
2388 char_u *arg_end = NULL;
2389 int len;
2390 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392
2393 /*
2394 * ":let $VAR = expr": Set environment variable.
2395 */
2396 if (*arg == '$')
2397 {
2398 /* Find the end of the name. */
2399 ++arg;
2400 name = arg;
2401 len = get_env_len(&arg);
2402 if (len == 0)
2403 EMSG2(_(e_invarg2), name - 1);
2404 else
2405 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 if (op != NULL && (*op == '+' || *op == '-'))
2407 EMSG2(_(e_letwrong), op);
2408 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002409 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002410 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002411 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002412 {
2413 c1 = name[len];
2414 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002415 p = get_tv_string_chk(tv);
2416 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 {
2418 int mustfree = FALSE;
2419 char_u *s = vim_getenv(name, &mustfree);
2420
2421 if (s != NULL)
2422 {
2423 p = tofree = concat_str(s, p);
2424 if (mustfree)
2425 vim_free(s);
2426 }
2427 }
2428 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002429 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002430 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002431 if (STRICMP(name, "HOME") == 0)
2432 init_homedir();
2433 else if (didset_vim && STRICMP(name, "VIM") == 0)
2434 didset_vim = FALSE;
2435 else if (didset_vimruntime
2436 && STRICMP(name, "VIMRUNTIME") == 0)
2437 didset_vimruntime = FALSE;
2438 arg_end = arg;
2439 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 }
2443 }
2444 }
2445
2446 /*
2447 * ":let &option = expr": Set option value.
2448 * ":let &l:option = expr": Set local option value.
2449 * ":let &g:option = expr": Set global option value.
2450 */
2451 else if (*arg == '&')
2452 {
2453 /* Find the end of the name. */
2454 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002455 if (p == NULL || (endchars != NULL
2456 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002457 EMSG(_(e_letunexp));
2458 else
2459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 long n;
2461 int opt_type;
2462 long numval;
2463 char_u *stringval = NULL;
2464 char_u *s;
2465
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 c1 = *p;
2467 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002468
2469 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002470 s = get_tv_string_chk(tv); /* != NULL if number or string */
2471 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002472 {
2473 opt_type = get_option_value(arg, &numval,
2474 &stringval, opt_flags);
2475 if ((opt_type == 1 && *op == '.')
2476 || (opt_type == 0 && *op != '.'))
2477 EMSG2(_(e_letwrong), op);
2478 else
2479 {
2480 if (opt_type == 1) /* number */
2481 {
2482 if (*op == '+')
2483 n = numval + n;
2484 else
2485 n = numval - n;
2486 }
2487 else if (opt_type == 0 && stringval != NULL) /* string */
2488 {
2489 s = concat_str(stringval, s);
2490 vim_free(stringval);
2491 stringval = s;
2492 }
2493 }
2494 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 if (s != NULL)
2496 {
2497 set_option_value(arg, n, s, opt_flags);
2498 arg_end = p;
2499 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002500 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 }
2503 }
2504
2505 /*
2506 * ":let @r = expr": Set register contents.
2507 */
2508 else if (*arg == '@')
2509 {
2510 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 if (op != NULL && (*op == '+' || *op == '-'))
2512 EMSG2(_(e_letwrong), op);
2513 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002514 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 EMSG(_(e_letunexp));
2516 else
2517 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002519 char_u *s;
2520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 p = get_tv_string_chk(tv);
2522 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002523 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002524 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002525 if (s != NULL)
2526 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002528 vim_free(s);
2529 }
2530 }
2531 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002534 arg_end = arg + 1;
2535 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
2538 }
2539
2540 /*
2541 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002544 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002548 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2552 EMSG(_(e_letunexp));
2553 else
2554 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002555 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 arg_end = p;
2557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 }
2561
2562 else
2563 EMSG2(_(e_invarg2), arg);
2564
2565 return arg_end;
2566}
2567
2568/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002569 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2570 */
2571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002572check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002573{
2574 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2575 {
2576 EMSG2(_(e_readonlyvar), arg);
2577 return TRUE;
2578 }
2579 return FALSE;
2580}
2581
2582/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 * Get an lval: variable, Dict item or List item that can be assigned a value
2584 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2585 * "name.key", "name.key[expr]" etc.
2586 * Indexing only works if "name" is an existing List or Dictionary.
2587 * "name" points to the start of the name.
2588 * If "rettv" is not NULL it points to the value to be assigned.
2589 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2590 * wrong; must end in space or cmd separator.
2591 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002592 * flags:
2593 * GLV_QUIET: do not give error messages
2594 * GLV_NO_AUTOLOAD: do not use script autoloading
2595 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002597 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 */
2600 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002601get_lval(
2602 char_u *name,
2603 typval_T *rettv,
2604 lval_T *lp,
2605 int unlet,
2606 int skip,
2607 int flags, /* GLV_ values */
2608 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 char_u *expr_start, *expr_end;
2612 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 dictitem_T *v;
2614 typval_T var1;
2615 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002617 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002619 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002620 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002621 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002622
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002624 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625
2626 if (skip)
2627 {
2628 /* When skipping just find the end of the name. */
2629 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002630 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 }
2632
2633 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002634 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 if (expr_start != NULL)
2636 {
2637 /* Don't expand the name when we already know there is an error. */
2638 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2639 && *p != '[' && *p != '.')
2640 {
2641 EMSG(_(e_trailing));
2642 return NULL;
2643 }
2644
2645 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2646 if (lp->ll_exp_name == NULL)
2647 {
2648 /* Report an invalid expression in braces, unless the
2649 * expression evaluation has been cancelled due to an
2650 * aborting error, an interrupt, or an exception. */
2651 if (!aborting() && !quiet)
2652 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002653 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 EMSG2(_(e_invarg2), name);
2655 return NULL;
2656 }
2657 }
2658 lp->ll_name = lp->ll_exp_name;
2659 }
2660 else
2661 lp->ll_name = name;
2662
2663 /* Without [idx] or .key we are done. */
2664 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2665 return p;
2666
2667 cc = *p;
2668 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002669 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (v == NULL && !quiet)
2671 EMSG2(_(e_undefvar), lp->ll_name);
2672 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 if (v == NULL)
2674 return NULL;
2675
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 /*
2677 * Loop until no more [idx] or .key is following.
2678 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002679 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2683 && !(lp->ll_tv->v_type == VAR_DICT
2684 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_("E689: Can only index a List or Dictionary"));
2688 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002689 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_("E708: [:] must come last"));
2694 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 len = -1;
2698 if (*p == '.')
2699 {
2700 key = p + 1;
2701 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2702 ;
2703 if (len == 0)
2704 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (!quiet)
2706 EMSG(_(e_emptykey));
2707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709 p = key + len;
2710 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 else
2712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 if (*p == ':')
2716 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 else
2718 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002719 empty1 = FALSE;
2720 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002722 if (get_tv_string_chk(&var1) == NULL)
2723 {
2724 /* not a number or string */
2725 clear_tv(&var1);
2726 return NULL;
2727 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 }
2729
2730 /* Optionally get the second index [ :expr]. */
2731 if (*p == ':')
2732 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002736 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002737 if (!empty1)
2738 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002740 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 if (rettv != NULL && (rettv->v_type != VAR_LIST
2742 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002744 if (!quiet)
2745 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002746 if (!empty1)
2747 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002749 }
2750 p = skipwhite(p + 1);
2751 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
2754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2757 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (!empty1)
2759 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 if (get_tv_string_chk(&var2) == NULL)
2763 {
2764 /* not a number or string */
2765 if (!empty1)
2766 clear_tv(&var1);
2767 clear_tv(&var2);
2768 return NULL;
2769 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002772 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002775
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002777 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (!quiet)
2779 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 if (!empty1)
2781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 }
2786
2787 /* Skip to past ']'. */
2788 ++p;
2789 }
2790
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 {
2793 if (len == -1)
2794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002795 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002796 key = get_tv_string_chk(&var1); /* is number or string */
2797 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002799 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002800 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 }
2802 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 lp->ll_list = NULL;
2804 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002805 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002807 /* When assigning to a scope dictionary check that a function and
2808 * variable name is valid (only variable name unless it is l: or
2809 * g: dictionary). Disallow overwriting a builtin function. */
2810 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002812 int prevval;
2813 int wrong;
2814
2815 if (len != -1)
2816 {
2817 prevval = key[len];
2818 key[len] = NUL;
2819 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002820 else
2821 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2823 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002824 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002825 || !valid_varname(key);
2826 if (len != -1)
2827 key[len] = prevval;
2828 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002829 return NULL;
2830 }
2831
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 /* Can't add "v:" variable. */
2835 if (lp->ll_dict == &vimvardict)
2836 {
2837 EMSG2(_(e_illvar), name);
2838 return NULL;
2839 }
2840
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002841 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002845 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (len == -1)
2847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002852 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 if (len == -1)
2855 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 p = NULL;
2858 break;
2859 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002860 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002861 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002862 return NULL;
2863
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 if (len == -1)
2865 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 }
2868 else
2869 {
2870 /*
2871 * Get the number and item for the only or first index of the List.
2872 */
2873 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002874 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002875 else
2876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002877 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var1);
2879 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 lp->ll_list = lp->ll_tv->vval.v_list;
2882 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2883 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002885 if (lp->ll_n1 < 0)
2886 {
2887 lp->ll_n1 = 0;
2888 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2889 }
2890 }
2891 if (lp->ll_li == NULL)
2892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002895 if (!quiet)
2896 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 }
2899
2900 /*
2901 * May need to find the item or absolute index for the second
2902 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 * When no index given: "lp->ll_empty2" is TRUE.
2904 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002905 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002908 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002910 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002911 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002913 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002914 {
2915 if (!quiet)
2916 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002920 }
2921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2923 if (lp->ll_n1 < 0)
2924 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2925 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002926 {
2927 if (!quiet)
2928 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002930 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002931 }
2932
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002934 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002935 }
2936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 return p;
2938}
2939
2940/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002941 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942 */
2943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002944clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945{
2946 vim_free(lp->ll_exp_name);
2947 vim_free(lp->ll_newkey);
2948}
2949
2950/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002951 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002953 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 */
2955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002956set_var_lval(
2957 lval_T *lp,
2958 char_u *endp,
2959 typval_T *rettv,
2960 int copy,
2961 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962{
2963 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 listitem_T *ri;
2965 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002966
2967 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 cc = *endp;
2972 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002973 if (op != NULL && *op != '=')
2974 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002975 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002977 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002978 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002979 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002980 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002982 if ((di == NULL
2983 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2984 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2985 FALSE)))
2986 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002987 set_var(lp->ll_name, &tv, FALSE);
2988 clear_tv(&tv);
2989 }
2990 }
2991 else
2992 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002993 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002996 else if (tv_check_lock(lp->ll_newkey == NULL
2997 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002998 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002999 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003000 else if (lp->ll_range)
3001 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003002 listitem_T *ll_li = lp->ll_li;
3003 int ll_n1 = lp->ll_n1;
3004
3005 /*
3006 * Check whether any of the list items is locked
3007 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003008 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003009 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003010 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003011 return;
3012 ri = ri->li_next;
3013 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3014 break;
3015 ll_li = ll_li->li_next;
3016 ++ll_n1;
3017 }
3018
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 /*
3020 * Assign the List values to the list items.
3021 */
3022 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 if (op != NULL && *op != '=')
3025 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3026 else
3027 {
3028 clear_tv(&lp->ll_li->li_tv);
3029 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3030 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003031 ri = ri->li_next;
3032 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3033 break;
3034 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003035 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003036 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003037 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003038 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 ri = NULL;
3040 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003041 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003042 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003043 lp->ll_li = lp->ll_li->li_next;
3044 ++lp->ll_n1;
3045 }
3046 if (ri != NULL)
3047 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003048 else if (lp->ll_empty2
3049 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 : lp->ll_n1 != lp->ll_n2)
3051 EMSG(_("E711: List value has not enough items"));
3052 }
3053 else
3054 {
3055 /*
3056 * Assign to a List or Dictionary item.
3057 */
3058 if (lp->ll_newkey != NULL)
3059 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 if (op != NULL && *op != '=')
3061 {
3062 EMSG2(_(e_letwrong), op);
3063 return;
3064 }
3065
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003067 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003068 if (di == NULL)
3069 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003070 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3071 {
3072 vim_free(di);
3073 return;
3074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003075 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003076 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003077 else if (op != NULL && *op != '=')
3078 {
3079 tv_op(lp->ll_tv, rettv, op);
3080 return;
3081 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003084
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 /*
3086 * Assign the value to the variable or list item.
3087 */
3088 if (copy)
3089 copy_tv(rettv, lp->ll_tv);
3090 else
3091 {
3092 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003093 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003094 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003095 }
3096 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003097}
3098
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003099/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3101 * Returns OK or FAIL.
3102 */
3103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105{
3106 long n;
3107 char_u numbuf[NUMBUFLEN];
3108 char_u *s;
3109
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003110 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3111 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3112 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003113 {
3114 switch (tv1->v_type)
3115 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003116 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003117 case VAR_DICT:
3118 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003119 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003120 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003121 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003122 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 break;
3124
3125 case VAR_LIST:
3126 if (*op != '+' || tv2->v_type != VAR_LIST)
3127 break;
3128 /* List += List */
3129 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3130 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3131 return OK;
3132
3133 case VAR_NUMBER:
3134 case VAR_STRING:
3135 if (tv2->v_type == VAR_LIST)
3136 break;
3137 if (*op == '+' || *op == '-')
3138 {
3139 /* nr += nr or nr -= nr*/
3140 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003141#ifdef FEAT_FLOAT
3142 if (tv2->v_type == VAR_FLOAT)
3143 {
3144 float_T f = n;
3145
3146 if (*op == '+')
3147 f += tv2->vval.v_float;
3148 else
3149 f -= tv2->vval.v_float;
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_FLOAT;
3152 tv1->vval.v_float = f;
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003155#endif
3156 {
3157 if (*op == '+')
3158 n += get_tv_number(tv2);
3159 else
3160 n -= get_tv_number(tv2);
3161 clear_tv(tv1);
3162 tv1->v_type = VAR_NUMBER;
3163 tv1->vval.v_number = n;
3164 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003165 }
3166 else
3167 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168 if (tv2->v_type == VAR_FLOAT)
3169 break;
3170
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003171 /* str .= str */
3172 s = get_tv_string(tv1);
3173 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3174 clear_tv(tv1);
3175 tv1->v_type = VAR_STRING;
3176 tv1->vval.v_string = s;
3177 }
3178 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003179
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003181#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003182 {
3183 float_T f;
3184
3185 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3186 && tv2->v_type != VAR_NUMBER
3187 && tv2->v_type != VAR_STRING))
3188 break;
3189 if (tv2->v_type == VAR_FLOAT)
3190 f = tv2->vval.v_float;
3191 else
3192 f = get_tv_number(tv2);
3193 if (*op == '+')
3194 tv1->vval.v_float += f;
3195 else
3196 tv1->vval.v_float -= f;
3197 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003198#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003199 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003200 }
3201 }
3202
3203 EMSG2(_(e_letwrong), op);
3204 return FAIL;
3205}
3206
3207/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 * Add a watcher to a list.
3209 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
3213 lw->lw_next = l->lv_watch;
3214 l->lv_watch = lw;
3215}
3216
3217/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003218 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219 * No warning when it isn't found...
3220 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
3226 lwp = &l->lv_watch;
3227 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3228 {
3229 if (lw == lwrem)
3230 {
3231 *lwp = lw->lw_next;
3232 break;
3233 }
3234 lwp = &lw->lw_next;
3235 }
3236}
3237
3238/*
3239 * Just before removing an item from a list: advance watchers to the next
3240 * item.
3241 */
3242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003243list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244{
Bram Moolenaar33570922005-01-25 22:26:29 +00003245 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246
3247 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3248 if (lw->lw_item == item)
3249 lw->lw_item = item->li_next;
3250}
3251
3252/*
3253 * Evaluate the expression used in a ":for var in expr" command.
3254 * "arg" points to "var".
3255 * Set "*errp" to TRUE for an error, FALSE otherwise;
3256 * Return a pointer that holds the info. Null when there is an error.
3257 */
3258 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003259eval_for_line(
3260 char_u *arg,
3261 int *errp,
3262 char_u **nextcmdp,
3263 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264{
Bram Moolenaar33570922005-01-25 22:26:29 +00003265 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003267 typval_T tv;
3268 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003269
3270 *errp = TRUE; /* default: there is an error */
3271
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 if (fi == NULL)
3274 return NULL;
3275
3276 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3277 if (expr == NULL)
3278 return fi;
3279
3280 expr = skipwhite(expr);
3281 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3282 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003283 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 return fi;
3285 }
3286
3287 if (skip)
3288 ++emsg_skip;
3289 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3290 {
3291 *errp = FALSE;
3292 if (!skip)
3293 {
3294 l = tv.vval.v_list;
3295 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003297 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003298 clear_tv(&tv);
3299 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003300 else
3301 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003302 /* No need to increment the refcount, it's already set for the
3303 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003304 fi->fi_list = l;
3305 list_add_watch(l, &fi->fi_lw);
3306 fi->fi_lw.lw_item = l->lv_first;
3307 }
3308 }
3309 }
3310 if (skip)
3311 --emsg_skip;
3312
3313 return fi;
3314}
3315
3316/*
3317 * Use the first item in a ":for" list. Advance to the next.
3318 * Assign the values to the variable (list). "arg" points to the first one.
3319 * Return TRUE when a valid item was found, FALSE when at end of list or
3320 * something wrong.
3321 */
3322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003323next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003324{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003325 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003327 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328
3329 item = fi->fi_lw.lw_item;
3330 if (item == NULL)
3331 result = FALSE;
3332 else
3333 {
3334 fi->fi_lw.lw_item = item->li_next;
3335 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3336 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3337 }
3338 return result;
3339}
3340
3341/*
3342 * Free the structure used to store info used by ":for".
3343 */
3344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003345free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346{
Bram Moolenaar33570922005-01-25 22:26:29 +00003347 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003349 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003350 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003352 list_unref(fi->fi_list);
3353 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 vim_free(fi);
3355}
3356
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3358
3359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360set_context_for_expression(
3361 expand_T *xp,
3362 char_u *arg,
3363 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 int got_eq = FALSE;
3366 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003367 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 if (cmdidx == CMD_let)
3370 {
3371 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003372 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003373 {
3374 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003375 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003376 {
3377 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003379 if (vim_iswhite(*p))
3380 break;
3381 }
3382 return;
3383 }
3384 }
3385 else
3386 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3387 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 while ((xp->xp_pattern = vim_strpbrk(arg,
3389 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3390 {
3391 c = *xp->xp_pattern;
3392 if (c == '&')
3393 {
3394 c = xp->xp_pattern[1];
3395 if (c == '&')
3396 {
3397 ++xp->xp_pattern;
3398 xp->xp_context = cmdidx != CMD_let || got_eq
3399 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3400 }
3401 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003402 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003404 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3405 xp->xp_pattern += 2;
3406
3407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 }
3409 else if (c == '$')
3410 {
3411 /* environment variable */
3412 xp->xp_context = EXPAND_ENV_VARS;
3413 }
3414 else if (c == '=')
3415 {
3416 got_eq = TRUE;
3417 xp->xp_context = EXPAND_EXPRESSION;
3418 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003419 else if (c == '#'
3420 && xp->xp_context == EXPAND_EXPRESSION)
3421 {
3422 /* Autoload function/variable contains '#'. */
3423 break;
3424 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003425 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 && xp->xp_context == EXPAND_FUNCTIONS
3427 && vim_strchr(xp->xp_pattern, '(') == NULL)
3428 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003429 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 break;
3431 }
3432 else if (cmdidx != CMD_let || got_eq)
3433 {
3434 if (c == '"') /* string */
3435 {
3436 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3437 if (c == '\\' && xp->xp_pattern[1] != NUL)
3438 ++xp->xp_pattern;
3439 xp->xp_context = EXPAND_NOTHING;
3440 }
3441 else if (c == '\'') /* literal string */
3442 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003443 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3445 /* skip */ ;
3446 xp->xp_context = EXPAND_NOTHING;
3447 }
3448 else if (c == '|')
3449 {
3450 if (xp->xp_pattern[1] == '|')
3451 {
3452 ++xp->xp_pattern;
3453 xp->xp_context = EXPAND_EXPRESSION;
3454 }
3455 else
3456 xp->xp_context = EXPAND_COMMANDS;
3457 }
3458 else
3459 xp->xp_context = EXPAND_EXPRESSION;
3460 }
3461 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003462 /* Doesn't look like something valid, expand as an expression
3463 * anyway. */
3464 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 arg = xp->xp_pattern;
3466 if (*arg != NUL)
3467 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3468 /* skip */ ;
3469 }
3470 xp->xp_pattern = arg;
3471}
3472
3473#endif /* FEAT_CMDL_COMPL */
3474
3475/*
3476 * ":1,25call func(arg1, arg2)" function call.
3477 */
3478 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 char_u *arg = eap->arg;
3482 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003484 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 linenr_T lnum;
3488 int doesrange;
3489 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003490 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003491 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003493 if (eap->skip)
3494 {
3495 /* trans_function_name() doesn't work well when skipping, use eval0()
3496 * instead to skip to any following command, e.g. for:
3497 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003498 ++emsg_skip;
3499 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3500 clear_tv(&rettv);
3501 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003502 return;
3503 }
3504
Bram Moolenaar65639032016-03-16 21:40:30 +01003505 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003506 if (fudi.fd_newkey != NULL)
3507 {
3508 /* Still need to give an error message for missing key. */
3509 EMSG2(_(e_dictkey), fudi.fd_newkey);
3510 vim_free(fudi.fd_newkey);
3511 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003512 if (tofree == NULL)
3513 return;
3514
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003515 /* Increase refcount on dictionary, it could get deleted when evaluating
3516 * the arguments. */
3517 if (fudi.fd_dict != NULL)
3518 ++fudi.fd_dict->dv_refcount;
3519
Bram Moolenaar65639032016-03-16 21:40:30 +01003520 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3521 * contents. For VAR_PARTIAL get its partial, unless we already have one
3522 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003523 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003524 name = deref_func_name(tofree, &len,
3525 partial != NULL ? NULL : &partial, FALSE);
3526
Bram Moolenaar532c7802005-01-27 14:44:31 +00003527 /* Skip white space to allow ":call func ()". Not good, but required for
3528 * backward compatibility. */
3529 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003530 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 if (*startarg != '(')
3533 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003534 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 goto end;
3536 }
3537
3538 /*
3539 * When skipping, evaluate the function once, to find the end of the
3540 * arguments.
3541 * When the function takes a range, this is discovered after the first
3542 * call, and the loop is broken.
3543 */
3544 if (eap->skip)
3545 {
3546 ++emsg_skip;
3547 lnum = eap->line2; /* do it once, also with an invalid range */
3548 }
3549 else
3550 lnum = eap->line1;
3551 for ( ; lnum <= eap->line2; ++lnum)
3552 {
3553 if (!eap->skip && eap->addr_count > 0)
3554 {
3555 curwin->w_cursor.lnum = lnum;
3556 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003557#ifdef FEAT_VIRTUALEDIT
3558 curwin->w_cursor.coladd = 0;
3559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
3561 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003562 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003563 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003564 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 {
3566 failed = TRUE;
3567 break;
3568 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003569
3570 /* Handle a function returning a Funcref, Dictionary or List. */
3571 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3572 {
3573 failed = TRUE;
3574 break;
3575 }
3576
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003577 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (doesrange || eap->skip)
3579 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003582 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003583 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003584 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 if (aborting())
3586 break;
3587 }
3588 if (eap->skip)
3589 --emsg_skip;
3590
3591 if (!failed)
3592 {
3593 /* Check for trailing illegal characters and a following command. */
3594 if (!ends_excmd(*arg))
3595 {
3596 emsg_severe = TRUE;
3597 EMSG(_(e_trailing));
3598 }
3599 else
3600 eap->nextcmd = check_nextcmd(arg);
3601 }
3602
3603end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003604 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003605 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606}
3607
3608/*
3609 * ":unlet[!] var1 ... " command.
3610 */
3611 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 ex_unletlock(eap, eap->arg, 0);
3615}
3616
3617/*
3618 * ":lockvar" and ":unlockvar" commands
3619 */
3620 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003621ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624 int deep = 2;
3625
3626 if (eap->forceit)
3627 deep = -1;
3628 else if (vim_isdigit(*arg))
3629 {
3630 deep = getdigits(&arg);
3631 arg = skipwhite(arg);
3632 }
3633
3634 ex_unletlock(eap, arg, deep);
3635}
3636
3637/*
3638 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3639 */
3640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003641ex_unletlock(
3642 exarg_T *eap,
3643 char_u *argstart,
3644 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645{
3646 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 do
3652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003653 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003654 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003655 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003656 if (lv.ll_name == NULL)
3657 error = TRUE; /* error but continue parsing */
3658 if (name_end == NULL || (!vim_iswhite(*name_end)
3659 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003661 if (name_end != NULL)
3662 {
3663 emsg_severe = TRUE;
3664 EMSG(_(e_trailing));
3665 }
3666 if (!(eap->skip || error))
3667 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 break;
3669 }
3670
3671 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 {
3673 if (eap->cmdidx == CMD_unlet)
3674 {
3675 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3676 error = TRUE;
3677 }
3678 else
3679 {
3680 if (do_lock_var(&lv, name_end, deep,
3681 eap->cmdidx == CMD_lockvar) == FAIL)
3682 error = TRUE;
3683 }
3684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 if (!eap->skip)
3687 clear_lval(&lv);
3688
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 arg = skipwhite(name_end);
3690 } while (!ends_excmd(*arg));
3691
3692 eap->nextcmd = check_nextcmd(arg);
3693}
3694
Bram Moolenaar8c711452005-01-14 21:53:12 +00003695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003696do_unlet_var(
3697 lval_T *lp,
3698 char_u *name_end,
3699 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003700{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 int ret = OK;
3702 int cc;
3703
3704 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003705 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706 cc = *name_end;
3707 *name_end = NUL;
3708
3709 /* Normal name or expanded name. */
3710 if (check_changedtick(lp->ll_name))
3711 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003712 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003716 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003717 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003718 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003719 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003720 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 else if (lp->ll_range)
3722 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003723 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003724 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003725 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003726
3727 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3728 {
3729 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003730 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003731 return FAIL;
3732 ll_li = li;
3733 ++ll_n1;
3734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003735
3736 /* Delete a range of List items. */
3737 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3738 {
3739 li = lp->ll_li->li_next;
3740 listitem_remove(lp->ll_list, lp->ll_li);
3741 lp->ll_li = li;
3742 ++lp->ll_n1;
3743 }
3744 }
3745 else
3746 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003747 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003748 /* unlet a List item. */
3749 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003751 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003752 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003753 }
3754
3755 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003756}
3757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758/*
3759 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003760 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 */
3762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003763do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
Bram Moolenaar33570922005-01-25 22:26:29 +00003765 hashtab_T *ht;
3766 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003768 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003769 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
Bram Moolenaar33570922005-01-25 22:26:29 +00003771 ht = find_var_ht(name, &varname);
3772 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003774 if (ht == &globvarht)
3775 d = &globvardict;
3776 else if (current_funccal != NULL
3777 && ht == &current_funccal->l_vars.dv_hashtab)
3778 d = &current_funccal->l_vars;
3779 else if (ht == &compat_hashtab)
3780 d = &vimvardict;
3781 else
3782 {
3783 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3784 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3785 }
3786 if (d == NULL)
3787 {
3788 EMSG2(_(e_intern2), "do_unlet()");
3789 return FAIL;
3790 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 hi = hash_find(ht, varname);
3792 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003793 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003794 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003795 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003796 || var_check_ro(di->di_flags, name, FALSE)
3797 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003798 return FAIL;
3799
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 delete_var(ht, hi);
3801 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804 if (forceit)
3805 return OK;
3806 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 return FAIL;
3808}
3809
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810/*
3811 * Lock or unlock variable indicated by "lp".
3812 * "deep" is the levels to go (-1 for unlimited);
3813 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3814 */
3815 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003816do_lock_var(
3817 lval_T *lp,
3818 char_u *name_end,
3819 int deep,
3820 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003821{
3822 int ret = OK;
3823 int cc;
3824 dictitem_T *di;
3825
3826 if (deep == 0) /* nothing to do */
3827 return OK;
3828
3829 if (lp->ll_tv == NULL)
3830 {
3831 cc = *name_end;
3832 *name_end = NUL;
3833
3834 /* Normal name or expanded name. */
3835 if (check_changedtick(lp->ll_name))
3836 ret = FAIL;
3837 else
3838 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003839 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003840 if (di == NULL)
3841 ret = FAIL;
3842 else
3843 {
3844 if (lock)
3845 di->di_flags |= DI_FLAGS_LOCK;
3846 else
3847 di->di_flags &= ~DI_FLAGS_LOCK;
3848 item_lock(&di->di_tv, deep, lock);
3849 }
3850 }
3851 *name_end = cc;
3852 }
3853 else if (lp->ll_range)
3854 {
3855 listitem_T *li = lp->ll_li;
3856
3857 /* (un)lock a range of List items. */
3858 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3859 {
3860 item_lock(&li->li_tv, deep, lock);
3861 li = li->li_next;
3862 ++lp->ll_n1;
3863 }
3864 }
3865 else if (lp->ll_list != NULL)
3866 /* (un)lock a List item. */
3867 item_lock(&lp->ll_li->li_tv, deep, lock);
3868 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003869 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003870 item_lock(&lp->ll_di->di_tv, deep, lock);
3871
3872 return ret;
3873}
3874
3875/*
3876 * Lock or unlock an item. "deep" is nr of levels to go.
3877 */
3878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003879item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003880{
3881 static int recurse = 0;
3882 list_T *l;
3883 listitem_T *li;
3884 dict_T *d;
3885 hashitem_T *hi;
3886 int todo;
3887
3888 if (recurse >= DICT_MAXNEST)
3889 {
3890 EMSG(_("E743: variable nested too deep for (un)lock"));
3891 return;
3892 }
3893 if (deep == 0)
3894 return;
3895 ++recurse;
3896
3897 /* lock/unlock the item itself */
3898 if (lock)
3899 tv->v_lock |= VAR_LOCKED;
3900 else
3901 tv->v_lock &= ~VAR_LOCKED;
3902
3903 switch (tv->v_type)
3904 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003905 case VAR_UNKNOWN:
3906 case VAR_NUMBER:
3907 case VAR_STRING:
3908 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003909 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003910 case VAR_FLOAT:
3911 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003912 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003913 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003914 break;
3915
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003916 case VAR_LIST:
3917 if ((l = tv->vval.v_list) != NULL)
3918 {
3919 if (lock)
3920 l->lv_lock |= VAR_LOCKED;
3921 else
3922 l->lv_lock &= ~VAR_LOCKED;
3923 if (deep < 0 || deep > 1)
3924 /* recursive: lock/unlock the items the List contains */
3925 for (li = l->lv_first; li != NULL; li = li->li_next)
3926 item_lock(&li->li_tv, deep - 1, lock);
3927 }
3928 break;
3929 case VAR_DICT:
3930 if ((d = tv->vval.v_dict) != NULL)
3931 {
3932 if (lock)
3933 d->dv_lock |= VAR_LOCKED;
3934 else
3935 d->dv_lock &= ~VAR_LOCKED;
3936 if (deep < 0 || deep > 1)
3937 {
3938 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003939 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003940 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3941 {
3942 if (!HASHITEM_EMPTY(hi))
3943 {
3944 --todo;
3945 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3946 }
3947 }
3948 }
3949 }
3950 }
3951 --recurse;
3952}
3953
Bram Moolenaara40058a2005-07-11 22:42:07 +00003954/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003955 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3956 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003957 */
3958 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003960{
3961 return (tv->v_lock & VAR_LOCKED)
3962 || (tv->v_type == VAR_LIST
3963 && tv->vval.v_list != NULL
3964 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3965 || (tv->v_type == VAR_DICT
3966 && tv->vval.v_dict != NULL
3967 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3968}
3969
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3971/*
3972 * Delete all "menutrans_" variables.
3973 */
3974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003975del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
Bram Moolenaar33570922005-01-25 22:26:29 +00003977 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003981 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003983 {
3984 if (!HASHITEM_EMPTY(hi))
3985 {
3986 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003987 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3988 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003989 }
3990 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003991 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992}
3993#endif
3994
3995#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3996
3997/*
3998 * Local string buffer for the next two functions to store a variable name
3999 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4000 * get_user_var_name().
4001 */
4002
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004003static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004
4005static char_u *varnamebuf = NULL;
4006static int varnamebuflen = 0;
4007
4008/*
4009 * Function to concatenate a prefix and a variable name.
4010 */
4011 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004012cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
4014 int len;
4015
4016 len = (int)STRLEN(name) + 3;
4017 if (len > varnamebuflen)
4018 {
4019 vim_free(varnamebuf);
4020 len += 10; /* some additional space */
4021 varnamebuf = alloc(len);
4022 if (varnamebuf == NULL)
4023 {
4024 varnamebuflen = 0;
4025 return NULL;
4026 }
4027 varnamebuflen = len;
4028 }
4029 *varnamebuf = prefix;
4030 varnamebuf[1] = ':';
4031 STRCPY(varnamebuf + 2, name);
4032 return varnamebuf;
4033}
4034
4035/*
4036 * Function given to ExpandGeneric() to obtain the list of user defined
4037 * (global/buffer/window/built-in) variable names.
4038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004040get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004042 static long_u gdone;
4043 static long_u bdone;
4044 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004045#ifdef FEAT_WINDOWS
4046 static long_u tdone;
4047#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004048 static int vidx;
4049 static hashitem_T *hi;
4050 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051
4052 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004055#ifdef FEAT_WINDOWS
4056 tdone = 0;
4057#endif
4058 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004059
4060 /* Global variables */
4061 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004063 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004065 else
4066 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 while (HASHITEM_EMPTY(hi))
4068 ++hi;
4069 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4070 return cat_prefix_varname('g', hi->hi_key);
4071 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004073
4074 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004075 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004078 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004080 else
4081 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004082 while (HASHITEM_EMPTY(hi))
4083 ++hi;
4084 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004088 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 return (char_u *)"b:changedtick";
4090 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004091
4092 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004093 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004096 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004097 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004098 else
4099 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004100 while (HASHITEM_EMPTY(hi))
4101 ++hi;
4102 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004104
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004105#ifdef FEAT_WINDOWS
4106 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004107 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004108 if (tdone < ht->ht_used)
4109 {
4110 if (tdone++ == 0)
4111 hi = ht->ht_array;
4112 else
4113 ++hi;
4114 while (HASHITEM_EMPTY(hi))
4115 ++hi;
4116 return cat_prefix_varname('t', hi->hi_key);
4117 }
4118#endif
4119
Bram Moolenaar33570922005-01-25 22:26:29 +00004120 /* v: variables */
4121 if (vidx < VV_LEN)
4122 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
4124 vim_free(varnamebuf);
4125 varnamebuf = NULL;
4126 varnamebuflen = 0;
4127 return NULL;
4128}
4129
4130#endif /* FEAT_CMDL_COMPL */
4131
4132/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004133 * Return TRUE if "pat" matches "text".
4134 * Does not use 'cpo' and always uses 'magic'.
4135 */
4136 static int
4137pattern_match(char_u *pat, char_u *text, int ic)
4138{
4139 int matches = FALSE;
4140 char_u *save_cpo;
4141 regmatch_T regmatch;
4142
4143 /* avoid 'l' flag in 'cpoptions' */
4144 save_cpo = p_cpo;
4145 p_cpo = (char_u *)"";
4146 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4147 if (regmatch.regprog != NULL)
4148 {
4149 regmatch.rm_ic = ic;
4150 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4151 vim_regfree(regmatch.regprog);
4152 }
4153 p_cpo = save_cpo;
4154 return matches;
4155}
4156
4157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 * types for expressions.
4159 */
4160typedef enum
4161{
4162 TYPE_UNKNOWN = 0
4163 , TYPE_EQUAL /* == */
4164 , TYPE_NEQUAL /* != */
4165 , TYPE_GREATER /* > */
4166 , TYPE_GEQUAL /* >= */
4167 , TYPE_SMALLER /* < */
4168 , TYPE_SEQUAL /* <= */
4169 , TYPE_MATCH /* =~ */
4170 , TYPE_NOMATCH /* !~ */
4171} exptype_T;
4172
4173/*
4174 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004175 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4177 */
4178
4179/*
4180 * Handle zero level expression.
4181 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004183 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 * Return OK or FAIL.
4185 */
4186 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004187eval0(
4188 char_u *arg,
4189 typval_T *rettv,
4190 char_u **nextcmd,
4191 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192{
4193 int ret;
4194 char_u *p;
4195
4196 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 if (ret == FAIL || !ends_excmd(*p))
4199 {
4200 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004201 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /*
4203 * Report the invalid expression unless the expression evaluation has
4204 * been cancelled due to an aborting error, an interrupt, or an
4205 * exception.
4206 */
4207 if (!aborting())
4208 EMSG2(_(e_invexpr2), arg);
4209 ret = FAIL;
4210 }
4211 if (nextcmd != NULL)
4212 *nextcmd = check_nextcmd(p);
4213
4214 return ret;
4215}
4216
4217/*
4218 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004219 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 *
4221 * "arg" must point to the first non-white of the expression.
4222 * "arg" is advanced to the next non-white after the recognized expression.
4223 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004224 * Note: "rettv.v_lock" is not set.
4225 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 * Return OK or FAIL.
4227 */
4228 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004229eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004232 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
4234 /*
4235 * Get the first variable.
4236 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 return FAIL;
4239
4240 if ((*arg)[0] == '?')
4241 {
4242 result = FALSE;
4243 if (evaluate)
4244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004245 int error = FALSE;
4246
4247 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004249 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004250 if (error)
4251 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 }
4253
4254 /*
4255 * Get the second variable.
4256 */
4257 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004258 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 return FAIL;
4260
4261 /*
4262 * Check for the ":".
4263 */
4264 if ((*arg)[0] != ':')
4265 {
4266 EMSG(_("E109: Missing ':' after '?'"));
4267 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FAIL;
4270 }
4271
4272 /*
4273 * Get the third variable.
4274 */
4275 *arg = skipwhite(*arg + 1);
4276 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4277 {
4278 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004279 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 return FAIL;
4281 }
4282 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 }
4285
4286 return OK;
4287}
4288
4289/*
4290 * Handle first level expression:
4291 * expr2 || expr2 || expr2 logical OR
4292 *
4293 * "arg" must point to the first non-white of the expression.
4294 * "arg" is advanced to the next non-white after the recognized expression.
4295 *
4296 * Return OK or FAIL.
4297 */
4298 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004299eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 long result;
4303 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 /*
4307 * Get the first variable.
4308 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004309 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 return FAIL;
4311
4312 /*
4313 * Repeat until there is no following "||".
4314 */
4315 first = TRUE;
4316 result = FALSE;
4317 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4318 {
4319 if (evaluate && first)
4320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004321 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004323 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (error)
4325 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 first = FALSE;
4327 }
4328
4329 /*
4330 * Get the second variable.
4331 */
4332 *arg = skipwhite(*arg + 2);
4333 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4334 return FAIL;
4335
4336 /*
4337 * Compute the result.
4338 */
4339 if (evaluate && !result)
4340 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004341 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004343 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004344 if (error)
4345 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 if (evaluate)
4348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004349 rettv->v_type = VAR_NUMBER;
4350 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 }
4353
4354 return OK;
4355}
4356
4357/*
4358 * Handle second level expression:
4359 * expr3 && expr3 && expr3 logical AND
4360 *
4361 * "arg" must point to the first non-white of the expression.
4362 * "arg" is advanced to the next non-white after the recognized expression.
4363 *
4364 * Return OK or FAIL.
4365 */
4366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004367eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
Bram Moolenaar33570922005-01-25 22:26:29 +00004369 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 long result;
4371 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374 /*
4375 * Get the first variable.
4376 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 return FAIL;
4379
4380 /*
4381 * Repeat until there is no following "&&".
4382 */
4383 first = TRUE;
4384 result = TRUE;
4385 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4386 {
4387 if (evaluate && first)
4388 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004389 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004392 if (error)
4393 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 first = FALSE;
4395 }
4396
4397 /*
4398 * Get the second variable.
4399 */
4400 *arg = skipwhite(*arg + 2);
4401 if (eval4(arg, &var2, evaluate && result) == FAIL)
4402 return FAIL;
4403
4404 /*
4405 * Compute the result.
4406 */
4407 if (evaluate && result)
4408 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004409 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004412 if (error)
4413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 if (evaluate)
4416 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004417 rettv->v_type = VAR_NUMBER;
4418 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 }
4421
4422 return OK;
4423}
4424
4425/*
4426 * Handle third level expression:
4427 * var1 == var2
4428 * var1 =~ var2
4429 * var1 != var2
4430 * var1 !~ var2
4431 * var1 > var2
4432 * var1 >= var2
4433 * var1 < var2
4434 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004435 * var1 is var2
4436 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 *
4438 * "arg" must point to the first non-white of the expression.
4439 * "arg" is advanced to the next non-white after the recognized expression.
4440 *
4441 * Return OK or FAIL.
4442 */
4443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004444eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
Bram Moolenaar33570922005-01-25 22:26:29 +00004446 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 char_u *p;
4448 int i;
4449 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 int len = 2;
4452 long n1, n2;
4453 char_u *s1, *s2;
4454 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456
4457 /*
4458 * Get the first variable.
4459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 return FAIL;
4462
4463 p = *arg;
4464 switch (p[0])
4465 {
4466 case '=': if (p[1] == '=')
4467 type = TYPE_EQUAL;
4468 else if (p[1] == '~')
4469 type = TYPE_MATCH;
4470 break;
4471 case '!': if (p[1] == '=')
4472 type = TYPE_NEQUAL;
4473 else if (p[1] == '~')
4474 type = TYPE_NOMATCH;
4475 break;
4476 case '>': if (p[1] != '=')
4477 {
4478 type = TYPE_GREATER;
4479 len = 1;
4480 }
4481 else
4482 type = TYPE_GEQUAL;
4483 break;
4484 case '<': if (p[1] != '=')
4485 {
4486 type = TYPE_SMALLER;
4487 len = 1;
4488 }
4489 else
4490 type = TYPE_SEQUAL;
4491 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 case 'i': if (p[1] == 's')
4493 {
4494 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4495 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004496 i = p[len];
4497 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004498 {
4499 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4500 type_is = TRUE;
4501 }
4502 }
4503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
4506 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004507 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 */
4509 if (type != TYPE_UNKNOWN)
4510 {
4511 /* extra question mark appended: ignore case */
4512 if (p[len] == '?')
4513 {
4514 ic = TRUE;
4515 ++len;
4516 }
4517 /* extra '#' appended: match case */
4518 else if (p[len] == '#')
4519 {
4520 ic = FALSE;
4521 ++len;
4522 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004523 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else
4525 ic = p_ic;
4526
4527 /*
4528 * Get the second variable.
4529 */
4530 *arg = skipwhite(p + len);
4531 if (eval5(arg, &var2, evaluate) == FAIL)
4532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004533 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 return FAIL;
4535 }
4536
4537 if (evaluate)
4538 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004539 if (type_is && rettv->v_type != var2.v_type)
4540 {
4541 /* For "is" a different type always means FALSE, for "notis"
4542 * it means TRUE. */
4543 n1 = (type == TYPE_NEQUAL);
4544 }
4545 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4546 {
4547 if (type_is)
4548 {
4549 n1 = (rettv->v_type == var2.v_type
4550 && rettv->vval.v_list == var2.vval.v_list);
4551 if (type == TYPE_NEQUAL)
4552 n1 = !n1;
4553 }
4554 else if (rettv->v_type != var2.v_type
4555 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4556 {
4557 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004560 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 else
4566 {
4567 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004568 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4569 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 }
4574
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004575 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4576 {
4577 if (type_is)
4578 {
4579 n1 = (rettv->v_type == var2.v_type
4580 && rettv->vval.v_dict == var2.vval.v_dict);
4581 if (type == TYPE_NEQUAL)
4582 n1 = !n1;
4583 }
4584 else if (rettv->v_type != var2.v_type
4585 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4586 {
4587 if (rettv->v_type != var2.v_type)
4588 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4589 else
4590 EMSG(_("E736: Invalid operation for Dictionary"));
4591 clear_tv(rettv);
4592 clear_tv(&var2);
4593 return FAIL;
4594 }
4595 else
4596 {
4597 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004598 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4599 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004600 if (type == TYPE_NEQUAL)
4601 n1 = !n1;
4602 }
4603 }
4604
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004605 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4606 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004608 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004609 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004610 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 clear_tv(rettv);
4612 clear_tv(&var2);
4613 return FAIL;
4614 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004615 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004616 if (type == TYPE_NEQUAL)
4617 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004618 }
4619
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004620#ifdef FEAT_FLOAT
4621 /*
4622 * If one of the two variables is a float, compare as a float.
4623 * When using "=~" or "!~", always compare as string.
4624 */
4625 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4626 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4627 {
4628 float_T f1, f2;
4629
4630 if (rettv->v_type == VAR_FLOAT)
4631 f1 = rettv->vval.v_float;
4632 else
4633 f1 = get_tv_number(rettv);
4634 if (var2.v_type == VAR_FLOAT)
4635 f2 = var2.vval.v_float;
4636 else
4637 f2 = get_tv_number(&var2);
4638 n1 = FALSE;
4639 switch (type)
4640 {
4641 case TYPE_EQUAL: n1 = (f1 == f2); break;
4642 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4643 case TYPE_GREATER: n1 = (f1 > f2); break;
4644 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4645 case TYPE_SMALLER: n1 = (f1 < f2); break;
4646 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4647 case TYPE_UNKNOWN:
4648 case TYPE_MATCH:
4649 case TYPE_NOMATCH: break; /* avoid gcc warning */
4650 }
4651 }
4652#endif
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
4655 * If one of the two variables is a number, compare as a number.
4656 * When using "=~" or "!~", always compare as string.
4657 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004658 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 n1 = get_tv_number(rettv);
4662 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 switch (type)
4664 {
4665 case TYPE_EQUAL: n1 = (n1 == n2); break;
4666 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4667 case TYPE_GREATER: n1 = (n1 > n2); break;
4668 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4669 case TYPE_SMALLER: n1 = (n1 < n2); break;
4670 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4671 case TYPE_UNKNOWN:
4672 case TYPE_MATCH:
4673 case TYPE_NOMATCH: break; /* avoid gcc warning */
4674 }
4675 }
4676 else
4677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004678 s1 = get_tv_string_buf(rettv, buf1);
4679 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4681 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4682 else
4683 i = 0;
4684 n1 = FALSE;
4685 switch (type)
4686 {
4687 case TYPE_EQUAL: n1 = (i == 0); break;
4688 case TYPE_NEQUAL: n1 = (i != 0); break;
4689 case TYPE_GREATER: n1 = (i > 0); break;
4690 case TYPE_GEQUAL: n1 = (i >= 0); break;
4691 case TYPE_SMALLER: n1 = (i < 0); break;
4692 case TYPE_SEQUAL: n1 = (i <= 0); break;
4693
4694 case TYPE_MATCH:
4695 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004696 n1 = pattern_match(s2, s1, ic);
4697 if (type == TYPE_NOMATCH)
4698 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 break;
4700
4701 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4702 }
4703 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004704 clear_tv(rettv);
4705 clear_tv(&var2);
4706 rettv->v_type = VAR_NUMBER;
4707 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710
4711 return OK;
4712}
4713
4714/*
4715 * Handle fourth level expression:
4716 * + number addition
4717 * - number subtraction
4718 * . string concatenation
4719 *
4720 * "arg" must point to the first non-white of the expression.
4721 * "arg" is advanced to the next non-white after the recognized expression.
4722 *
4723 * Return OK or FAIL.
4724 */
4725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004726eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar33570922005-01-25 22:26:29 +00004728 typval_T var2;
4729 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 int op;
4731 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004732#ifdef FEAT_FLOAT
4733 float_T f1 = 0, f2 = 0;
4734#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 char_u *s1, *s2;
4736 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4737 char_u *p;
4738
4739 /*
4740 * Get the first variable.
4741 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 return FAIL;
4744
4745 /*
4746 * Repeat computing, until no '+', '-' or '.' is following.
4747 */
4748 for (;;)
4749 {
4750 op = **arg;
4751 if (op != '+' && op != '-' && op != '.')
4752 break;
4753
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004754 if ((op != '+' || rettv->v_type != VAR_LIST)
4755#ifdef FEAT_FLOAT
4756 && (op == '.' || rettv->v_type != VAR_FLOAT)
4757#endif
4758 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004759 {
4760 /* For "list + ...", an illegal use of the first operand as
4761 * a number cannot be determined before evaluating the 2nd
4762 * operand: if this is also a list, all is ok.
4763 * For "something . ...", "something - ..." or "non-list + ...",
4764 * we know that the first operand needs to be a string or number
4765 * without evaluating the 2nd operand. So check before to avoid
4766 * side effects after an error. */
4767 if (evaluate && get_tv_string_chk(rettv) == NULL)
4768 {
4769 clear_tv(rettv);
4770 return FAIL;
4771 }
4772 }
4773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 /*
4775 * Get the second variable.
4776 */
4777 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004778 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 return FAIL;
4782 }
4783
4784 if (evaluate)
4785 {
4786 /*
4787 * Compute the result.
4788 */
4789 if (op == '.')
4790 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4792 s2 = get_tv_string_buf_chk(&var2, buf2);
4793 if (s2 == NULL) /* type error ? */
4794 {
4795 clear_tv(rettv);
4796 clear_tv(&var2);
4797 return FAIL;
4798 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004799 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004800 clear_tv(rettv);
4801 rettv->v_type = VAR_STRING;
4802 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004804 else if (op == '+' && rettv->v_type == VAR_LIST
4805 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004806 {
4807 /* concatenate Lists */
4808 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4809 &var3) == FAIL)
4810 {
4811 clear_tv(rettv);
4812 clear_tv(&var2);
4813 return FAIL;
4814 }
4815 clear_tv(rettv);
4816 *rettv = var3;
4817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 else
4819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004820 int error = FALSE;
4821
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004824 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004825 f1 = rettv->vval.v_float;
4826 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004827 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004828 else
4829#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831 n1 = get_tv_number_chk(rettv, &error);
4832 if (error)
4833 {
4834 /* This can only happen for "list + non-list". For
4835 * "non-list + ..." or "something - ...", we returned
4836 * before evaluating the 2nd operand. */
4837 clear_tv(rettv);
4838 return FAIL;
4839 }
4840#ifdef FEAT_FLOAT
4841 if (var2.v_type == VAR_FLOAT)
4842 f1 = n1;
4843#endif
4844 }
4845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 {
4848 f2 = var2.vval.v_float;
4849 n2 = 0;
4850 }
4851 else
4852#endif
4853 {
4854 n2 = get_tv_number_chk(&var2, &error);
4855 if (error)
4856 {
4857 clear_tv(rettv);
4858 clear_tv(&var2);
4859 return FAIL;
4860 }
4861#ifdef FEAT_FLOAT
4862 if (rettv->v_type == VAR_FLOAT)
4863 f2 = n2;
4864#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004865 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004866 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004867
4868#ifdef FEAT_FLOAT
4869 /* If there is a float on either side the result is a float. */
4870 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4871 {
4872 if (op == '+')
4873 f1 = f1 + f2;
4874 else
4875 f1 = f1 - f2;
4876 rettv->v_type = VAR_FLOAT;
4877 rettv->vval.v_float = f1;
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004880#endif
4881 {
4882 if (op == '+')
4883 n1 = n1 + n2;
4884 else
4885 n1 = n1 - n2;
4886 rettv->v_type = VAR_NUMBER;
4887 rettv->vval.v_number = n1;
4888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004890 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 }
4893 return OK;
4894}
4895
4896/*
4897 * Handle fifth level expression:
4898 * * number multiplication
4899 * / number division
4900 * % number modulo
4901 *
4902 * "arg" must point to the first non-white of the expression.
4903 * "arg" is advanced to the next non-white after the recognized expression.
4904 *
4905 * Return OK or FAIL.
4906 */
4907 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004908eval6(
4909 char_u **arg,
4910 typval_T *rettv,
4911 int evaluate,
4912 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
Bram Moolenaar33570922005-01-25 22:26:29 +00004914 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 int op;
4916 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004917#ifdef FEAT_FLOAT
4918 int use_float = FALSE;
4919 float_T f1 = 0, f2;
4920#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 /*
4924 * Get the first variable.
4925 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004926 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928
4929 /*
4930 * Repeat computing, until no '*', '/' or '%' is following.
4931 */
4932 for (;;)
4933 {
4934 op = **arg;
4935 if (op != '*' && op != '/' && op != '%')
4936 break;
4937
4938 if (evaluate)
4939 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940#ifdef FEAT_FLOAT
4941 if (rettv->v_type == VAR_FLOAT)
4942 {
4943 f1 = rettv->vval.v_float;
4944 use_float = TRUE;
4945 n1 = 0;
4946 }
4947 else
4948#endif
4949 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004950 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004951 if (error)
4952 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 else
4955 n1 = 0;
4956
4957 /*
4958 * Get the second variable.
4959 */
4960 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004961 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return FAIL;
4963
4964 if (evaluate)
4965 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966#ifdef FEAT_FLOAT
4967 if (var2.v_type == VAR_FLOAT)
4968 {
4969 if (!use_float)
4970 {
4971 f1 = n1;
4972 use_float = TRUE;
4973 }
4974 f2 = var2.vval.v_float;
4975 n2 = 0;
4976 }
4977 else
4978#endif
4979 {
4980 n2 = get_tv_number_chk(&var2, &error);
4981 clear_tv(&var2);
4982 if (error)
4983 return FAIL;
4984#ifdef FEAT_FLOAT
4985 if (use_float)
4986 f2 = n2;
4987#endif
4988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989
4990 /*
4991 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004992 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004994#ifdef FEAT_FLOAT
4995 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004997 if (op == '*')
4998 f1 = f1 * f2;
4999 else if (op == '/')
5000 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001# ifdef VMS
5002 /* VMS crashes on divide by zero, work around it */
5003 if (f2 == 0.0)
5004 {
5005 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005006 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005008 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005009 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005010 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005011 }
5012 else
5013 f1 = f1 / f2;
5014# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015 /* We rely on the floating point library to handle divide
5016 * by zero to result in "inf" and not a crash. */
5017 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005018# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005021 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005022 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 return FAIL;
5024 }
5025 rettv->v_type = VAR_FLOAT;
5026 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031 if (op == '*')
5032 n1 = n1 * n2;
5033 else if (op == '/')
5034 {
5035 if (n2 == 0) /* give an error message? */
5036 {
5037 if (n1 == 0)
5038 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5039 else if (n1 < 0)
5040 n1 = -0x7fffffffL;
5041 else
5042 n1 = 0x7fffffffL;
5043 }
5044 else
5045 n1 = n1 / n2;
5046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005048 {
5049 if (n2 == 0) /* give an error message? */
5050 n1 = 0;
5051 else
5052 n1 = n1 % n2;
5053 }
5054 rettv->v_type = VAR_NUMBER;
5055 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 }
5059
5060 return OK;
5061}
5062
5063/*
5064 * Handle sixth level expression:
5065 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005066 * "string" string constant
5067 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 * &option-name option value
5069 * @r register contents
5070 * identifier variable value
5071 * function() function call
5072 * $VAR environment variable
5073 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005074 * [expr, expr] List
5075 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 *
5077 * Also handle:
5078 * ! in front logical NOT
5079 * - in front unary minus
5080 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 * trailing [] subscript in String or List
5082 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 *
5084 * "arg" must point to the first non-white of the expression.
5085 * "arg" is advanced to the next non-white after the recognized expression.
5086 *
5087 * Return OK or FAIL.
5088 */
5089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005090eval7(
5091 char_u **arg,
5092 typval_T *rettv,
5093 int evaluate,
5094 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 long n;
5097 int len;
5098 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 char_u *start_leader, *end_leader;
5100 int ret = OK;
5101 char_u *alias;
5102
5103 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005105 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108
5109 /*
5110 * Skip '!' and '-' characters. They are handled later.
5111 */
5112 start_leader = *arg;
5113 while (**arg == '!' || **arg == '-' || **arg == '+')
5114 *arg = skipwhite(*arg + 1);
5115 end_leader = *arg;
5116
5117 switch (**arg)
5118 {
5119 /*
5120 * Number constant.
5121 */
5122 case '0':
5123 case '1':
5124 case '2':
5125 case '3':
5126 case '4':
5127 case '5':
5128 case '6':
5129 case '7':
5130 case '8':
5131 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005132 {
5133#ifdef FEAT_FLOAT
5134 char_u *p = skipdigits(*arg + 1);
5135 int get_float = FALSE;
5136
5137 /* We accept a float when the format matches
5138 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005139 * strict to avoid backwards compatibility problems.
5140 * Don't look for a float after the "." operator, so that
5141 * ":let vers = 1.2.3" doesn't fail. */
5142 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 get_float = TRUE;
5145 p = skipdigits(p + 2);
5146 if (*p == 'e' || *p == 'E')
5147 {
5148 ++p;
5149 if (*p == '-' || *p == '+')
5150 ++p;
5151 if (!vim_isdigit(*p))
5152 get_float = FALSE;
5153 else
5154 p = skipdigits(p + 1);
5155 }
5156 if (ASCII_ISALPHA(*p) || *p == '.')
5157 get_float = FALSE;
5158 }
5159 if (get_float)
5160 {
5161 float_T f;
5162
5163 *arg += string2float(*arg, &f);
5164 if (evaluate)
5165 {
5166 rettv->v_type = VAR_FLOAT;
5167 rettv->vval.v_float = f;
5168 }
5169 }
5170 else
5171#endif
5172 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005173 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005174 *arg += len;
5175 if (evaluate)
5176 {
5177 rettv->v_type = VAR_NUMBER;
5178 rettv->vval.v_number = n;
5179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
5184 /*
5185 * String constant: "string".
5186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005193 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005194 break;
5195
5196 /*
5197 * List: [expr, expr]
5198 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 break;
5201
5202 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005203 * Dictionary: {key: val, key: val}
5204 */
5205 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5206 break;
5207
5208 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005209 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005211 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213
5214 /*
5215 * Environment variable: $VAR.
5216 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005217 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 break;
5219
5220 /*
5221 * Register contents: @r.
5222 */
5223 case '@': ++*arg;
5224 if (evaluate)
5225 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005226 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005227 rettv->vval.v_string = get_reg_contents(**arg,
5228 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 if (**arg != NUL)
5231 ++*arg;
5232 break;
5233
5234 /*
5235 * nested expression: (expression).
5236 */
5237 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 if (**arg == ')')
5240 ++*arg;
5241 else if (ret == OK)
5242 {
5243 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005244 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 ret = FAIL;
5246 }
5247 break;
5248
Bram Moolenaar8c711452005-01-14 21:53:12 +00005249 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 break;
5251 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252
5253 if (ret == NOTDONE)
5254 {
5255 /*
5256 * Must be a variable or function name.
5257 * Can also be a curly-braces kind of name: {expr}.
5258 */
5259 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 if (alias != NULL)
5262 s = alias;
5263
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005265 ret = FAIL;
5266 else
5267 {
5268 if (**arg == '(') /* recursive! */
5269 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005270 partial_T *partial;
5271
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 /* If "s" is the name of a variable of type VAR_FUNC
5273 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005274 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275
5276 /* Invoke the function. */
5277 ret = get_func_tv(s, len, rettv, arg,
5278 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005279 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005280
5281 /* If evaluate is FALSE rettv->v_type was not set in
5282 * get_func_tv, but it's needed in handle_subscript() to parse
5283 * what follows. So set it here. */
5284 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5285 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005286 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005287 rettv->v_type = VAR_FUNC;
5288 }
5289
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 /* Stop the expression evaluation when immediately
5291 * aborting on error, or when an interrupt occurred or
5292 * an exception was thrown but not caught. */
5293 if (aborting())
5294 {
5295 if (ret == OK)
5296 clear_tv(rettv);
5297 ret = FAIL;
5298 }
5299 }
5300 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005301 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005302 else
5303 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005305 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 }
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 *arg = skipwhite(*arg);
5309
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005310 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5311 * expr(expr). */
5312 if (ret == OK)
5313 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 /*
5316 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5317 */
5318 if (ret == OK && evaluate && end_leader > start_leader)
5319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321 int val = 0;
5322#ifdef FEAT_FLOAT
5323 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005325 if (rettv->v_type == VAR_FLOAT)
5326 f = rettv->vval.v_float;
5327 else
5328#endif
5329 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005330 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 clear_tv(rettv);
5333 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else
5336 {
5337 while (end_leader > start_leader)
5338 {
5339 --end_leader;
5340 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005341 {
5342#ifdef FEAT_FLOAT
5343 if (rettv->v_type == VAR_FLOAT)
5344 f = !f;
5345 else
5346#endif
5347 val = !val;
5348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005349 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005350 {
5351#ifdef FEAT_FLOAT
5352 if (rettv->v_type == VAR_FLOAT)
5353 f = -f;
5354 else
5355#endif
5356 val = -val;
5357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005359#ifdef FEAT_FLOAT
5360 if (rettv->v_type == VAR_FLOAT)
5361 {
5362 clear_tv(rettv);
5363 rettv->vval.v_float = f;
5364 }
5365 else
5366#endif
5367 {
5368 clear_tv(rettv);
5369 rettv->v_type = VAR_NUMBER;
5370 rettv->vval.v_number = val;
5371 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 }
5374
5375 return ret;
5376}
5377
5378/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005379 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5380 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5382 */
5383 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005384eval_index(
5385 char_u **arg,
5386 typval_T *rettv,
5387 int evaluate,
5388 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389{
5390 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005391 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005393 long len = -1;
5394 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397
Bram Moolenaara03f2332016-02-06 18:09:59 +01005398 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005401 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005402 if (verbose)
5403 EMSG(_("E695: Cannot index a Funcref"));
5404 return FAIL;
5405 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005406#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005407 if (verbose)
5408 EMSG(_(e_float_as_string));
5409 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005410#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005411 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005412 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005413 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005414 if (verbose)
5415 EMSG(_("E909: Cannot index a special variable"));
5416 return FAIL;
5417 case VAR_UNKNOWN:
5418 if (evaluate)
5419 return FAIL;
5420 /* FALLTHROUGH */
5421
5422 case VAR_STRING:
5423 case VAR_NUMBER:
5424 case VAR_LIST:
5425 case VAR_DICT:
5426 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005427 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005429 init_tv(&var1);
5430 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005433 /*
5434 * dict.name
5435 */
5436 key = *arg + 1;
5437 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5438 ;
5439 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005441 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
5443 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005445 /*
5446 * something[idx]
5447 *
5448 * Get the (first) variable from inside the [].
5449 */
5450 *arg = skipwhite(*arg + 1);
5451 if (**arg == ':')
5452 empty1 = TRUE;
5453 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5454 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005455 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5456 {
5457 /* not a number or string */
5458 clear_tv(&var1);
5459 return FAIL;
5460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461
5462 /*
5463 * Get the second variable from inside the [:].
5464 */
5465 if (**arg == ':')
5466 {
5467 range = TRUE;
5468 *arg = skipwhite(*arg + 1);
5469 if (**arg == ']')
5470 empty2 = TRUE;
5471 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005473 if (!empty1)
5474 clear_tv(&var1);
5475 return FAIL;
5476 }
5477 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5478 {
5479 /* not a number or string */
5480 if (!empty1)
5481 clear_tv(&var1);
5482 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005483 return FAIL;
5484 }
5485 }
5486
5487 /* Check for the ']'. */
5488 if (**arg != ']')
5489 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005490 if (verbose)
5491 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 clear_tv(&var1);
5493 if (range)
5494 clear_tv(&var2);
5495 return FAIL;
5496 }
5497 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 }
5499
5500 if (evaluate)
5501 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005502 n1 = 0;
5503 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 n1 = get_tv_number(&var1);
5506 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005507 }
5508 if (range)
5509 {
5510 if (empty2)
5511 n2 = -1;
5512 else
5513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514 n2 = get_tv_number(&var2);
5515 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 }
5517 }
5518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005519 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005521 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005522 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005523 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005524 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005525 case VAR_SPECIAL:
5526 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005527 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005528 break; /* not evaluating, skipping over subscript */
5529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 case VAR_NUMBER:
5531 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005532 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005533 len = (long)STRLEN(s);
5534 if (range)
5535 {
5536 /* The resulting variable is a substring. If the indexes
5537 * are out of range the result is empty. */
5538 if (n1 < 0)
5539 {
5540 n1 = len + n1;
5541 if (n1 < 0)
5542 n1 = 0;
5543 }
5544 if (n2 < 0)
5545 n2 = len + n2;
5546 else if (n2 >= len)
5547 n2 = len;
5548 if (n1 >= len || n2 < 0 || n1 > n2)
5549 s = NULL;
5550 else
5551 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5552 }
5553 else
5554 {
5555 /* The resulting variable is a string of a single
5556 * character. If the index is too big or negative the
5557 * result is empty. */
5558 if (n1 >= len || n1 < 0)
5559 s = NULL;
5560 else
5561 s = vim_strnsave(s + n1, 1);
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 clear_tv(rettv);
5564 rettv->v_type = VAR_STRING;
5565 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 break;
5567
5568 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005569 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 if (n1 < 0)
5571 n1 = len + n1;
5572 if (!empty1 && (n1 < 0 || n1 >= len))
5573 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005574 /* For a range we allow invalid values and return an empty
5575 * list. A list index out of range is an error. */
5576 if (!range)
5577 {
5578 if (verbose)
5579 EMSGN(_(e_listidx), n1);
5580 return FAIL;
5581 }
5582 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 }
5584 if (range)
5585 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005586 list_T *l;
5587 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588
5589 if (n2 < 0)
5590 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005591 else if (n2 >= len)
5592 n2 = len - 1;
5593 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005594 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 l = list_alloc();
5596 if (l == NULL)
5597 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005598 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599 n1 <= n2; ++n1)
5600 {
5601 if (list_append_tv(l, &item->li_tv) == FAIL)
5602 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005603 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 return FAIL;
5605 }
5606 item = item->li_next;
5607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005608 clear_tv(rettv);
5609 rettv->v_type = VAR_LIST;
5610 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005611 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005612 }
5613 else
5614 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005615 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005616 clear_tv(rettv);
5617 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005618 }
5619 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005620
5621 case VAR_DICT:
5622 if (range)
5623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005624 if (verbose)
5625 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 if (len == -1)
5627 clear_tv(&var1);
5628 return FAIL;
5629 }
5630 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005631 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632
5633 if (len == -1)
5634 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005635 key = get_tv_string_chk(&var1);
5636 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005637 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 clear_tv(&var1);
5639 return FAIL;
5640 }
5641 }
5642
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005643 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005645 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005646 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 if (len == -1)
5648 clear_tv(&var1);
5649 if (item == NULL)
5650 return FAIL;
5651
5652 copy_tv(&item->di_tv, &var1);
5653 clear_tv(rettv);
5654 *rettv = var1;
5655 }
5656 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 }
5658 }
5659
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005660 return OK;
5661}
5662
5663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 * Get an option value.
5665 * "arg" points to the '&' or '+' before the option name.
5666 * "arg" is advanced to character after the option name.
5667 * Return OK or FAIL.
5668 */
5669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005670get_option_tv(
5671 char_u **arg,
5672 typval_T *rettv, /* when NULL, only check if option exists */
5673 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
5675 char_u *option_end;
5676 long numval;
5677 char_u *stringval;
5678 int opt_type;
5679 int c;
5680 int working = (**arg == '+'); /* has("+option") */
5681 int ret = OK;
5682 int opt_flags;
5683
5684 /*
5685 * Isolate the option name and find its value.
5686 */
5687 option_end = find_option_end(arg, &opt_flags);
5688 if (option_end == NULL)
5689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005690 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 EMSG2(_("E112: Option name missing: %s"), *arg);
5692 return FAIL;
5693 }
5694
5695 if (!evaluate)
5696 {
5697 *arg = option_end;
5698 return OK;
5699 }
5700
5701 c = *option_end;
5702 *option_end = NUL;
5703 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
5706 if (opt_type == -3) /* invalid name */
5707 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005708 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 EMSG2(_("E113: Unknown option: %s"), *arg);
5710 ret = FAIL;
5711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005712 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
5714 if (opt_type == -2) /* hidden string option */
5715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005716 rettv->v_type = VAR_STRING;
5717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719 else if (opt_type == -1) /* hidden number option */
5720 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005721 rettv->v_type = VAR_NUMBER;
5722 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 else if (opt_type == 1) /* number option */
5725 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 rettv->v_type = VAR_NUMBER;
5727 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 else /* string option */
5730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005731 rettv->v_type = VAR_STRING;
5732 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 }
5734 }
5735 else if (working && (opt_type == -2 || opt_type == -1))
5736 ret = FAIL;
5737
5738 *option_end = c; /* put back for error messages */
5739 *arg = option_end;
5740
5741 return ret;
5742}
5743
5744/*
5745 * Allocate a variable for a string constant.
5746 * Return OK or FAIL.
5747 */
5748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005749get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751 char_u *p;
5752 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 int extra = 0;
5754
5755 /*
5756 * Find the end of the string, skipping backslashed characters.
5757 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005758 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
5760 if (*p == '\\' && p[1] != NUL)
5761 {
5762 ++p;
5763 /* A "\<x>" form occupies at least 4 characters, and produces up
5764 * to 6 characters: reserve space for 2 extra */
5765 if (*p == '<')
5766 extra += 2;
5767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769
5770 if (*p != '"')
5771 {
5772 EMSG2(_("E114: Missing quote: %s"), *arg);
5773 return FAIL;
5774 }
5775
5776 /* If only parsing, set *arg and return here */
5777 if (!evaluate)
5778 {
5779 *arg = p + 1;
5780 return OK;
5781 }
5782
5783 /*
5784 * Copy the string into allocated memory, handling backslashed
5785 * characters.
5786 */
5787 name = alloc((unsigned)(p - *arg + extra));
5788 if (name == NULL)
5789 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 rettv->v_type = VAR_STRING;
5791 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 if (*p == '\\')
5796 {
5797 switch (*++p)
5798 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005799 case 'b': *name++ = BS; ++p; break;
5800 case 'e': *name++ = ESC; ++p; break;
5801 case 'f': *name++ = FF; ++p; break;
5802 case 'n': *name++ = NL; ++p; break;
5803 case 'r': *name++ = CAR; ++p; break;
5804 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
5806 case 'X': /* hex: "\x1", "\x12" */
5807 case 'x':
5808 case 'u': /* Unicode: "\u0023" */
5809 case 'U':
5810 if (vim_isxdigit(p[1]))
5811 {
5812 int n, nr;
5813 int c = toupper(*p);
5814
5815 if (c == 'X')
5816 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005817 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005819 else
5820 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 nr = 0;
5822 while (--n >= 0 && vim_isxdigit(p[1]))
5823 {
5824 ++p;
5825 nr = (nr << 4) + hex2nr(*p);
5826 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828#ifdef FEAT_MBYTE
5829 /* For "\u" store the number according to
5830 * 'encoding'. */
5831 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 else
5834#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 break;
5838
5839 /* octal: "\1", "\12", "\123" */
5840 case '0':
5841 case '1':
5842 case '2':
5843 case '3':
5844 case '4':
5845 case '5':
5846 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 case '7': *name = *p++ - '0';
5848 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 *name = (*name << 3) + *p++ - '0';
5851 if (*p >= '0' && *p <= '7')
5852 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856
5857 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (extra != 0)
5860 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 break;
5863 }
5864 /* FALLTHROUGH */
5865
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 break;
5868 }
5869 }
5870 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005874 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 *arg = p + 1;
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 return OK;
5878}
5879
5880/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 * Return OK or FAIL.
5883 */
5884 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005889 int reduce = 0;
5890
5891 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 */
5894 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5895 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 break;
5900 ++reduce;
5901 ++p;
5902 }
5903 }
5904
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 return FAIL;
5909 }
5910
Bram Moolenaar8c711452005-01-14 21:53:12 +00005911 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005912 if (!evaluate)
5913 {
5914 *arg = p + 1;
5915 return OK;
5916 }
5917
5918 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 */
5921 str = alloc((unsigned)((p - *arg) - reduce));
5922 if (str == NULL)
5923 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005924 rettv->v_type = VAR_STRING;
5925 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926
Bram Moolenaar8c711452005-01-14 21:53:12 +00005927 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005929 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005930 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005932 break;
5933 ++p;
5934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005935 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005936 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005937 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005938 *arg = p + 1;
5939
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 return OK;
5941}
5942
Bram Moolenaarddecc252016-04-06 22:59:37 +02005943 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005944partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005945{
5946 int i;
5947
5948 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005949 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005950 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005951 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005952 func_unref(pt->pt_name);
5953 vim_free(pt->pt_name);
5954 vim_free(pt);
5955}
5956
5957/*
5958 * Unreference a closure: decrement the reference count and free it when it
5959 * becomes zero.
5960 */
5961 void
5962partial_unref(partial_T *pt)
5963{
5964 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005965 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005966}
5967
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005968/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 * Allocate a variable for a List and fill it from "*arg".
5970 * Return OK or FAIL.
5971 */
5972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005973get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974{
Bram Moolenaar33570922005-01-25 22:26:29 +00005975 list_T *l = NULL;
5976 typval_T tv;
5977 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978
5979 if (evaluate)
5980 {
5981 l = list_alloc();
5982 if (l == NULL)
5983 return FAIL;
5984 }
5985
5986 *arg = skipwhite(*arg + 1);
5987 while (**arg != ']' && **arg != NUL)
5988 {
5989 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5990 goto failret;
5991 if (evaluate)
5992 {
5993 item = listitem_alloc();
5994 if (item != NULL)
5995 {
5996 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005997 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005998 list_append(l, item);
5999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006000 else
6001 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006002 }
6003
6004 if (**arg == ']')
6005 break;
6006 if (**arg != ',')
6007 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006008 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 goto failret;
6010 }
6011 *arg = skipwhite(*arg + 1);
6012 }
6013
6014 if (**arg != ']')
6015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006016 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017failret:
6018 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006019 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 return FAIL;
6021 }
6022
6023 *arg = skipwhite(*arg + 1);
6024 if (evaluate)
6025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006026 rettv->v_type = VAR_LIST;
6027 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 ++l->lv_refcount;
6029 }
6030
6031 return OK;
6032}
6033
6034/*
6035 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006036 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006038 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 list_T *l;
6042
6043 l = (list_T *)alloc_clear(sizeof(list_T));
6044 if (l != NULL)
6045 {
6046 /* Prepend the list to the list of lists for garbage collection. */
6047 if (first_list != NULL)
6048 first_list->lv_used_prev = l;
6049 l->lv_used_prev = NULL;
6050 l->lv_used_next = first_list;
6051 first_list = l;
6052 }
6053 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054}
6055
6056/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006057 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006058 * Returns OK or FAIL.
6059 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006062{
6063 list_T *l = list_alloc();
6064
6065 if (l == NULL)
6066 return FAIL;
6067
6068 rettv->vval.v_list = l;
6069 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006070 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006071 ++l->lv_refcount;
6072 return OK;
6073}
6074
6075/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 * Unreference a list: decrement the reference count and free it when it
6077 * becomes zero.
6078 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006081{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006082 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006083 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084}
6085
6086/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006087 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 * Ignores the reference count.
6089 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006090 static void
6091list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar33570922005-01-25 22:26:29 +00006093 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006095 for (item = l->lv_first; item != NULL; item = l->lv_first)
6096 {
6097 /* Remove the item before deleting it. */
6098 l->lv_first = item->li_next;
6099 clear_tv(&item->li_tv);
6100 vim_free(item);
6101 }
6102}
6103
6104 static void
6105list_free_list(list_T *l)
6106{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006107 /* Remove the list from the list of lists for garbage collection. */
6108 if (l->lv_used_prev == NULL)
6109 first_list = l->lv_used_next;
6110 else
6111 l->lv_used_prev->lv_used_next = l->lv_used_next;
6112 if (l->lv_used_next != NULL)
6113 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6114
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115 vim_free(l);
6116}
6117
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006118 void
6119list_free(list_T *l)
6120{
6121 if (!in_free_unref_items)
6122 {
6123 list_free_contents(l);
6124 list_free_list(l);
6125 }
6126}
6127
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128/*
6129 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006130 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006131 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006132 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006133listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006134{
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006136}
6137
6138/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006139 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006140 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006143{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006144 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006145 vim_free(item);
6146}
6147
6148/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006149 * Remove a list item from a List and free it. Also clears the value.
6150 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006153{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006154 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006155 listitem_free(item);
6156}
6157
6158/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 * Get the number of items in a list.
6160 */
6161 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006162list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164 if (l == NULL)
6165 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006166 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006167}
6168
6169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE when two lists have exactly the same values.
6171 */
6172 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006173list_equal(
6174 list_T *l1,
6175 list_T *l2,
6176 int ic, /* ignore case for strings */
6177 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178{
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006181 if (l1 == NULL || l2 == NULL)
6182 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006183 if (l1 == l2)
6184 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006185 if (list_len(l1) != list_len(l2))
6186 return FALSE;
6187
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188 for (item1 = l1->lv_first, item2 = l2->lv_first;
6189 item1 != NULL && item2 != NULL;
6190 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006192 return FALSE;
6193 return item1 == NULL && item2 == NULL;
6194}
6195
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006196/*
6197 * Return the dictitem that an entry in a hashtable points to.
6198 */
6199 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006200dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006201{
6202 return HI2DI(hi);
6203}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006204
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006205/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006206 * Return TRUE when two dictionaries have exactly the same key/values.
6207 */
6208 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006209dict_equal(
6210 dict_T *d1,
6211 dict_T *d2,
6212 int ic, /* ignore case for strings */
6213 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006214{
Bram Moolenaar33570922005-01-25 22:26:29 +00006215 hashitem_T *hi;
6216 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006217 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006218
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006219 if (d1 == NULL || d2 == NULL)
6220 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006221 if (d1 == d2)
6222 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006223 if (dict_len(d1) != dict_len(d2))
6224 return FALSE;
6225
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006226 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006227 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006228 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006229 if (!HASHITEM_EMPTY(hi))
6230 {
6231 item2 = dict_find(d2, hi->hi_key, -1);
6232 if (item2 == NULL)
6233 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006234 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006235 return FALSE;
6236 --todo;
6237 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006238 }
6239 return TRUE;
6240}
6241
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006242static int tv_equal_recurse_limit;
6243
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006244/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006245 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006247 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006248 */
6249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006250tv_equal(
6251 typval_T *tv1,
6252 typval_T *tv2,
6253 int ic, /* ignore case */
6254 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006255{
6256 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006257 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006258 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006259 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006261 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6262 if ((tv1->v_type == VAR_FUNC
6263 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6264 && (tv2->v_type == VAR_FUNC
6265 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6266 {
6267 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6268 : tv1->vval.v_partial->pt_name;
6269 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6270 : tv2->vval.v_partial->pt_name;
6271 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6272 }
6273
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006274 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006275 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006276
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006277 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006278 * recursiveness to a limit. We guess they are equal then.
6279 * A fixed limit has the problem of still taking an awful long time.
6280 * Reduce the limit every time running into it. That should work fine for
6281 * deeply linked structures that are not recursively linked and catch
6282 * recursiveness quickly. */
6283 if (!recursive)
6284 tv_equal_recurse_limit = 1000;
6285 if (recursive_cnt >= tv_equal_recurse_limit)
6286 {
6287 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006288 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006289 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006290
6291 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006292 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006293 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006294 ++recursive_cnt;
6295 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6296 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006297 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006298
6299 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006300 ++recursive_cnt;
6301 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6302 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006303 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006304
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006305 case VAR_NUMBER:
6306 return tv1->vval.v_number == tv2->vval.v_number;
6307
6308 case VAR_STRING:
6309 s1 = get_tv_string_buf(tv1, buf1);
6310 s2 = get_tv_string_buf(tv2, buf2);
6311 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006312
6313 case VAR_SPECIAL:
6314 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006315
6316 case VAR_FLOAT:
6317#ifdef FEAT_FLOAT
6318 return tv1->vval.v_float == tv2->vval.v_float;
6319#endif
6320 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006321#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006322 return tv1->vval.v_job == tv2->vval.v_job;
6323#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006324 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006325#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006326 return tv1->vval.v_channel == tv2->vval.v_channel;
6327#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006328 case VAR_FUNC:
6329 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006330 case VAR_UNKNOWN:
6331 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006332 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006333
Bram Moolenaara03f2332016-02-06 18:09:59 +01006334 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6335 * does not equal anything, not even itself. */
6336 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006337}
6338
6339/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006340 * Locate item with index "n" in list "l" and return it.
6341 * A negative index is counted from the end; -1 is the last item.
6342 * Returns NULL when "n" is out of range.
6343 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006344 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346{
Bram Moolenaar33570922005-01-25 22:26:29 +00006347 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 long idx;
6349
6350 if (l == NULL)
6351 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006352
6353 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006354 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006355 n = l->lv_len + n;
6356
6357 /* Check for index out of range. */
6358 if (n < 0 || n >= l->lv_len)
6359 return NULL;
6360
6361 /* When there is a cached index may start search from there. */
6362 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006363 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006364 if (n < l->lv_idx / 2)
6365 {
6366 /* closest to the start of the list */
6367 item = l->lv_first;
6368 idx = 0;
6369 }
6370 else if (n > (l->lv_idx + l->lv_len) / 2)
6371 {
6372 /* closest to the end of the list */
6373 item = l->lv_last;
6374 idx = l->lv_len - 1;
6375 }
6376 else
6377 {
6378 /* closest to the cached index */
6379 item = l->lv_idx_item;
6380 idx = l->lv_idx;
6381 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006382 }
6383 else
6384 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006385 if (n < l->lv_len / 2)
6386 {
6387 /* closest to the start of the list */
6388 item = l->lv_first;
6389 idx = 0;
6390 }
6391 else
6392 {
6393 /* closest to the end of the list */
6394 item = l->lv_last;
6395 idx = l->lv_len - 1;
6396 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006398
6399 while (n > idx)
6400 {
6401 /* search forward */
6402 item = item->li_next;
6403 ++idx;
6404 }
6405 while (n < idx)
6406 {
6407 /* search backward */
6408 item = item->li_prev;
6409 --idx;
6410 }
6411
6412 /* cache the used index */
6413 l->lv_idx = idx;
6414 l->lv_idx_item = item;
6415
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006416 return item;
6417}
6418
6419/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006420 * Get list item "l[idx]" as a number.
6421 */
6422 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006423list_find_nr(
6424 list_T *l,
6425 long idx,
6426 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006427{
6428 listitem_T *li;
6429
6430 li = list_find(l, idx);
6431 if (li == NULL)
6432 {
6433 if (errorp != NULL)
6434 *errorp = TRUE;
6435 return -1L;
6436 }
6437 return get_tv_number_chk(&li->li_tv, errorp);
6438}
6439
6440/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006441 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6442 */
6443 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006445{
6446 listitem_T *li;
6447
6448 li = list_find(l, idx - 1);
6449 if (li == NULL)
6450 {
6451 EMSGN(_(e_listidx), idx);
6452 return NULL;
6453 }
6454 return get_tv_string(&li->li_tv);
6455}
6456
6457/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006458 * Locate "item" list "l" and return its index.
6459 * Returns -1 when "item" is not in the list.
6460 */
6461 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006463{
6464 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006465 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006466
6467 if (l == NULL)
6468 return -1;
6469 idx = 0;
6470 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6471 ++idx;
6472 if (li == NULL)
6473 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006474 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006475}
6476
6477/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006478 * Append item "item" to the end of list "l".
6479 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006480 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006481list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482{
6483 if (l->lv_last == NULL)
6484 {
6485 /* empty list */
6486 l->lv_first = item;
6487 l->lv_last = item;
6488 item->li_prev = NULL;
6489 }
6490 else
6491 {
6492 l->lv_last->li_next = item;
6493 item->li_prev = l->lv_last;
6494 l->lv_last = item;
6495 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006496 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 item->li_next = NULL;
6498}
6499
6500/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006505list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006507 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508
Bram Moolenaar05159a02005-02-26 23:04:13 +00006509 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006510 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006511 copy_tv(tv, &li->li_tv);
6512 list_append(l, li);
6513 return OK;
6514}
6515
6516/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006517 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006518 * Return FAIL when out of memory.
6519 */
6520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006521list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006522{
6523 listitem_T *li = listitem_alloc();
6524
6525 if (li == NULL)
6526 return FAIL;
6527 li->li_tv.v_type = VAR_DICT;
6528 li->li_tv.v_lock = 0;
6529 li->li_tv.vval.v_dict = dict;
6530 list_append(list, li);
6531 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 return OK;
6533}
6534
6535/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006536 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006537 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006538 * Returns FAIL when out of memory.
6539 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006542{
6543 listitem_T *li = listitem_alloc();
6544
6545 if (li == NULL)
6546 return FAIL;
6547 list_append(l, li);
6548 li->li_tv.v_type = VAR_STRING;
6549 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006550 if (str == NULL)
6551 li->li_tv.vval.v_string = NULL;
6552 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006553 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006554 return FAIL;
6555 return OK;
6556}
6557
6558/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006559 * Append "n" to list "l".
6560 * Returns FAIL when out of memory.
6561 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006562 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006563list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006564{
6565 listitem_T *li;
6566
6567 li = listitem_alloc();
6568 if (li == NULL)
6569 return FAIL;
6570 li->li_tv.v_type = VAR_NUMBER;
6571 li->li_tv.v_lock = 0;
6572 li->li_tv.vval.v_number = n;
6573 list_append(l, li);
6574 return OK;
6575}
6576
6577/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006578 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 * If "item" is NULL append at the end.
6580 * Return FAIL when out of memory.
6581 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006582 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006583list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584{
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006586
6587 if (ni == NULL)
6588 return FAIL;
6589 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006590 list_insert(l, ni, item);
6591 return OK;
6592}
6593
6594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006595list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006596{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006597 if (item == NULL)
6598 /* Append new item at end of list. */
6599 list_append(l, ni);
6600 else
6601 {
6602 /* Insert new item before existing item. */
6603 ni->li_prev = item->li_prev;
6604 ni->li_next = item;
6605 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006606 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006607 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006608 ++l->lv_idx;
6609 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006610 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006611 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006612 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 l->lv_idx_item = NULL;
6614 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006615 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006616 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006617 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006618}
6619
6620/*
6621 * Extend "l1" with "l2".
6622 * If "bef" is NULL append at the end, otherwise insert before this item.
6623 * Returns FAIL when out of memory.
6624 */
6625 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006626list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006627{
Bram Moolenaar33570922005-01-25 22:26:29 +00006628 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006629 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006630
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006631 /* We also quit the loop when we have inserted the original item count of
6632 * the list, avoid a hang when we extend a list with itself. */
6633 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006634 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6635 return FAIL;
6636 return OK;
6637}
6638
6639/*
6640 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6641 * Return FAIL when out of memory.
6642 */
6643 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006644list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006645{
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006648 if (l1 == NULL || l2 == NULL)
6649 return FAIL;
6650
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653 if (l == NULL)
6654 return FAIL;
6655 tv->v_type = VAR_LIST;
6656 tv->vval.v_list = l;
6657
6658 /* append all items from the second list */
6659 return list_extend(l, l2, NULL);
6660}
6661
6662/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006663 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006665 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006666 * Returns NULL when out of memory.
6667 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006668 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670{
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 list_T *copy;
6672 listitem_T *item;
6673 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006674
6675 if (orig == NULL)
6676 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677
6678 copy = list_alloc();
6679 if (copy != NULL)
6680 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006681 if (copyID != 0)
6682 {
6683 /* Do this before adding the items, because one of the items may
6684 * refer back to this list. */
6685 orig->lv_copyID = copyID;
6686 orig->lv_copylist = copy;
6687 }
6688 for (item = orig->lv_first; item != NULL && !got_int;
6689 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690 {
6691 ni = listitem_alloc();
6692 if (ni == NULL)
6693 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006694 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006695 {
6696 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6697 {
6698 vim_free(ni);
6699 break;
6700 }
6701 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704 list_append(copy, ni);
6705 }
6706 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006707 if (item != NULL)
6708 {
6709 list_unref(copy);
6710 copy = NULL;
6711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006712 }
6713
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006714 return copy;
6715}
6716
6717/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006718 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006719 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006720 * This used to be called list_remove, but that conflicts with a Sun header
6721 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006722 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006724vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006725{
Bram Moolenaar33570922005-01-25 22:26:29 +00006726 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006727
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006728 /* notify watchers */
6729 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006730 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006731 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732 list_fix_watch(l, ip);
6733 if (ip == item2)
6734 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006735 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006736
6737 if (item2->li_next == NULL)
6738 l->lv_last = item->li_prev;
6739 else
6740 item2->li_next->li_prev = item->li_prev;
6741 if (item->li_prev == NULL)
6742 l->lv_first = item2->li_next;
6743 else
6744 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006745 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006746}
6747
6748/*
6749 * Return an allocated string with the string representation of a list.
6750 * May return NULL.
6751 */
6752 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006753list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006754{
6755 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006756
6757 if (tv->vval.v_list == NULL)
6758 return NULL;
6759 ga_init2(&ga, (int)sizeof(char), 80);
6760 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006761 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006762 {
6763 vim_free(ga.ga_data);
6764 return NULL;
6765 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006766 ga_append(&ga, ']');
6767 ga_append(&ga, NUL);
6768 return (char_u *)ga.ga_data;
6769}
6770
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006771typedef struct join_S {
6772 char_u *s;
6773 char_u *tofree;
6774} join_T;
6775
6776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006777list_join_inner(
6778 garray_T *gap, /* to store the result in */
6779 list_T *l,
6780 char_u *sep,
6781 int echo_style,
6782 int copyID,
6783 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784{
6785 int i;
6786 join_T *p;
6787 int len;
6788 int sumlen = 0;
6789 int first = TRUE;
6790 char_u *tofree;
6791 char_u numbuf[NUMBUFLEN];
6792 listitem_T *item;
6793 char_u *s;
6794
6795 /* Stringify each item in the list. */
6796 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6797 {
6798 if (echo_style)
6799 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6800 else
6801 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6802 if (s == NULL)
6803 return FAIL;
6804
6805 len = (int)STRLEN(s);
6806 sumlen += len;
6807
Bram Moolenaarcde88542015-08-11 19:14:00 +02006808 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006809 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6810 if (tofree != NULL || s != numbuf)
6811 {
6812 p->s = s;
6813 p->tofree = tofree;
6814 }
6815 else
6816 {
6817 p->s = vim_strnsave(s, len);
6818 p->tofree = p->s;
6819 }
6820
6821 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006822 if (did_echo_string_emsg) /* recursion error, bail out */
6823 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006824 }
6825
6826 /* Allocate result buffer with its total size, avoid re-allocation and
6827 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6828 if (join_gap->ga_len >= 2)
6829 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6830 if (ga_grow(gap, sumlen + 2) == FAIL)
6831 return FAIL;
6832
6833 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6834 {
6835 if (first)
6836 first = FALSE;
6837 else
6838 ga_concat(gap, sep);
6839 p = ((join_T *)join_gap->ga_data) + i;
6840
6841 if (p->s != NULL)
6842 ga_concat(gap, p->s);
6843 line_breakcheck();
6844 }
6845
6846 return OK;
6847}
6848
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006849/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006850 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006851 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006852 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006853 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006855list_join(
6856 garray_T *gap,
6857 list_T *l,
6858 char_u *sep,
6859 int echo_style,
6860 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006861{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006862 garray_T join_ga;
6863 int retval;
6864 join_T *p;
6865 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006866
Bram Moolenaard39a7512015-04-16 22:51:22 +02006867 if (l->lv_len < 1)
6868 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006869 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6870 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6871
6872 /* Dispose each item in join_ga. */
6873 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006874 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006875 p = (join_T *)join_ga.ga_data;
6876 for (i = 0; i < join_ga.ga_len; ++i)
6877 {
6878 vim_free(p->tofree);
6879 ++p;
6880 }
6881 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006882 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006883
6884 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006885}
6886
6887/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006888 * Return the next (unique) copy ID.
6889 * Used for serializing nested structures.
6890 */
6891 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006893{
6894 current_copyID += COPYID_INC;
6895 return current_copyID;
6896}
6897
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006898/* Used by get_func_tv() */
6899static garray_T funcargs = GA_EMPTY;
6900
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006901/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006902 * Garbage collection for lists and dictionaries.
6903 *
6904 * We use reference counts to be able to free most items right away when they
6905 * are no longer used. But for composite items it's possible that it becomes
6906 * unused while the reference count is > 0: When there is a recursive
6907 * reference. Example:
6908 * :let l = [1, 2, 3]
6909 * :let d = {9: l}
6910 * :let l[1] = d
6911 *
6912 * Since this is quite unusual we handle this with garbage collection: every
6913 * once in a while find out which lists and dicts are not referenced from any
6914 * variable.
6915 *
6916 * Here is a good reference text about garbage collection (refers to Python
6917 * but it applies to all reference-counting mechanisms):
6918 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006919 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006920
6921/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006922 * Do garbage collection for lists and dicts.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006923 * When "testing" is TRUE this is called from garbagecollect_for_testing().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006924 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006925 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006926 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006927garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006928{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006929 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006930 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931 buf_T *buf;
6932 win_T *wp;
6933 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006934 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006935 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006936 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006937#ifdef FEAT_WINDOWS
6938 tabpage_T *tp;
6939#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006940
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006941 if (!testing)
6942 {
6943 /* Only do this once. */
6944 want_garbage_collect = FALSE;
6945 may_garbage_collect = FALSE;
6946 garbage_collect_at_exit = FALSE;
6947 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006948
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 /* We advance by two because we add one for items referenced through
6950 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006951 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006952
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006953 /*
6954 * 1. Go through all accessible variables and mark all lists and dicts
6955 * with copyID.
6956 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957
6958 /* Don't free variables in the previous_funccal list unless they are only
6959 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006960 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006961 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6962 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6964 NULL);
6965 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6966 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 }
6968
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006969 /* script-local variables */
6970 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006971 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972
6973 /* buffer-local variables */
6974 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006975 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6976 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006977
6978 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006979 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006980 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6981 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006982#ifdef FEAT_AUTOCMD
6983 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006984 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6985 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006986#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006987
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006988#ifdef FEAT_WINDOWS
6989 /* tabpage-local variables */
6990 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006991 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6992 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006993#endif
6994
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006995 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006996 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997
6998 /* function-local variables */
6999 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7000 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007001 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7002 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007003 }
7004
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007005 /* function call arguments, if v:testing is set. */
7006 for (i = 0; i < funcargs.ga_len; ++i)
7007 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7008 copyID, NULL, NULL);
7009
Bram Moolenaard812df62008-11-09 12:46:09 +00007010 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007011 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007012
Bram Moolenaar1dced572012-04-05 16:54:08 +02007013#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007014 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007015#endif
7016
Bram Moolenaardb913952012-06-29 12:54:53 +02007017#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007018 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007019#endif
7020
7021#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007022 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007023#endif
7024
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007025#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007026 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007027#endif
7028
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007029 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007030 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007031 /*
7032 * 2. Free lists and dictionaries that are not referenced.
7033 */
7034 did_free = free_unref_items(copyID);
7035
7036 /*
7037 * 3. Check if any funccal can be freed now.
7038 */
7039 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007040 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007041 if (can_free_funccal(*pfc, copyID))
7042 {
7043 fc = *pfc;
7044 *pfc = fc->caller;
7045 free_funccal(fc, TRUE);
7046 did_free = TRUE;
7047 did_free_funccal = TRUE;
7048 }
7049 else
7050 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007051 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 if (did_free_funccal)
7053 /* When a funccal was freed some more items might be garbage
7054 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007055 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007056 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 else if (p_verbose > 0)
7058 {
7059 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7060 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007061
7062 return did_free;
7063}
7064
7065/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007066 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007067 */
7068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007070{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007071 dict_T *dd, *dd_next;
7072 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007073 int did_free = FALSE;
7074
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007075 /* Let all "free" functions know that we are here. This means no
7076 * dictionaries, lists, channels or jobs are to be freed, because we will
7077 * do that here. */
7078 in_free_unref_items = TRUE;
7079
7080 /*
7081 * PASS 1: free the contents of the items. We don't free the items
7082 * themselves yet, so that it is possible to decrement refcount counters
7083 */
7084
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007086 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007088 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007089 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007091 /* Free the Dictionary and ordinary items it contains, but don't
7092 * recurse into Lists and Dictionaries, they will be in the list
7093 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007094 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007095 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097
7098 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007099 * Go through the list of lists and free items without the copyID.
7100 * But don't free a list that has a watcher (used in a for loop), these
7101 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007102 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007103 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7104 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7105 && ll->lv_watch == NULL)
7106 {
7107 /* Free the List and ordinary items it contains, but don't recurse
7108 * into Lists and Dictionaries, they will be in the list of dicts
7109 * or list of lists. */
7110 list_free_contents(ll);
7111 did_free = TRUE;
7112 }
7113
7114#ifdef FEAT_JOB_CHANNEL
7115 /* Go through the list of jobs and free items without the copyID. This
7116 * must happen before doing channels, because jobs refer to channels, but
7117 * the reference from the channel to the job isn't tracked. */
7118 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7119
7120 /* Go through the list of channels and free items without the copyID. */
7121 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7122#endif
7123
7124 /*
7125 * PASS 2: free the items themselves.
7126 */
7127 for (dd = first_dict; dd != NULL; dd = dd_next)
7128 {
7129 dd_next = dd->dv_used_next;
7130 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7131 dict_free_dict(dd);
7132 }
7133
7134 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007135 {
7136 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007137 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7138 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007140 /* Free the List and ordinary items it contains, but don't recurse
7141 * into Lists and Dictionaries, they will be in the list of dicts
7142 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007143 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007144 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007145 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007146
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007147#ifdef FEAT_JOB_CHANNEL
7148 /* Go through the list of jobs and free items without the copyID. This
7149 * must happen before doing channels, because jobs refer to channels, but
7150 * the reference from the channel to the job isn't tracked. */
7151 free_unused_jobs(copyID, COPYID_MASK);
7152
7153 /* Go through the list of channels and free items without the copyID. */
7154 free_unused_channels(copyID, COPYID_MASK);
7155#endif
7156
7157 in_free_unref_items = FALSE;
7158
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007159 return did_free;
7160}
7161
7162/*
7163 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007164 * "list_stack" is used to add lists to be marked. Can be NULL.
7165 *
7166 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007167 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007169set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007170{
7171 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007173 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007174 hashtab_T *cur_ht;
7175 ht_stack_T *ht_stack = NULL;
7176 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007177
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 cur_ht = ht;
7179 for (;;)
7180 {
7181 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007183 /* Mark each item in the hashtab. If the item contains a hashtab
7184 * it is added to ht_stack, if it contains a list it is added to
7185 * list_stack. */
7186 todo = (int)cur_ht->ht_used;
7187 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7188 if (!HASHITEM_EMPTY(hi))
7189 {
7190 --todo;
7191 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7192 &ht_stack, list_stack);
7193 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007195
7196 if (ht_stack == NULL)
7197 break;
7198
7199 /* take an item from the stack */
7200 cur_ht = ht_stack->ht;
7201 tempitem = ht_stack;
7202 ht_stack = ht_stack->prev;
7203 free(tempitem);
7204 }
7205
7206 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007207}
7208
7209/*
7210 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007211 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7212 *
7213 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007214 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007216set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007218 listitem_T *li;
7219 int abort = FALSE;
7220 list_T *cur_l;
7221 list_stack_T *list_stack = NULL;
7222 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007223
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007224 cur_l = l;
7225 for (;;)
7226 {
7227 if (!abort)
7228 /* Mark each item in the list. If the item contains a hashtab
7229 * it is added to ht_stack, if it contains a list it is added to
7230 * list_stack. */
7231 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7232 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7233 ht_stack, &list_stack);
7234 if (list_stack == NULL)
7235 break;
7236
7237 /* take an item from the stack */
7238 cur_l = list_stack->list;
7239 tempitem = list_stack;
7240 list_stack = list_stack->prev;
7241 free(tempitem);
7242 }
7243
7244 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245}
7246
7247/*
7248 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007249 * "list_stack" is used to add lists to be marked. Can be NULL.
7250 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7251 *
7252 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007253 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007254 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007255set_ref_in_item(
7256 typval_T *tv,
7257 int copyID,
7258 ht_stack_T **ht_stack,
7259 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007260{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007261 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007262
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007263 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007264 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007265 dict_T *dd = tv->vval.v_dict;
7266
Bram Moolenaara03f2332016-02-06 18:09:59 +01007267 if (dd != NULL && dd->dv_copyID != copyID)
7268 {
7269 /* Didn't see this dict yet. */
7270 dd->dv_copyID = copyID;
7271 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007272 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007273 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7274 }
7275 else
7276 {
7277 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7278 if (newitem == NULL)
7279 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007280 else
7281 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007282 newitem->ht = &dd->dv_hashtab;
7283 newitem->prev = *ht_stack;
7284 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007285 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007286 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007287 }
7288 }
7289 else if (tv->v_type == VAR_LIST)
7290 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007291 list_T *ll = tv->vval.v_list;
7292
Bram Moolenaara03f2332016-02-06 18:09:59 +01007293 if (ll != NULL && ll->lv_copyID != copyID)
7294 {
7295 /* Didn't see this list yet. */
7296 ll->lv_copyID = copyID;
7297 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007298 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007299 abort = set_ref_in_list(ll, copyID, ht_stack);
7300 }
7301 else
7302 {
7303 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007304 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007305 if (newitem == NULL)
7306 abort = TRUE;
7307 else
7308 {
7309 newitem->list = ll;
7310 newitem->prev = *list_stack;
7311 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007312 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007313 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007314 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007315 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007316 else if (tv->v_type == VAR_PARTIAL)
7317 {
7318 partial_T *pt = tv->vval.v_partial;
7319 int i;
7320
7321 /* A partial does not have a copyID, because it cannot contain itself.
7322 */
7323 if (pt != NULL)
7324 {
7325 if (pt->pt_dict != NULL)
7326 {
7327 typval_T dtv;
7328
7329 dtv.v_type = VAR_DICT;
7330 dtv.vval.v_dict = pt->pt_dict;
7331 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7332 }
7333
7334 for (i = 0; i < pt->pt_argc; ++i)
7335 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7336 ht_stack, list_stack);
7337 }
7338 }
7339#ifdef FEAT_JOB_CHANNEL
7340 else if (tv->v_type == VAR_JOB)
7341 {
7342 job_T *job = tv->vval.v_job;
7343 typval_T dtv;
7344
7345 if (job != NULL && job->jv_copyID != copyID)
7346 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007347 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007348 if (job->jv_channel != NULL)
7349 {
7350 dtv.v_type = VAR_CHANNEL;
7351 dtv.vval.v_channel = job->jv_channel;
7352 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7353 }
7354 if (job->jv_exit_partial != NULL)
7355 {
7356 dtv.v_type = VAR_PARTIAL;
7357 dtv.vval.v_partial = job->jv_exit_partial;
7358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7359 }
7360 }
7361 }
7362 else if (tv->v_type == VAR_CHANNEL)
7363 {
7364 channel_T *ch =tv->vval.v_channel;
7365 int part;
7366 typval_T dtv;
7367 jsonq_T *jq;
7368 cbq_T *cq;
7369
7370 if (ch != NULL && ch->ch_copyID != copyID)
7371 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007372 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007373 for (part = PART_SOCK; part <= PART_IN; ++part)
7374 {
7375 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7376 jq = jq->jq_next)
7377 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7378 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7379 cq = cq->cq_next)
7380 if (cq->cq_partial != NULL)
7381 {
7382 dtv.v_type = VAR_PARTIAL;
7383 dtv.vval.v_partial = cq->cq_partial;
7384 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7385 }
7386 if (ch->ch_part[part].ch_partial != NULL)
7387 {
7388 dtv.v_type = VAR_PARTIAL;
7389 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7390 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7391 }
7392 }
7393 if (ch->ch_partial != NULL)
7394 {
7395 dtv.v_type = VAR_PARTIAL;
7396 dtv.vval.v_partial = ch->ch_partial;
7397 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7398 }
7399 if (ch->ch_close_partial != NULL)
7400 {
7401 dtv.v_type = VAR_PARTIAL;
7402 dtv.vval.v_partial = ch->ch_close_partial;
7403 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7404 }
7405 }
7406 }
7407#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007408 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007409}
7410
7411/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 * Allocate an empty header for a dictionary.
7413 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007414 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007415dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416{
Bram Moolenaar33570922005-01-25 22:26:29 +00007417 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007418
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007420 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007421 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007422 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007423 if (first_dict != NULL)
7424 first_dict->dv_used_prev = d;
7425 d->dv_used_next = first_dict;
7426 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007427 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007428
Bram Moolenaar33570922005-01-25 22:26:29 +00007429 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007430 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007431 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007432 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007434 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007436}
7437
7438/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007439 * Allocate an empty dict for a return value.
7440 * Returns OK or FAIL.
7441 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007442 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007443rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007444{
7445 dict_T *d = dict_alloc();
7446
7447 if (d == NULL)
7448 return FAIL;
7449
7450 rettv->vval.v_dict = d;
7451 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007452 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007453 ++d->dv_refcount;
7454 return OK;
7455}
7456
7457
7458/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 * Unreference a Dictionary: decrement the reference count and free it when it
7460 * becomes zero.
7461 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007462 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007464{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007465 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007466 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467}
7468
7469/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007470 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007471 * Ignores the reference count.
7472 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007473 static void
7474dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007476 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007477 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007478 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007479
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007480 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007481 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007482 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 if (!HASHITEM_EMPTY(hi))
7486 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007487 /* Remove the item before deleting it, just in case there is
7488 * something recursive causing trouble. */
7489 di = HI2DI(hi);
7490 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007491 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007492 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 --todo;
7494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007497}
7498
7499 static void
7500dict_free_dict(dict_T *d)
7501{
7502 /* Remove the dict from the list of dicts for garbage collection. */
7503 if (d->dv_used_prev == NULL)
7504 first_dict = d->dv_used_next;
7505 else
7506 d->dv_used_prev->dv_used_next = d->dv_used_next;
7507 if (d->dv_used_next != NULL)
7508 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 vim_free(d);
7510}
7511
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007512 void
7513dict_free(dict_T *d)
7514{
7515 if (!in_free_unref_items)
7516 {
7517 dict_free_contents(d);
7518 dict_free_dict(d);
7519 }
7520}
7521
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522/*
7523 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007524 * The "key" is copied to the new item.
7525 * Note that the value of the item "di_tv" still needs to be initialized!
7526 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007528 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530{
Bram Moolenaar33570922005-01-25 22:26:29 +00007531 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007533 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007534 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007535 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007536 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007537 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007540}
7541
7542/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007543 * Make a copy of a Dictionary item.
7544 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007545 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007547{
Bram Moolenaar33570922005-01-25 22:26:29 +00007548 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007550 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7551 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 if (di != NULL)
7553 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007554 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007555 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556 copy_tv(&org->di_tv, &di->di_tv);
7557 }
7558 return di;
7559}
7560
7561/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007562 * Remove item "item" from Dictionary "dict" and free it.
7563 */
7564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007565dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007566{
Bram Moolenaar33570922005-01-25 22:26:29 +00007567 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007568
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570 if (HASHITEM_EMPTY(hi))
7571 EMSG2(_(e_intern2), "dictitem_remove()");
7572 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007573 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 dictitem_free(item);
7575}
7576
7577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 * Free a dict item. Also clears the value.
7579 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007580 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007581dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007582{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007584 if (item->di_flags & DI_FLAGS_ALLOC)
7585 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007586}
7587
7588/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7590 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007591 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007592 * Returns NULL when out of memory.
7593 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007595dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007596{
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 dict_T *copy;
7598 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007600 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007601
7602 if (orig == NULL)
7603 return NULL;
7604
7605 copy = dict_alloc();
7606 if (copy != NULL)
7607 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (copyID != 0)
7609 {
7610 orig->dv_copyID = copyID;
7611 orig->dv_copydict = copy;
7612 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007613 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007614 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007615 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007616 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007618 --todo;
7619
7620 di = dictitem_alloc(hi->hi_key);
7621 if (di == NULL)
7622 break;
7623 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007624 {
7625 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7626 copyID) == FAIL)
7627 {
7628 vim_free(di);
7629 break;
7630 }
7631 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 else
7633 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7634 if (dict_add(copy, di) == FAIL)
7635 {
7636 dictitem_free(di);
7637 break;
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007641
Bram Moolenaare9a41262005-01-15 22:18:47 +00007642 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007643 if (todo > 0)
7644 {
7645 dict_unref(copy);
7646 copy = NULL;
7647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007648 }
7649
7650 return copy;
7651}
7652
7653/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007655 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007657 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007658dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007659{
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661}
7662
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007664 * Add a number or string entry to dictionary "d".
7665 * When "str" is NULL use number "nr", otherwise use "str".
7666 * Returns FAIL when out of memory and when key already exists.
7667 */
7668 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007669dict_add_nr_str(
7670 dict_T *d,
7671 char *key,
7672 long nr,
7673 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007674{
7675 dictitem_T *item;
7676
7677 item = dictitem_alloc((char_u *)key);
7678 if (item == NULL)
7679 return FAIL;
7680 item->di_tv.v_lock = 0;
7681 if (str == NULL)
7682 {
7683 item->di_tv.v_type = VAR_NUMBER;
7684 item->di_tv.vval.v_number = nr;
7685 }
7686 else
7687 {
7688 item->di_tv.v_type = VAR_STRING;
7689 item->di_tv.vval.v_string = vim_strsave(str);
7690 }
7691 if (dict_add(d, item) == FAIL)
7692 {
7693 dictitem_free(item);
7694 return FAIL;
7695 }
7696 return OK;
7697}
7698
7699/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007700 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007701 * Returns FAIL when out of memory and when key already exists.
7702 */
7703 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007704dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007705{
7706 dictitem_T *item;
7707
7708 item = dictitem_alloc((char_u *)key);
7709 if (item == NULL)
7710 return FAIL;
7711 item->di_tv.v_lock = 0;
7712 item->di_tv.v_type = VAR_LIST;
7713 item->di_tv.vval.v_list = list;
7714 if (dict_add(d, item) == FAIL)
7715 {
7716 dictitem_free(item);
7717 return FAIL;
7718 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007719 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007720 return OK;
7721}
7722
7723/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 * Get the number of items in a Dictionary.
7725 */
7726 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007727dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007728{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007729 if (d == NULL)
7730 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007731 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007732}
7733
7734/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007735 * Find item "key[len]" in Dictionary "d".
7736 * If "len" is negative use strlen(key).
7737 * Returns NULL when not found.
7738 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007739 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007740dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007741{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007742#define AKEYLEN 200
7743 char_u buf[AKEYLEN];
7744 char_u *akey;
7745 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007746 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007748 if (len < 0)
7749 akey = key;
7750 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007751 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007752 tofree = akey = vim_strnsave(key, len);
7753 if (akey == NULL)
7754 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007755 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007756 else
7757 {
7758 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007759 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007760 akey = buf;
7761 }
7762
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007764 vim_free(tofree);
7765 if (HASHITEM_EMPTY(hi))
7766 return NULL;
7767 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007768}
7769
7770/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007771 * Get a string item from a dictionary.
7772 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007773 * Returns NULL if the entry doesn't exist or out of memory.
7774 */
7775 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007776get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007777{
7778 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007779 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007780
7781 di = dict_find(d, key, -1);
7782 if (di == NULL)
7783 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007784 s = get_tv_string(&di->di_tv);
7785 if (save && s != NULL)
7786 s = vim_strsave(s);
7787 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007788}
7789
7790/*
7791 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007792 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007793 */
7794 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007795get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007796{
7797 dictitem_T *di;
7798
7799 di = dict_find(d, key, -1);
7800 if (di == NULL)
7801 return 0;
7802 return get_tv_number(&di->di_tv);
7803}
7804
7805/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806 * Return an allocated string with the string representation of a Dictionary.
7807 * May return NULL.
7808 */
7809 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007810dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007811{
7812 garray_T ga;
7813 int first = TRUE;
7814 char_u *tofree;
7815 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007816 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007817 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007818 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007819 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007820
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007821 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007822 return NULL;
7823 ga_init2(&ga, (int)sizeof(char), 80);
7824 ga_append(&ga, '{');
7825
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007826 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007827 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007828 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007829 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007830 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007831 --todo;
7832
7833 if (first)
7834 first = FALSE;
7835 else
7836 ga_concat(&ga, (char_u *)", ");
7837
7838 tofree = string_quote(hi->hi_key, FALSE);
7839 if (tofree != NULL)
7840 {
7841 ga_concat(&ga, tofree);
7842 vim_free(tofree);
7843 }
7844 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007846 if (s != NULL)
7847 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007848 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007849 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007850 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007851 line_breakcheck();
7852
Bram Moolenaar8c711452005-01-14 21:53:12 +00007853 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007854 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007855 if (todo > 0)
7856 {
7857 vim_free(ga.ga_data);
7858 return NULL;
7859 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007860
7861 ga_append(&ga, '}');
7862 ga_append(&ga, NUL);
7863 return (char_u *)ga.ga_data;
7864}
7865
7866/*
7867 * Allocate a variable for a Dictionary and fill it from "*arg".
7868 * Return OK or FAIL. Returns NOTDONE for {expr}.
7869 */
7870 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007871get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007872{
Bram Moolenaar33570922005-01-25 22:26:29 +00007873 dict_T *d = NULL;
7874 typval_T tvkey;
7875 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007876 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007877 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007879 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007880
7881 /*
7882 * First check if it's not a curly-braces thing: {expr}.
7883 * Must do this without evaluating, otherwise a function may be called
7884 * twice. Unfortunately this means we need to call eval1() twice for the
7885 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007886 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007887 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007888 if (*start != '}')
7889 {
7890 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7891 return FAIL;
7892 if (*start == '}')
7893 return NOTDONE;
7894 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007895
7896 if (evaluate)
7897 {
7898 d = dict_alloc();
7899 if (d == NULL)
7900 return FAIL;
7901 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007902 tvkey.v_type = VAR_UNKNOWN;
7903 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007904
7905 *arg = skipwhite(*arg + 1);
7906 while (**arg != '}' && **arg != NUL)
7907 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007908 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007909 goto failret;
7910 if (**arg != ':')
7911 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007912 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007913 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007914 goto failret;
7915 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007916 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007917 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007918 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02007919 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00007920 {
7921 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00007922 clear_tv(&tvkey);
7923 goto failret;
7924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007926
7927 *arg = skipwhite(*arg + 1);
7928 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7929 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007930 if (evaluate)
7931 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007932 goto failret;
7933 }
7934 if (evaluate)
7935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007936 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937 if (item != NULL)
7938 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007939 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007940 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 clear_tv(&tv);
7942 goto failret;
7943 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007944 item = dictitem_alloc(key);
7945 clear_tv(&tvkey);
7946 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007947 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007949 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007950 if (dict_add(d, item) == FAIL)
7951 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007952 }
7953 }
7954
7955 if (**arg == '}')
7956 break;
7957 if (**arg != ',')
7958 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007959 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007960 goto failret;
7961 }
7962 *arg = skipwhite(*arg + 1);
7963 }
7964
7965 if (**arg != '}')
7966 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007967 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007968failret:
7969 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007970 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 return FAIL;
7972 }
7973
7974 *arg = skipwhite(*arg + 1);
7975 if (evaluate)
7976 {
7977 rettv->v_type = VAR_DICT;
7978 rettv->vval.v_dict = d;
7979 ++d->dv_refcount;
7980 }
7981
7982 return OK;
7983}
7984
Bram Moolenaar17a13432016-01-24 14:22:10 +01007985 static char *
7986get_var_special_name(int nr)
7987{
7988 switch (nr)
7989 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007990 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007991 case VVAL_TRUE: return "v:true";
7992 case VVAL_NONE: return "v:none";
7993 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007994 }
7995 EMSG2(_(e_intern2), "get_var_special_name()");
7996 return "42";
7997}
7998
Bram Moolenaar8c711452005-01-14 21:53:12 +00007999/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008000 * Return a string with the string representation of a variable.
8001 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008002 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008003 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008004 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008005 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008006 */
8007 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008008echo_string(
8009 typval_T *tv,
8010 char_u **tofree,
8011 char_u *numbuf,
8012 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008013{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 static int recurse = 0;
8015 char_u *r = NULL;
8016
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008019 if (!did_echo_string_emsg)
8020 {
8021 /* Only give this message once for a recursive call to avoid
8022 * flooding the user with errors. And stop iterating over lists
8023 * and dicts. */
8024 did_echo_string_emsg = TRUE;
8025 EMSG(_("E724: variable nested too deep for displaying"));
8026 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008028 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008029 }
8030 ++recurse;
8031
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008032 switch (tv->v_type)
8033 {
8034 case VAR_FUNC:
8035 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008036 r = tv->vval.v_string;
8037 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008038
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008039 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008040 {
8041 partial_T *pt = tv->vval.v_partial;
8042 char_u *fname = string_quote(pt == NULL ? NULL
8043 : pt->pt_name, FALSE);
8044 garray_T ga;
8045 int i;
8046 char_u *tf;
8047
8048 ga_init2(&ga, 1, 100);
8049 ga_concat(&ga, (char_u *)"function(");
8050 if (fname != NULL)
8051 {
8052 ga_concat(&ga, fname);
8053 vim_free(fname);
8054 }
8055 if (pt != NULL && pt->pt_argc > 0)
8056 {
8057 ga_concat(&ga, (char_u *)", [");
8058 for (i = 0; i < pt->pt_argc; ++i)
8059 {
8060 if (i > 0)
8061 ga_concat(&ga, (char_u *)", ");
8062 ga_concat(&ga,
8063 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8064 vim_free(tf);
8065 }
8066 ga_concat(&ga, (char_u *)"]");
8067 }
8068 if (pt != NULL && pt->pt_dict != NULL)
8069 {
8070 typval_T dtv;
8071
8072 ga_concat(&ga, (char_u *)", ");
8073 dtv.v_type = VAR_DICT;
8074 dtv.vval.v_dict = pt->pt_dict;
8075 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8076 vim_free(tf);
8077 }
8078 ga_concat(&ga, (char_u *)")");
8079
8080 *tofree = ga.ga_data;
8081 r = *tofree;
8082 break;
8083 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008084
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008085 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008086 if (tv->vval.v_list == NULL)
8087 {
8088 *tofree = NULL;
8089 r = NULL;
8090 }
8091 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
8092 {
8093 *tofree = NULL;
8094 r = (char_u *)"[...]";
8095 }
8096 else
8097 {
8098 tv->vval.v_list->lv_copyID = copyID;
8099 *tofree = list2string(tv, copyID);
8100 r = *tofree;
8101 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008102 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008103
Bram Moolenaar8c711452005-01-14 21:53:12 +00008104 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008105 if (tv->vval.v_dict == NULL)
8106 {
8107 *tofree = NULL;
8108 r = NULL;
8109 }
8110 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
8111 {
8112 *tofree = NULL;
8113 r = (char_u *)"{...}";
8114 }
8115 else
8116 {
8117 tv->vval.v_dict->dv_copyID = copyID;
8118 *tofree = dict2string(tv, copyID);
8119 r = *tofree;
8120 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008122
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123 case VAR_STRING:
8124 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008125 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008126 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008127 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008128 *tofree = NULL;
8129 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008130 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008131
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008132 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008133#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008134 *tofree = NULL;
8135 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8136 r = numbuf;
8137 break;
8138#endif
8139
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008140 case VAR_SPECIAL:
8141 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008142 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008143 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008144 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008145
Bram Moolenaar8502c702014-06-17 12:51:16 +02008146 if (--recurse == 0)
8147 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008148 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149}
8150
8151/*
8152 * Return a string with the string representation of a variable.
8153 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8154 * "numbuf" is used for a number.
8155 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008156 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008157 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008158 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008159tv2string(
8160 typval_T *tv,
8161 char_u **tofree,
8162 char_u *numbuf,
8163 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008164{
8165 switch (tv->v_type)
8166 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008167 case VAR_FUNC:
8168 *tofree = string_quote(tv->vval.v_string, TRUE);
8169 return *tofree;
8170 case VAR_STRING:
8171 *tofree = string_quote(tv->vval.v_string, FALSE);
8172 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008173 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008174#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008175 *tofree = NULL;
8176 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8177 return numbuf;
8178#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008180 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008181 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008182 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008183 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008184 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008185 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008186 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008187 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008189 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008190}
8191
8192/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 * Return string "str" in ' quotes, doubling ' characters.
8194 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008195 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008196 */
8197 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008198string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008199{
Bram Moolenaar33570922005-01-25 22:26:29 +00008200 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008201 char_u *p, *r, *s;
8202
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 len = (function ? 13 : 3);
8204 if (str != NULL)
8205 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008206 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008207 for (p = str; *p != NUL; mb_ptr_adv(p))
8208 if (*p == '\'')
8209 ++len;
8210 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008211 s = r = alloc(len);
8212 if (r != NULL)
8213 {
8214 if (function)
8215 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008216 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008217 r += 10;
8218 }
8219 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008220 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008221 if (str != NULL)
8222 for (p = str; *p != NUL; )
8223 {
8224 if (*p == '\'')
8225 *r++ = '\'';
8226 MB_COPY_CHAR(p, r);
8227 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008228 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008229 if (function)
8230 *r++ = ')';
8231 *r++ = NUL;
8232 }
8233 return s;
8234}
8235
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008236#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237/*
8238 * Convert the string "text" to a floating point number.
8239 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8240 * this always uses a decimal point.
8241 * Returns the length of the text that was consumed.
8242 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008244string2float(
8245 char_u *text,
8246 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008247{
8248 char *s = (char *)text;
8249 float_T f;
8250
8251 f = strtod(s, &s);
8252 *value = f;
8253 return (int)((char_u *)s - text);
8254}
8255#endif
8256
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 * Get the value of an environment variable.
8259 * "arg" is pointing to the '$'. It is advanced to after the name.
8260 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008261 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 */
8263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008264get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 char_u *string = NULL;
8267 int len;
8268 int cc;
8269 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008270 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271
8272 ++*arg;
8273 name = *arg;
8274 len = get_env_len(arg);
8275 if (evaluate)
8276 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008277 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008278 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008279
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008280 cc = name[len];
8281 name[len] = NUL;
8282 /* first try vim_getenv(), fast for normal environment vars */
8283 string = vim_getenv(name, &mustfree);
8284 if (string != NULL && *string != NUL)
8285 {
8286 if (!mustfree)
8287 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008289 else
8290 {
8291 if (mustfree)
8292 vim_free(string);
8293
8294 /* next try expanding things like $VIM and ${HOME} */
8295 string = expand_env_save(name - 1);
8296 if (string != NULL && *string == '$')
8297 {
8298 vim_free(string);
8299 string = NULL;
8300 }
8301 }
8302 name[len] = cc;
8303
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 rettv->v_type = VAR_STRING;
8305 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307
8308 return OK;
8309}
8310
8311/*
8312 * Array with names and number of arguments of all internal functions
8313 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8314 */
8315static struct fst
8316{
8317 char *f_name; /* function name */
8318 char f_min_argc; /* minimal number of arguments */
8319 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008320 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008321 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322} functions[] =
8323{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008324#ifdef FEAT_FLOAT
8325 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008326 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008327#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008328 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008329 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008330 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {"append", 2, 2, f_append},
8332 {"argc", 0, 0, f_argc},
8333 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008334 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008335 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008336#ifdef FEAT_FLOAT
8337 {"asin", 1, 1, f_asin}, /* WJMc */
8338#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008339 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008340 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008341 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008342 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008343 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008344 {"assert_notequal", 2, 3, f_assert_notequal},
8345 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008346 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008347#ifdef FEAT_FLOAT
8348 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008349 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008352 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"bufexists", 1, 1, f_bufexists},
8354 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8355 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8356 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8357 {"buflisted", 1, 1, f_buflisted},
8358 {"bufloaded", 1, 1, f_bufloaded},
8359 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008360 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"bufwinnr", 1, 1, f_bufwinnr},
8362 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008363 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008364 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008365 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008366#ifdef FEAT_FLOAT
8367 {"ceil", 1, 1, f_ceil},
8368#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008369#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008370 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008371 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8372 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008373 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008374 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008375 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008376 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008377 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008378 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008379 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008380 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008381 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8382 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008383 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008384 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008385#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008386 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008387 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008389 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008391#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008392 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008393 {"complete_add", 1, 1, f_complete_add},
8394 {"complete_check", 0, 0, f_complete_check},
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008397 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008398#ifdef FEAT_FLOAT
8399 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008400 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008401#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008402 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008404 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008405 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008406 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008408 {"diff_filler", 1, 1, f_diff_filler},
8409 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008410 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008411 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008413 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"eventhandler", 0, 0, f_eventhandler},
8415 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008416 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008418#ifdef FEAT_FLOAT
8419 {"exp", 1, 1, f_exp},
8420#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008421 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008422 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008423 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8425 {"filereadable", 1, 1, f_filereadable},
8426 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008427 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008428 {"finddir", 1, 3, f_finddir},
8429 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008430#ifdef FEAT_FLOAT
8431 {"float2nr", 1, 1, f_float2nr},
8432 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008433 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008434#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008435 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"fnamemodify", 2, 2, f_fnamemodify},
8437 {"foldclosed", 1, 1, f_foldclosed},
8438 {"foldclosedend", 1, 1, f_foldclosedend},
8439 {"foldlevel", 1, 1, f_foldlevel},
8440 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008441 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008443 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008444 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008445 {"garbagecollect_for_testing", 0, 0, f_garbagecollect_for_testing},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008446 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008447 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008448 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"getchar", 0, 1, f_getchar},
8450 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008451 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"getcmdline", 0, 0, f_getcmdline},
8453 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008454 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008455 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008456 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008457 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008458 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008459 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"getfsize", 1, 1, f_getfsize},
8461 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008462 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008463 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008464 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008465 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008466 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008467 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008468 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008469 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008471 {"gettabvar", 2, 3, f_gettabvar},
8472 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {"getwinposx", 0, 0, f_getwinposx},
8474 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008475 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008476 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008477 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008478 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008480 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008481 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008482 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8484 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8485 {"histadd", 2, 2, f_histadd},
8486 {"histdel", 1, 2, f_histdel},
8487 {"histget", 1, 2, f_histget},
8488 {"histnr", 1, 1, f_histnr},
8489 {"hlID", 1, 1, f_hlID},
8490 {"hlexists", 1, 1, f_hlexists},
8491 {"hostname", 0, 0, f_hostname},
8492 {"iconv", 3, 3, f_iconv},
8493 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008494 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008495 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008497 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 {"inputrestore", 0, 0, f_inputrestore},
8499 {"inputsave", 0, 0, f_inputsave},
8500 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008501 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008502 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008504 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008505#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8506 {"isnan", 1, 1, f_isnan},
8507#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008508 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008509#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008510 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008511 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008512 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008513 {"job_start", 1, 2, f_job_start},
8514 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008515 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008516#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008517 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008518 {"js_decode", 1, 1, f_js_decode},
8519 {"js_encode", 1, 1, f_js_encode},
8520 {"json_decode", 1, 1, f_json_decode},
8521 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008522 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008524 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {"libcall", 3, 3, f_libcall},
8526 {"libcallnr", 3, 3, f_libcallnr},
8527 {"line", 1, 1, f_line},
8528 {"line2byte", 1, 1, f_line2byte},
8529 {"lispindent", 1, 1, f_lispindent},
8530 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008531#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008532 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008533 {"log10", 1, 1, f_log10},
8534#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008535#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008536 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008537#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008538 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008539 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008540 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008541 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008542 {"matchadd", 2, 5, f_matchadd},
8543 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008544 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008545 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008546 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008547 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008548 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008549 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008550 {"max", 1, 1, f_max},
8551 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008552#ifdef vim_mkdir
8553 {"mkdir", 1, 3, f_mkdir},
8554#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008555 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008556#ifdef FEAT_MZSCHEME
8557 {"mzeval", 1, 1, f_mzeval},
8558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008560 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008561 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008562 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008563#ifdef FEAT_PERL
8564 {"perleval", 1, 1, f_perleval},
8565#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008566#ifdef FEAT_FLOAT
8567 {"pow", 2, 2, f_pow},
8568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008570 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008571 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008572#ifdef FEAT_PYTHON3
8573 {"py3eval", 1, 1, f_py3eval},
8574#endif
8575#ifdef FEAT_PYTHON
8576 {"pyeval", 1, 1, f_pyeval},
8577#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008578 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008579 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008580 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008581#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008582 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008583#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008584 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {"remote_expr", 2, 3, f_remote_expr},
8586 {"remote_foreground", 1, 1, f_remote_foreground},
8587 {"remote_peek", 1, 2, f_remote_peek},
8588 {"remote_read", 1, 1, f_remote_read},
8589 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008590 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008592 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008594 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008595#ifdef FEAT_FLOAT
8596 {"round", 1, 1, f_round},
8597#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008598 {"screenattr", 2, 2, f_screenattr},
8599 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008600 {"screencol", 0, 0, f_screencol},
8601 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008602 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008603 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008604 {"searchpair", 3, 7, f_searchpair},
8605 {"searchpairpos", 3, 7, f_searchpairpos},
8606 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {"server2client", 2, 2, f_server2client},
8608 {"serverlist", 0, 0, f_serverlist},
8609 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008610 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008612 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008614 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008615 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008616 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008617 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008619 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008620 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008622#ifdef FEAT_CRYPT
8623 {"sha256", 1, 1, f_sha256},
8624#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008625 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008626 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008628#ifdef FEAT_FLOAT
8629 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008630 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008631#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008632 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008633 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008634 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008635 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008636 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008637#ifdef FEAT_FLOAT
8638 {"sqrt", 1, 1, f_sqrt},
8639 {"str2float", 1, 1, f_str2float},
8640#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008641 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008642 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008643 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008644 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645#ifdef HAVE_STRFTIME
8646 {"strftime", 1, 2, f_strftime},
8647#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008648 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 {"strlen", 1, 1, f_strlen},
8652 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008653 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008655 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008656 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {"substitute", 4, 4, f_substitute},
8658 {"synID", 3, 3, f_synID},
8659 {"synIDattr", 2, 3, f_synIDattr},
8660 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008661 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008662 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008663 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008664 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008665 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008666 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008667 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008668 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008669 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008670#ifdef FEAT_FLOAT
8671 {"tan", 1, 1, f_tan},
8672 {"tanh", 1, 1, f_tanh},
8673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008675 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008676#ifdef FEAT_TIMERS
8677 {"timer_start", 2, 3, f_timer_start},
8678 {"timer_stop", 1, 1, f_timer_stop},
8679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 {"tolower", 1, 1, f_tolower},
8681 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008682 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008683#ifdef FEAT_FLOAT
8684 {"trunc", 1, 1, f_trunc},
8685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008687 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008688 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008689 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008690 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 {"virtcol", 1, 1, f_virtcol},
8692 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008693 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008694 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008695 {"win_getid", 0, 2, f_win_getid},
8696 {"win_gotoid", 1, 1, f_win_gotoid},
8697 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8698 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 {"winbufnr", 1, 1, f_winbufnr},
8700 {"wincol", 0, 0, f_wincol},
8701 {"winheight", 1, 1, f_winheight},
8702 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008703 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008705 {"winrestview", 1, 1, f_winrestview},
8706 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008708 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008709 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008710 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711};
8712
8713#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8714
8715/*
8716 * Function given to ExpandGeneric() to obtain the list of internal
8717 * or user defined function names.
8718 */
8719 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008720get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 static int intidx = -1;
8723 char_u *name;
8724
8725 if (idx == 0)
8726 intidx = -1;
8727 if (intidx < 0)
8728 {
8729 name = get_user_func_name(xp, idx);
8730 if (name != NULL)
8731 return name;
8732 }
8733 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8734 {
8735 STRCPY(IObuff, functions[intidx].f_name);
8736 STRCAT(IObuff, "(");
8737 if (functions[intidx].f_max_argc == 0)
8738 STRCAT(IObuff, ")");
8739 return IObuff;
8740 }
8741
8742 return NULL;
8743}
8744
8745/*
8746 * Function given to ExpandGeneric() to obtain the list of internal or
8747 * user defined variable or function names.
8748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 static int intidx = -1;
8753 char_u *name;
8754
8755 if (idx == 0)
8756 intidx = -1;
8757 if (intidx < 0)
8758 {
8759 name = get_function_name(xp, idx);
8760 if (name != NULL)
8761 return name;
8762 }
8763 return get_user_var_name(xp, ++intidx);
8764}
8765
8766#endif /* FEAT_CMDL_COMPL */
8767
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008768#if defined(EBCDIC) || defined(PROTO)
8769/*
8770 * Compare struct fst by function name.
8771 */
8772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008773compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008774{
8775 struct fst *p1 = (struct fst *)s1;
8776 struct fst *p2 = (struct fst *)s2;
8777
8778 return STRCMP(p1->f_name, p2->f_name);
8779}
8780
8781/*
8782 * Sort the function table by function name.
8783 * The sorting of the table above is ASCII dependant.
8784 * On machines using EBCDIC we have to sort it.
8785 */
8786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008787sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008788{
8789 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8790
8791 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8792}
8793#endif
8794
8795
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796/*
8797 * Find internal function in table above.
8798 * Return index, or -1 if not found
8799 */
8800 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008801find_internal_func(
8802 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 int first = 0;
8805 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8806 int cmp;
8807 int x;
8808
8809 /*
8810 * Find the function name in the table. Binary search.
8811 */
8812 while (first <= last)
8813 {
8814 x = first + ((unsigned)(last - first) >> 1);
8815 cmp = STRCMP(name, functions[x].f_name);
8816 if (cmp < 0)
8817 last = x - 1;
8818 else if (cmp > 0)
8819 first = x + 1;
8820 else
8821 return x;
8822 }
8823 return -1;
8824}
8825
8826/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008827 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8828 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008829 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8830 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008831 */
8832 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008833deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008834{
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008836 int cc;
8837
Bram Moolenaar65639032016-03-16 21:40:30 +01008838 if (partialp != NULL)
8839 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008840
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008841 cc = name[*lenp];
8842 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008843 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008844 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008845 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008846 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008847 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008848 {
8849 *lenp = 0;
8850 return (char_u *)""; /* just in case */
8851 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008852 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008853 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008854 }
8855
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008856 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8857 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008858 partial_T *pt = v->di_tv.vval.v_partial;
8859
8860 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008861 {
8862 *lenp = 0;
8863 return (char_u *)""; /* just in case */
8864 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008865 if (partialp != NULL)
8866 *partialp = pt;
8867 *lenp = (int)STRLEN(pt->pt_name);
8868 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008869 }
8870
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 return name;
8872}
8873
8874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 * Allocate a variable for the result of a function.
8876 * Return OK or FAIL.
8877 */
8878 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008879get_func_tv(
8880 char_u *name, /* name of the function */
8881 int len, /* length of "name" */
8882 typval_T *rettv,
8883 char_u **arg, /* argument, pointing to the '(' */
8884 linenr_T firstline, /* first line of range */
8885 linenr_T lastline, /* last line of range */
8886 int *doesrange, /* return: function handled range */
8887 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008888 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008889 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 char_u *argp;
8892 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008893 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 int argcount = 0; /* number of arguments found */
8895
8896 /*
8897 * Get the arguments.
8898 */
8899 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008900 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8903 if (*argp == ')' || *argp == ',' || *argp == NUL)
8904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8906 {
8907 ret = FAIL;
8908 break;
8909 }
8910 ++argcount;
8911 if (*argp != ',')
8912 break;
8913 }
8914 if (*argp == ')')
8915 ++argp;
8916 else
8917 ret = FAIL;
8918
8919 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008920 {
8921 int i = 0;
8922
8923 if (get_vim_var_nr(VV_TESTING))
8924 {
8925 /* Prepare for calling garbagecollect_for_testing(), need to know
8926 * what variables are used on the call stack. */
8927 if (funcargs.ga_itemsize == 0)
8928 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
8929 for (i = 0; i < argcount; ++i)
8930 if (ga_grow(&funcargs, 1) == OK)
8931 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
8932 &argvars[i];
8933 }
8934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008935 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008936 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02008937
8938 funcargs.ga_len -= i;
8939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 {
8942 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008943 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008944 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008945 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947
8948 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008949 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
8951 *arg = skipwhite(argp);
8952 return ret;
8953}
8954
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008955#define ERROR_UNKNOWN 0
8956#define ERROR_TOOMANY 1
8957#define ERROR_TOOFEW 2
8958#define ERROR_SCRIPT 3
8959#define ERROR_DICT 4
8960#define ERROR_NONE 5
8961#define ERROR_OTHER 6
8962#define FLEN_FIXED 40
8963
8964/*
8965 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8966 * Change <SNR>123_name() to K_SNR 123_name().
8967 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8968 * (slow).
8969 */
8970 static char_u *
8971fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8972{
8973 int llen;
8974 char_u *fname;
8975 int i;
8976
8977 llen = eval_fname_script(name);
8978 if (llen > 0)
8979 {
8980 fname_buf[0] = K_SPECIAL;
8981 fname_buf[1] = KS_EXTRA;
8982 fname_buf[2] = (int)KE_SNR;
8983 i = 3;
8984 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8985 {
8986 if (current_SID <= 0)
8987 *error = ERROR_SCRIPT;
8988 else
8989 {
8990 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8991 i = (int)STRLEN(fname_buf);
8992 }
8993 }
8994 if (i + STRLEN(name + llen) < FLEN_FIXED)
8995 {
8996 STRCPY(fname_buf + i, name + llen);
8997 fname = fname_buf;
8998 }
8999 else
9000 {
9001 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9002 if (fname == NULL)
9003 *error = ERROR_OTHER;
9004 else
9005 {
9006 *tofree = fname;
9007 mch_memmove(fname, fname_buf, (size_t)i);
9008 STRCPY(fname + i, name + llen);
9009 }
9010 }
9011 }
9012 else
9013 fname = name;
9014 return fname;
9015}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016
9017/*
9018 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009019 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009020 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009023call_func(
9024 char_u *funcname, /* name of the function */
9025 int len, /* length of "name" */
9026 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009027 int argcount_in, /* number of "argvars" */
9028 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009029 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009030 linenr_T firstline, /* first line of range */
9031 linenr_T lastline, /* last line of range */
9032 int *doesrange, /* return: function handled range */
9033 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009034 partial_T *partial, /* optional, can be NULL */
9035 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036{
9037 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 int error = ERROR_NONE;
9039 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009042 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009044 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009045 int argcount = argcount_in;
9046 typval_T *argvars = argvars_in;
9047 dict_T *selfdict = selfdict_in;
9048 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9049 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009050
9051 /* Make a copy of the name, if it comes from a funcref variable it could
9052 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009053 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009054 if (name == NULL)
9055 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009057 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058
9059 *doesrange = FALSE;
9060
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009061 if (partial != NULL)
9062 {
9063 if (partial->pt_dict != NULL)
9064 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009065 /* When the function has a partial with a dict and there is a dict
9066 * argument, use the dict argument. That is backwards compatible.
9067 */
9068 if (selfdict_in == NULL)
9069 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009070 }
9071 if (error == ERROR_NONE && partial->pt_argc > 0)
9072 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009073 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9074 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9075 for (i = 0; i < argcount_in; ++i)
9076 argv[i + argv_clear] = argvars_in[i];
9077 argvars = argv;
9078 argcount = partial->pt_argc + argcount_in;
9079 }
9080 }
9081
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
9083 /* execute the function if no errors detected and executing */
9084 if (evaluate && error == ERROR_NONE)
9085 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009086 char_u *rfname = fname;
9087
9088 /* Ignore "g:" before a function name. */
9089 if (fname[0] == 'g' && fname[1] == ':')
9090 rfname = fname + 2;
9091
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009092 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9093 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 error = ERROR_UNKNOWN;
9095
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009096 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 {
9098 /*
9099 * User defined function.
9100 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009101 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009104 /* Trigger FuncUndefined event, may load the function. */
9105 if (fp == NULL
9106 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009107 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009108 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009110 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009111 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 }
9113#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009114 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009115 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009116 {
9117 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009118 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009119 }
9120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (fp != NULL)
9122 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009123 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009125 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009127 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009129 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009130 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 else
9132 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009133 int did_save_redo = FALSE;
9134
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 /*
9136 * Call the user function.
9137 * Save and restore search patterns, script variables and
9138 * redo buffer.
9139 */
9140 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009141#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009142 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009143#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009144 {
9145 saveRedobuff();
9146 did_save_redo = TRUE;
9147 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009148 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009149 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009150 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009151 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9152 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9153 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009154 /* Function was unreferenced while being used, free it
9155 * now. */
9156 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009157 if (did_save_redo)
9158 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 restore_search_patterns();
9160 error = ERROR_NONE;
9161 }
9162 }
9163 }
9164 else
9165 {
9166 /*
9167 * Find the function name in the table, call its implementation.
9168 */
9169 i = find_internal_func(fname);
9170 if (i >= 0)
9171 {
9172 if (argcount < functions[i].f_min_argc)
9173 error = ERROR_TOOFEW;
9174 else if (argcount > functions[i].f_max_argc)
9175 error = ERROR_TOOMANY;
9176 else
9177 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009178 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 error = ERROR_NONE;
9181 }
9182 }
9183 }
9184 /*
9185 * The function call (or "FuncUndefined" autocommand sequence) might
9186 * have been aborted by an error, an interrupt, or an explicitly thrown
9187 * exception that has not been caught so far. This situation can be
9188 * tested for by calling aborting(). For an error in an internal
9189 * function or for the "E132" error in call_user_func(), however, the
9190 * throw point at which the "force_abort" flag (temporarily reset by
9191 * emsg()) is normally updated has not been reached yet. We need to
9192 * update that flag first to make aborting() reliable.
9193 */
9194 update_force_abort();
9195 }
9196 if (error == ERROR_NONE)
9197 ret = OK;
9198
9199 /*
9200 * Report an error unless the argument evaluation or function call has been
9201 * cancelled due to an aborting error, an interrupt, or an exception.
9202 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009203 if (!aborting())
9204 {
9205 switch (error)
9206 {
9207 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009208 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009209 break;
9210 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009211 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009212 break;
9213 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009214 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009215 name);
9216 break;
9217 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009218 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009219 name);
9220 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009221 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009222 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009223 name);
9224 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009225 }
9226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009228 while (argv_clear > 0)
9229 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009230 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009231 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232
9233 return ret;
9234}
9235
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009236/*
9237 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009238 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009239 */
9240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009241emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009242{
9243 char_u *p;
9244
9245 if (*name == K_SPECIAL)
9246 p = concat_str((char_u *)"<SNR>", name + 3);
9247 else
9248 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009249 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009250 if (p != name)
9251 vim_free(p);
9252}
9253
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009254/*
9255 * Return TRUE for a non-zero Number and a non-empty String.
9256 */
9257 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009258non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009259{
9260 return ((argvars[0].v_type == VAR_NUMBER
9261 && argvars[0].vval.v_number != 0)
9262 || (argvars[0].v_type == VAR_STRING
9263 && argvars[0].vval.v_string != NULL
9264 && *argvars[0].vval.v_string != NUL));
9265}
9266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267/*********************************************
9268 * Implementation of the built-in functions
9269 */
9270
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009271#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009272static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009273
9274/*
9275 * Get the float value of "argvars[0]" into "f".
9276 * Returns FAIL when the argument is not a Number or Float.
9277 */
9278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009279get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009280{
9281 if (argvars[0].v_type == VAR_FLOAT)
9282 {
9283 *f = argvars[0].vval.v_float;
9284 return OK;
9285 }
9286 if (argvars[0].v_type == VAR_NUMBER)
9287 {
9288 *f = (float_T)argvars[0].vval.v_number;
9289 return OK;
9290 }
9291 EMSG(_("E808: Number or Float required"));
9292 return FAIL;
9293}
9294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009295/*
9296 * "abs(expr)" function
9297 */
9298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009299f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009300{
9301 if (argvars[0].v_type == VAR_FLOAT)
9302 {
9303 rettv->v_type = VAR_FLOAT;
9304 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9305 }
9306 else
9307 {
9308 varnumber_T n;
9309 int error = FALSE;
9310
9311 n = get_tv_number_chk(&argvars[0], &error);
9312 if (error)
9313 rettv->vval.v_number = -1;
9314 else if (n > 0)
9315 rettv->vval.v_number = n;
9316 else
9317 rettv->vval.v_number = -n;
9318 }
9319}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009320
9321/*
9322 * "acos()" function
9323 */
9324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009325f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009326{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009327 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009328
9329 rettv->v_type = VAR_FLOAT;
9330 if (get_float_arg(argvars, &f) == OK)
9331 rettv->vval.v_float = acos(f);
9332 else
9333 rettv->vval.v_float = 0.0;
9334}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009335#endif
9336
Bram Moolenaar071d4272004-06-13 20:20:40 +00009337/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339 */
9340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009341f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342{
Bram Moolenaar33570922005-01-25 22:26:29 +00009343 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009346 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009348 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009349 && !tv_check_lock(l->lv_lock,
9350 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009351 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009353 }
9354 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009355 EMSG(_(e_listreq));
9356}
9357
9358/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009359 * "alloc_fail(id, countdown, repeat)" function
9360 */
9361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009363{
9364 if (argvars[0].v_type != VAR_NUMBER
9365 || argvars[0].vval.v_number <= 0
9366 || argvars[1].v_type != VAR_NUMBER
9367 || argvars[1].vval.v_number < 0
9368 || argvars[2].v_type != VAR_NUMBER)
9369 EMSG(_(e_invarg));
9370 else
9371 {
9372 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009373 if (alloc_fail_id >= aid_last)
9374 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009375 alloc_fail_countdown = argvars[1].vval.v_number;
9376 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009377 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009378 }
9379}
9380
9381/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009382 * "and(expr, expr)" function
9383 */
9384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009385f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009386{
9387 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9388 & get_tv_number_chk(&argvars[1], NULL);
9389}
9390
9391/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009392 * "append(lnum, string/list)" function
9393 */
9394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009396{
9397 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009398 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009399 list_T *l = NULL;
9400 listitem_T *li = NULL;
9401 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009402 long added = 0;
9403
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009404 /* When coming here from Insert mode, sync undo, so that this can be
9405 * undone separately from what was previously inserted. */
9406 if (u_sync_once == 2)
9407 {
9408 u_sync_once = 1; /* notify that u_sync() was called */
9409 u_sync(TRUE);
9410 }
9411
Bram Moolenaar0d660222005-01-07 21:51:51 +00009412 lnum = get_tv_lnum(argvars);
9413 if (lnum >= 0
9414 && lnum <= curbuf->b_ml.ml_line_count
9415 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009416 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009417 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009418 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419 l = argvars[1].vval.v_list;
9420 if (l == NULL)
9421 return;
9422 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009423 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424 for (;;)
9425 {
9426 if (l == NULL)
9427 tv = &argvars[1]; /* append a string */
9428 else if (li == NULL)
9429 break; /* end of list */
9430 else
9431 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009432 line = get_tv_string_chk(tv);
9433 if (line == NULL) /* type error */
9434 {
9435 rettv->vval.v_number = 1; /* Failed */
9436 break;
9437 }
9438 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009439 ++added;
9440 if (l == NULL)
9441 break;
9442 li = li->li_next;
9443 }
9444
9445 appended_lines_mark(lnum, added);
9446 if (curwin->w_cursor.lnum > lnum)
9447 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009449 else
9450 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451}
9452
9453/*
9454 * "argc()" function
9455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009457f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009459 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460}
9461
9462/*
9463 * "argidx()" function
9464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009466f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009468 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469}
9470
9471/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009472 * "arglistid()" function
9473 */
9474 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009475f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009476{
9477 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009478
9479 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009480 wp = find_tabwin(&argvars[0], &argvars[1]);
9481 if (wp != NULL)
9482 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009483}
9484
9485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 * "argv(nr)" function
9487 */
9488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009489f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009490{
9491 int idx;
9492
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009493 if (argvars[0].v_type != VAR_UNKNOWN)
9494 {
9495 idx = get_tv_number_chk(&argvars[0], NULL);
9496 if (idx >= 0 && idx < ARGCOUNT)
9497 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9498 else
9499 rettv->vval.v_string = NULL;
9500 rettv->v_type = VAR_STRING;
9501 }
9502 else if (rettv_list_alloc(rettv) == OK)
9503 for (idx = 0; idx < ARGCOUNT; ++idx)
9504 list_append_string(rettv->vval.v_list,
9505 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506}
9507
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009508typedef enum
9509{
9510 ASSERT_EQUAL,
9511 ASSERT_NOTEQUAL,
9512 ASSERT_MATCH,
9513 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009514 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009515} assert_type_T;
9516
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009517static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009518static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, assert_type_T is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009519static void assert_error(garray_T *gap);
9520static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009521
9522/*
9523 * Prepare "gap" for an assert error and add the sourcing position.
9524 */
9525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009526prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009527{
9528 char buf[NUMBUFLEN];
9529
9530 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009531 if (sourcing_name != NULL)
9532 {
9533 ga_concat(gap, sourcing_name);
9534 if (sourcing_lnum > 0)
9535 ga_concat(gap, (char_u *)" ");
9536 }
9537 if (sourcing_lnum > 0)
9538 {
9539 sprintf(buf, "line %ld", (long)sourcing_lnum);
9540 ga_concat(gap, (char_u *)buf);
9541 }
9542 if (sourcing_name != NULL || sourcing_lnum > 0)
9543 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009544}
9545
9546/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009547 * Append "str" to "gap", escaping unprintable characters.
9548 * Changes NL to \n, CR to \r, etc.
9549 */
9550 static void
9551ga_concat_esc(garray_T *gap, char_u *str)
9552{
9553 char_u *p;
9554 char_u buf[NUMBUFLEN];
9555
Bram Moolenaarf1551962016-03-15 12:55:58 +01009556 if (str == NULL)
9557 {
9558 ga_concat(gap, (char_u *)"NULL");
9559 return;
9560 }
9561
Bram Moolenaar23689172016-02-15 22:37:37 +01009562 for (p = str; *p != NUL; ++p)
9563 switch (*p)
9564 {
9565 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9566 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9567 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9568 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9569 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9570 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9571 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9572 default:
9573 if (*p < ' ')
9574 {
9575 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9576 ga_concat(gap, buf);
9577 }
9578 else
9579 ga_append(gap, *p);
9580 break;
9581 }
9582}
9583
9584/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009585 * Fill "gap" with information about an assert error.
9586 */
9587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009588fill_assert_error(
9589 garray_T *gap,
9590 typval_T *opt_msg_tv,
9591 char_u *exp_str,
9592 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009593 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009594 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009595{
9596 char_u numbuf[NUMBUFLEN];
9597 char_u *tofree;
9598
9599 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9600 {
9601 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9602 vim_free(tofree);
9603 }
9604 else
9605 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009606 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009607 ga_concat(gap, (char_u *)"Pattern ");
9608 else
9609 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009610 if (exp_str == NULL)
9611 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009612 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009613 vim_free(tofree);
9614 }
9615 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009616 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009617 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009618 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009619 else if (atype == ASSERT_NOTMATCH)
9620 ga_concat(gap, (char_u *)" does match ");
9621 else if (atype == ASSERT_NOTEQUAL)
9622 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009623 else
9624 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009625 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009626 vim_free(tofree);
9627 }
9628}
Bram Moolenaar43345542015-11-29 17:35:35 +01009629
9630/*
9631 * Add an assert error to v:errors.
9632 */
9633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009635{
9636 struct vimvar *vp = &vimvars[VV_ERRORS];
9637
9638 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9639 /* Make sure v:errors is a list. */
9640 set_vim_var_list(VV_ERRORS, list_alloc());
9641 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9642}
9643
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009644 static void
9645assert_equal_common(typval_T *argvars, assert_type_T atype)
9646{
9647 garray_T ga;
9648
9649 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9650 != (atype == ASSERT_EQUAL))
9651 {
9652 prepare_assert_error(&ga);
9653 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9654 atype);
9655 assert_error(&ga);
9656 ga_clear(&ga);
9657 }
9658}
9659
Bram Moolenaar43345542015-11-29 17:35:35 +01009660/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009661 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009662 */
9663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009664f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009665{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009666 assert_equal_common(argvars, ASSERT_EQUAL);
9667}
Bram Moolenaar43345542015-11-29 17:35:35 +01009668
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009669/*
9670 * "assert_notequal(expected, actual[, msg])" function
9671 */
9672 static void
9673f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9674{
9675 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009676}
9677
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009678/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009679 * "assert_exception(string[, msg])" function
9680 */
9681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009682f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009683{
9684 garray_T ga;
9685 char *error;
9686
9687 error = (char *)get_tv_string_chk(&argvars[0]);
9688 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9689 {
9690 prepare_assert_error(&ga);
9691 ga_concat(&ga, (char_u *)"v:exception is not set");
9692 assert_error(&ga);
9693 ga_clear(&ga);
9694 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009695 else if (error != NULL
9696 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009697 {
9698 prepare_assert_error(&ga);
9699 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009700 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009701 assert_error(&ga);
9702 ga_clear(&ga);
9703 }
9704}
9705
9706/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009707 * "assert_fails(cmd [, error])" function
9708 */
9709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009710f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009711{
9712 char_u *cmd = get_tv_string_chk(&argvars[0]);
9713 garray_T ga;
9714
9715 called_emsg = FALSE;
9716 suppress_errthrow = TRUE;
9717 emsg_silent = TRUE;
9718 do_cmdline_cmd(cmd);
9719 if (!called_emsg)
9720 {
9721 prepare_assert_error(&ga);
9722 ga_concat(&ga, (char_u *)"command did not fail: ");
9723 ga_concat(&ga, cmd);
9724 assert_error(&ga);
9725 ga_clear(&ga);
9726 }
9727 else if (argvars[1].v_type != VAR_UNKNOWN)
9728 {
9729 char_u buf[NUMBUFLEN];
9730 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9731
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009732 if (error == NULL
9733 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009734 {
9735 prepare_assert_error(&ga);
9736 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009737 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009738 assert_error(&ga);
9739 ga_clear(&ga);
9740 }
9741 }
9742
9743 called_emsg = FALSE;
9744 suppress_errthrow = FALSE;
9745 emsg_silent = FALSE;
9746 emsg_on_display = FALSE;
9747 set_vim_var_string(VV_ERRMSG, NULL, 0);
9748}
9749
9750/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009751 * Common for assert_true() and assert_false().
9752 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009755{
9756 int error = FALSE;
9757 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009758
Bram Moolenaar37127922016-02-06 20:29:28 +01009759 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009760 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009761 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009762 if (argvars[0].v_type != VAR_NUMBER
9763 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9764 || error)
9765 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009766 prepare_assert_error(&ga);
9767 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009768 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009769 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009770 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009771 ga_clear(&ga);
9772 }
9773}
9774
9775/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009776 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009777 */
9778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009779f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009780{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009781 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009782}
9783
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009784 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009785assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009786{
9787 garray_T ga;
9788 char_u buf1[NUMBUFLEN];
9789 char_u buf2[NUMBUFLEN];
9790 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9791 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9792
Bram Moolenaar72188e92016-03-28 22:48:29 +02009793 if (pat == NULL || text == NULL)
9794 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009795 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009796 {
9797 prepare_assert_error(&ga);
9798 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009799 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009800 assert_error(&ga);
9801 ga_clear(&ga);
9802 }
9803}
9804
9805/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009806 * "assert_match(pattern, actual[, msg])" function
9807 */
9808 static void
9809f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9810{
9811 assert_match_common(argvars, ASSERT_MATCH);
9812}
9813
9814/*
9815 * "assert_notmatch(pattern, actual[, msg])" function
9816 */
9817 static void
9818f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9819{
9820 assert_match_common(argvars, ASSERT_NOTMATCH);
9821}
9822
9823/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009824 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009825 */
9826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009827f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009828{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009829 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009830}
9831
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009832#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009833/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009834 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009835 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009838{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009839 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009840
9841 rettv->v_type = VAR_FLOAT;
9842 if (get_float_arg(argvars, &f) == OK)
9843 rettv->vval.v_float = asin(f);
9844 else
9845 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009846}
9847
9848/*
9849 * "atan()" function
9850 */
9851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009853{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009854 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009855
9856 rettv->v_type = VAR_FLOAT;
9857 if (get_float_arg(argvars, &f) == OK)
9858 rettv->vval.v_float = atan(f);
9859 else
9860 rettv->vval.v_float = 0.0;
9861}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009862
9863/*
9864 * "atan2()" function
9865 */
9866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009867f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009868{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009869 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009870
9871 rettv->v_type = VAR_FLOAT;
9872 if (get_float_arg(argvars, &fx) == OK
9873 && get_float_arg(&argvars[1], &fy) == OK)
9874 rettv->vval.v_float = atan2(fx, fy);
9875 else
9876 rettv->vval.v_float = 0.0;
9877}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009878#endif
9879
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880/*
9881 * "browse(save, title, initdir, default)" function
9882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009884f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009885{
9886#ifdef FEAT_BROWSE
9887 int save;
9888 char_u *title;
9889 char_u *initdir;
9890 char_u *defname;
9891 char_u buf[NUMBUFLEN];
9892 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009893 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009895 save = get_tv_number_chk(&argvars[0], &error);
9896 title = get_tv_string_chk(&argvars[1]);
9897 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9898 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 if (error || title == NULL || initdir == NULL || defname == NULL)
9901 rettv->vval.v_string = NULL;
9902 else
9903 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009904 do_browse(save ? BROWSE_SAVE : 0,
9905 title, defname, NULL, initdir, NULL, curbuf);
9906#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009907 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009908#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009909 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009910}
9911
9912/*
9913 * "browsedir(title, initdir)" function
9914 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009916f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009917{
9918#ifdef FEAT_BROWSE
9919 char_u *title;
9920 char_u *initdir;
9921 char_u buf[NUMBUFLEN];
9922
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009923 title = get_tv_string_chk(&argvars[0]);
9924 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009925
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009926 if (title == NULL || initdir == NULL)
9927 rettv->vval.v_string = NULL;
9928 else
9929 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009930 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009932 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935}
9936
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009937static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009938
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939/*
9940 * Find a buffer by number or exact name.
9941 */
9942 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009943find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944{
9945 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009947 if (avar->v_type == VAR_NUMBER)
9948 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009949 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009951 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009952 if (buf == NULL)
9953 {
9954 /* No full path name match, try a match with a URL or a "nofile"
9955 * buffer, these don't use the full path. */
9956 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9957 if (buf->b_fname != NULL
9958 && (path_with_url(buf->b_fname)
9959#ifdef FEAT_QUICKFIX
9960 || bt_nofile(buf)
9961#endif
9962 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009963 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009964 break;
9965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009966 }
9967 return buf;
9968}
9969
9970/*
9971 * "bufexists(expr)" function
9972 */
9973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009974f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977}
9978
9979/*
9980 * "buflisted(expr)" function
9981 */
9982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009983f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984{
9985 buf_T *buf;
9986
9987 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009988 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009989}
9990
9991/*
9992 * "bufloaded(expr)" function
9993 */
9994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009995f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
9997 buf_T *buf;
9998
9999 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010000 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001}
10002
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010003 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010004buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 int save_magic;
10007 char_u *save_cpo;
10008 buf_T *buf;
10009
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10011 save_magic = p_magic;
10012 p_magic = TRUE;
10013 save_cpo = p_cpo;
10014 p_cpo = (char_u *)"";
10015
10016 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010017 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
10019 p_magic = save_magic;
10020 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010021 return buf;
10022}
10023
10024/*
10025 * Get buffer by number or pattern.
10026 */
10027 static buf_T *
10028get_buf_tv(typval_T *tv, int curtab_only)
10029{
10030 char_u *name = tv->vval.v_string;
10031 buf_T *buf;
10032
10033 if (tv->v_type == VAR_NUMBER)
10034 return buflist_findnr((int)tv->vval.v_number);
10035 if (tv->v_type != VAR_STRING)
10036 return NULL;
10037 if (name == NULL || *name == NUL)
10038 return curbuf;
10039 if (name[0] == '$' && name[1] == NUL)
10040 return lastbuf;
10041
10042 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043
10044 /* If not found, try expanding the name, like done for bufexists(). */
10045 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047
10048 return buf;
10049}
10050
10051/*
10052 * "bufname(expr)" function
10053 */
10054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010055f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056{
10057 buf_T *buf;
10058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010059 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010061 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010062 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010064 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010065 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010066 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 --emsg_off;
10068}
10069
10070/*
10071 * "bufnr(expr)" function
10072 */
10073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010074f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075{
10076 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010077 int error = FALSE;
10078 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010080 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010082 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010083 --emsg_off;
10084
10085 /* If the buffer isn't found and the second argument is not zero create a
10086 * new buffer. */
10087 if (buf == NULL
10088 && argvars[1].v_type != VAR_UNKNOWN
10089 && get_tv_number_chk(&argvars[1], &error) != 0
10090 && !error
10091 && (name = get_tv_string_chk(&argvars[0])) != NULL
10092 && !error)
10093 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10094
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010096 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010097 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010098 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099}
10100
10101/*
10102 * "bufwinnr(nr)" function
10103 */
10104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010105f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106{
10107#ifdef FEAT_WINDOWS
10108 win_T *wp;
10109 int winnr = 0;
10110#endif
10111 buf_T *buf;
10112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010113 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010115 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116#ifdef FEAT_WINDOWS
10117 for (wp = firstwin; wp; wp = wp->w_next)
10118 {
10119 ++winnr;
10120 if (wp->w_buffer == buf)
10121 break;
10122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010123 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010125 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126#endif
10127 --emsg_off;
10128}
10129
10130/*
10131 * "byte2line(byte)" function
10132 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010134f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#else
10139 long boff = 0;
10140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010141 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 (linenr_T)0, &boff);
10147#endif
10148}
10149
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010151byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010152{
10153#ifdef FEAT_MBYTE
10154 char_u *t;
10155#endif
10156 char_u *str;
10157 long idx;
10158
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010159 str = get_tv_string_chk(&argvars[0]);
10160 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010161 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010162 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010163 return;
10164
10165#ifdef FEAT_MBYTE
10166 t = str;
10167 for ( ; idx > 0; idx--)
10168 {
10169 if (*t == NUL) /* EOL reached */
10170 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010171 if (enc_utf8 && comp)
10172 t += utf_ptr2len(t);
10173 else
10174 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010176 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010177#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010178 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010180#endif
10181}
10182
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010183/*
10184 * "byteidx()" function
10185 */
10186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010187f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010188{
10189 byteidx(argvars, rettv, FALSE);
10190}
10191
10192/*
10193 * "byteidxcomp()" function
10194 */
10195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010196f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010197{
10198 byteidx(argvars, rettv, TRUE);
10199}
10200
Bram Moolenaardb913952012-06-29 12:54:53 +020010201 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010202func_call(
10203 char_u *name,
10204 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010205 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010206 dict_T *selfdict,
10207 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010208{
10209 listitem_T *item;
10210 typval_T argv[MAX_FUNC_ARGS + 1];
10211 int argc = 0;
10212 int dummy;
10213 int r = 0;
10214
10215 for (item = args->vval.v_list->lv_first; item != NULL;
10216 item = item->li_next)
10217 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010218 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010219 {
10220 EMSG(_("E699: Too many arguments"));
10221 break;
10222 }
10223 /* Make a copy of each argument. This is needed to be able to set
10224 * v_lock to VAR_FIXED in the copy without changing the original list.
10225 */
10226 copy_tv(&item->li_tv, &argv[argc++]);
10227 }
10228
10229 if (item == NULL)
10230 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10231 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010232 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010233
10234 /* Free the arguments. */
10235 while (argc > 0)
10236 clear_tv(&argv[--argc]);
10237
10238 return r;
10239}
10240
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010241/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010242 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010243 */
10244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010245f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010246{
10247 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010248 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010249 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010250
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010251 if (argvars[1].v_type != VAR_LIST)
10252 {
10253 EMSG(_(e_listreq));
10254 return;
10255 }
10256 if (argvars[1].vval.v_list == NULL)
10257 return;
10258
10259 if (argvars[0].v_type == VAR_FUNC)
10260 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010261 else if (argvars[0].v_type == VAR_PARTIAL)
10262 {
10263 partial = argvars[0].vval.v_partial;
10264 func = partial->pt_name;
10265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010266 else
10267 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010268 if (*func == NUL)
10269 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010270
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 if (argvars[2].v_type != VAR_UNKNOWN)
10272 {
10273 if (argvars[2].v_type != VAR_DICT)
10274 {
10275 EMSG(_(e_dictreq));
10276 return;
10277 }
10278 selfdict = argvars[2].vval.v_dict;
10279 }
10280
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010281 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010282}
10283
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010284#ifdef FEAT_FLOAT
10285/*
10286 * "ceil({float})" function
10287 */
10288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010289f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010290{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010291 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010292
10293 rettv->v_type = VAR_FLOAT;
10294 if (get_float_arg(argvars, &f) == OK)
10295 rettv->vval.v_float = ceil(f);
10296 else
10297 rettv->vval.v_float = 0.0;
10298}
10299#endif
10300
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010301#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010302/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010303 * "ch_close()" function
10304 */
10305 static void
10306f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10307{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010308 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010309
10310 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010311 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010312 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010313 channel_clear(channel);
10314 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010315}
10316
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010317/*
10318 * "ch_getbufnr()" function
10319 */
10320 static void
10321f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10322{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010323 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010324
10325 rettv->vval.v_number = -1;
10326 if (channel != NULL)
10327 {
10328 char_u *what = get_tv_string(&argvars[1]);
10329 int part;
10330
10331 if (STRCMP(what, "err") == 0)
10332 part = PART_ERR;
10333 else if (STRCMP(what, "out") == 0)
10334 part = PART_OUT;
10335 else if (STRCMP(what, "in") == 0)
10336 part = PART_IN;
10337 else
10338 part = PART_SOCK;
10339 if (channel->ch_part[part].ch_buffer != NULL)
10340 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10341 }
10342}
10343
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010344/*
10345 * "ch_getjob()" function
10346 */
10347 static void
10348f_ch_getjob(typval_T *argvars, typval_T *rettv)
10349{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010350 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010351
10352 if (channel != NULL)
10353 {
10354 rettv->v_type = VAR_JOB;
10355 rettv->vval.v_job = channel->ch_job;
10356 if (channel->ch_job != NULL)
10357 ++channel->ch_job->jv_refcount;
10358 }
10359}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010360
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010361/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010362 * "ch_info()" function
10363 */
10364 static void
10365f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10366{
10367 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10368
10369 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10370 channel_info(channel, rettv->vval.v_dict);
10371}
10372
10373/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010374 * "ch_log()" function
10375 */
10376 static void
10377f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10378{
10379 char_u *msg = get_tv_string(&argvars[0]);
10380 channel_T *channel = NULL;
10381
10382 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010383 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010384
10385 ch_log(channel, (char *)msg);
10386}
10387
10388/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010389 * "ch_logfile()" function
10390 */
10391 static void
10392f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10393{
10394 char_u *fname;
10395 char_u *opt = (char_u *)"";
10396 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010397
10398 fname = get_tv_string(&argvars[0]);
10399 if (argvars[1].v_type == VAR_STRING)
10400 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010401 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010402}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010403
10404/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010405 * "ch_open()" function
10406 */
10407 static void
10408f_ch_open(typval_T *argvars, typval_T *rettv)
10409{
Bram Moolenaar77073442016-02-13 23:23:53 +010010410 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010411 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010412}
10413
10414/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010415 * "ch_read()" function
10416 */
10417 static void
10418f_ch_read(typval_T *argvars, typval_T *rettv)
10419{
10420 common_channel_read(argvars, rettv, FALSE);
10421}
10422
10423/*
10424 * "ch_readraw()" function
10425 */
10426 static void
10427f_ch_readraw(typval_T *argvars, typval_T *rettv)
10428{
10429 common_channel_read(argvars, rettv, TRUE);
10430}
10431
10432/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010433 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010434 */
10435 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010436f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10437{
10438 ch_expr_common(argvars, rettv, TRUE);
10439}
10440
10441/*
10442 * "ch_sendexpr()" function
10443 */
10444 static void
10445f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10446{
10447 ch_expr_common(argvars, rettv, FALSE);
10448}
10449
10450/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010451 * "ch_evalraw()" function
10452 */
10453 static void
10454f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10455{
10456 ch_raw_common(argvars, rettv, TRUE);
10457}
10458
10459/*
10460 * "ch_sendraw()" function
10461 */
10462 static void
10463f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10464{
10465 ch_raw_common(argvars, rettv, FALSE);
10466}
10467
10468/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010469 * "ch_setoptions()" function
10470 */
10471 static void
10472f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10473{
10474 channel_T *channel;
10475 jobopt_T opt;
10476
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010477 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010478 if (channel == NULL)
10479 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010480 clear_job_options(&opt);
10481 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010482 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10483 channel_set_options(channel, &opt);
10484 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010485}
10486
10487/*
10488 * "ch_status()" function
10489 */
10490 static void
10491f_ch_status(typval_T *argvars, typval_T *rettv)
10492{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010493 channel_T *channel;
10494
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010495 /* return an empty string by default */
10496 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010497 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010498
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010499 channel = get_channel_arg(&argvars[0], FALSE);
10500 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010501}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010502#endif
10503
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010504/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010505 * "changenr()" function
10506 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010508f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010509{
10510 rettv->vval.v_number = curbuf->b_u_seq_cur;
10511}
10512
10513/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 * "char2nr(string)" function
10515 */
10516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010517f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518{
10519#ifdef FEAT_MBYTE
10520 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010521 {
10522 int utf8 = 0;
10523
10524 if (argvars[1].v_type != VAR_UNKNOWN)
10525 utf8 = get_tv_number_chk(&argvars[1], NULL);
10526
10527 if (utf8)
10528 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10529 else
10530 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 else
10533#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010534 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535}
10536
10537/*
10538 * "cindent(lnum)" function
10539 */
10540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010541f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542{
10543#ifdef FEAT_CINDENT
10544 pos_T pos;
10545 linenr_T lnum;
10546
10547 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10550 {
10551 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010552 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010553 curwin->w_cursor = pos;
10554 }
10555 else
10556#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010558}
10559
10560/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010561 * "clearmatches()" function
10562 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010564f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010565{
10566#ifdef FEAT_SEARCH_EXTRA
10567 clear_matches(curwin);
10568#endif
10569}
10570
10571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 * "col(string)" function
10573 */
10574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010575f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576{
10577 colnr_T col = 0;
10578 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010579 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010581 fp = var2fpos(&argvars[0], FALSE, &fnum);
10582 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010583 {
10584 if (fp->col == MAXCOL)
10585 {
10586 /* '> can be MAXCOL, get the length of the line then */
10587 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010588 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 else
10590 col = MAXCOL;
10591 }
10592 else
10593 {
10594 col = fp->col + 1;
10595#ifdef FEAT_VIRTUALEDIT
10596 /* col(".") when the cursor is on the NUL at the end of the line
10597 * because of "coladd" can be seen as an extra column. */
10598 if (virtual_active() && fp == &curwin->w_cursor)
10599 {
10600 char_u *p = ml_get_cursor();
10601
10602 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10603 curwin->w_virtcol - curwin->w_cursor.coladd))
10604 {
10605# ifdef FEAT_MBYTE
10606 int l;
10607
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010608 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609 col += l;
10610# else
10611 if (*p != NUL && p[1] == NUL)
10612 ++col;
10613# endif
10614 }
10615 }
10616#endif
10617 }
10618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620}
10621
Bram Moolenaar572cb562005-08-05 21:35:02 +000010622#if defined(FEAT_INS_EXPAND)
10623/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010624 * "complete()" function
10625 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010627f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010628{
10629 int startcol;
10630
10631 if ((State & INSERT) == 0)
10632 {
10633 EMSG(_("E785: complete() can only be used in Insert mode"));
10634 return;
10635 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010636
10637 /* Check for undo allowed here, because if something was already inserted
10638 * the line was already saved for undo and this check isn't done. */
10639 if (!undo_allowed())
10640 return;
10641
Bram Moolenaarade00832006-03-10 21:46:58 +000010642 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10643 {
10644 EMSG(_(e_invarg));
10645 return;
10646 }
10647
10648 startcol = get_tv_number_chk(&argvars[0], NULL);
10649 if (startcol <= 0)
10650 return;
10651
10652 set_completion(startcol - 1, argvars[1].vval.v_list);
10653}
10654
10655/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010656 * "complete_add()" function
10657 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010659f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010660{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010661 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010662}
10663
10664/*
10665 * "complete_check()" function
10666 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010668f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010669{
10670 int saved = RedrawingDisabled;
10671
10672 RedrawingDisabled = 0;
10673 ins_compl_check_keys(0);
10674 rettv->vval.v_number = compl_interrupted;
10675 RedrawingDisabled = saved;
10676}
10677#endif
10678
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679/*
10680 * "confirm(message, buttons[, default [, type]])" function
10681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010683f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684{
10685#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10686 char_u *message;
10687 char_u *buttons = NULL;
10688 char_u buf[NUMBUFLEN];
10689 char_u buf2[NUMBUFLEN];
10690 int def = 1;
10691 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 char_u *typestr;
10693 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010694
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010695 message = get_tv_string_chk(&argvars[0]);
10696 if (message == NULL)
10697 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010698 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010700 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10701 if (buttons == NULL)
10702 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010703 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010706 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010708 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10709 if (typestr == NULL)
10710 error = TRUE;
10711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010713 switch (TOUPPER_ASC(*typestr))
10714 {
10715 case 'E': type = VIM_ERROR; break;
10716 case 'Q': type = VIM_QUESTION; break;
10717 case 'I': type = VIM_INFO; break;
10718 case 'W': type = VIM_WARNING; break;
10719 case 'G': type = VIM_GENERIC; break;
10720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 }
10722 }
10723 }
10724 }
10725
10726 if (buttons == NULL || *buttons == NUL)
10727 buttons = (char_u *)_("&Ok");
10728
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010729 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010730 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010731 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732#endif
10733}
10734
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010735/*
10736 * "copy()" function
10737 */
10738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010739f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010740{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010741 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010742}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010744#ifdef FEAT_FLOAT
10745/*
10746 * "cos()" function
10747 */
10748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010749f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010750{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010751 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010752
10753 rettv->v_type = VAR_FLOAT;
10754 if (get_float_arg(argvars, &f) == OK)
10755 rettv->vval.v_float = cos(f);
10756 else
10757 rettv->vval.v_float = 0.0;
10758}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010759
10760/*
10761 * "cosh()" function
10762 */
10763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010764f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010765{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010766 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010767
10768 rettv->v_type = VAR_FLOAT;
10769 if (get_float_arg(argvars, &f) == OK)
10770 rettv->vval.v_float = cosh(f);
10771 else
10772 rettv->vval.v_float = 0.0;
10773}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010774#endif
10775
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010777 * "count()" function
10778 */
10779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010780f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010781{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010782 long n = 0;
10783 int ic = FALSE;
10784
Bram Moolenaare9a41262005-01-15 22:18:47 +000010785 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010786 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010787 listitem_T *li;
10788 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010789 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010790
Bram Moolenaare9a41262005-01-15 22:18:47 +000010791 if ((l = argvars[0].vval.v_list) != NULL)
10792 {
10793 li = l->lv_first;
10794 if (argvars[2].v_type != VAR_UNKNOWN)
10795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 int error = FALSE;
10797
10798 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010799 if (argvars[3].v_type != VAR_UNKNOWN)
10800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010801 idx = get_tv_number_chk(&argvars[3], &error);
10802 if (!error)
10803 {
10804 li = list_find(l, idx);
10805 if (li == NULL)
10806 EMSGN(_(e_listidx), idx);
10807 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010808 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010809 if (error)
10810 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010811 }
10812
10813 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010814 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010815 ++n;
10816 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010817 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010818 else if (argvars[0].v_type == VAR_DICT)
10819 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 int todo;
10821 dict_T *d;
10822 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010823
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010824 if ((d = argvars[0].vval.v_dict) != NULL)
10825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 int error = FALSE;
10827
Bram Moolenaare9a41262005-01-15 22:18:47 +000010828 if (argvars[2].v_type != VAR_UNKNOWN)
10829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010830 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010831 if (argvars[3].v_type != VAR_UNKNOWN)
10832 EMSG(_(e_invarg));
10833 }
10834
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010835 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010836 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010837 {
10838 if (!HASHITEM_EMPTY(hi))
10839 {
10840 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010841 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010842 ++n;
10843 }
10844 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 }
10846 }
10847 else
10848 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010849 rettv->vval.v_number = n;
10850}
10851
10852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10854 *
10855 * Checks the existence of a cscope connection.
10856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010858f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860#ifdef FEAT_CSCOPE
10861 int num = 0;
10862 char_u *dbpath = NULL;
10863 char_u *prepend = NULL;
10864 char_u buf[NUMBUFLEN];
10865
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010866 if (argvars[0].v_type != VAR_UNKNOWN
10867 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010869 num = (int)get_tv_number(&argvars[0]);
10870 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010871 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 }
10874
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876#endif
10877}
10878
10879/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010880 * "cursor(lnum, col)" function, or
10881 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010883 * Moves the cursor to the specified line and column.
10884 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010887f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888{
10889 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010890#ifdef FEAT_VIRTUALEDIT
10891 long coladd = 0;
10892#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010893 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010895 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010896 if (argvars[1].v_type == VAR_UNKNOWN)
10897 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010898 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010899 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010900
Bram Moolenaar493c1782014-05-28 14:34:46 +020010901 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010902 {
10903 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010904 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010905 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010906 line = pos.lnum;
10907 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010908#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010909 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010910#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010911 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010912 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010913 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010914 set_curswant = FALSE;
10915 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010916 }
10917 else
10918 {
10919 line = get_tv_lnum(argvars);
10920 col = get_tv_number_chk(&argvars[1], NULL);
10921#ifdef FEAT_VIRTUALEDIT
10922 if (argvars[2].v_type != VAR_UNKNOWN)
10923 coladd = get_tv_number_chk(&argvars[2], NULL);
10924#endif
10925 }
10926 if (line < 0 || col < 0
10927#ifdef FEAT_VIRTUALEDIT
10928 || coladd < 0
10929#endif
10930 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010931 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932 if (line > 0)
10933 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 if (col > 0)
10935 curwin->w_cursor.col = col - 1;
10936#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010937 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938#endif
10939
10940 /* Make sure the cursor is in a valid position. */
10941 check_cursor();
10942#ifdef FEAT_MBYTE
10943 /* Correct cursor for multi-byte character. */
10944 if (has_mbyte)
10945 mb_adjust_cursor();
10946#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010947
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010948 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010949 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950}
10951
10952/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010953 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 */
10955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010956f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010958 int noref = 0;
10959
10960 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010961 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010962 if (noref < 0 || noref > 1)
10963 EMSG(_(e_invarg));
10964 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010965 {
10966 current_copyID += COPYID_INC;
10967 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969}
10970
10971/*
10972 * "delete()" function
10973 */
10974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010975f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010977 char_u nbuf[NUMBUFLEN];
10978 char_u *name;
10979 char_u *flags;
10980
10981 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010983 return;
10984
10985 name = get_tv_string(&argvars[0]);
10986 if (name == NULL || *name == NUL)
10987 {
10988 EMSG(_(e_invarg));
10989 return;
10990 }
10991
10992 if (argvars[1].v_type != VAR_UNKNOWN)
10993 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010995 flags = (char_u *)"";
10996
10997 if (*flags == NUL)
10998 /* delete a file */
10999 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11000 else if (STRCMP(flags, "d") == 0)
11001 /* delete an empty directory */
11002 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11003 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011004 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011005 rettv->vval.v_number = delete_recursive(name);
11006 else
11007 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008}
11009
11010/*
11011 * "did_filetype()" function
11012 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011014f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015{
11016#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018#endif
11019}
11020
11021/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011022 * "diff_filler()" function
11023 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011025f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011026{
11027#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011028 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011029#endif
11030}
11031
11032/*
11033 * "diff_hlID()" function
11034 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011035 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011036f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011037{
11038#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011039 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011040 static linenr_T prev_lnum = 0;
11041 static int changedtick = 0;
11042 static int fnum = 0;
11043 static int change_start = 0;
11044 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011045 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011046 int filler_lines;
11047 int col;
11048
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011049 if (lnum < 0) /* ignore type error in {lnum} arg */
11050 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011051 if (lnum != prev_lnum
11052 || changedtick != curbuf->b_changedtick
11053 || fnum != curbuf->b_fnum)
11054 {
11055 /* New line, buffer, change: need to get the values. */
11056 filler_lines = diff_check(curwin, lnum);
11057 if (filler_lines < 0)
11058 {
11059 if (filler_lines == -1)
11060 {
11061 change_start = MAXCOL;
11062 change_end = -1;
11063 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11064 hlID = HLF_ADD; /* added line */
11065 else
11066 hlID = HLF_CHD; /* changed line */
11067 }
11068 else
11069 hlID = HLF_ADD; /* added line */
11070 }
11071 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011072 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011073 prev_lnum = lnum;
11074 changedtick = curbuf->b_changedtick;
11075 fnum = curbuf->b_fnum;
11076 }
11077
11078 if (hlID == HLF_CHD || hlID == HLF_TXD)
11079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011080 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011081 if (col >= change_start && col <= change_end)
11082 hlID = HLF_TXD; /* changed text */
11083 else
11084 hlID = HLF_CHD; /* changed line */
11085 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011086 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011087#endif
11088}
11089
11090/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011091 * "disable_char_avail_for_testing({expr})" function
11092 */
11093 static void
11094f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11095{
11096 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11097}
11098
11099/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011100 * "empty({expr})" function
11101 */
11102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011103f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011104{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011105 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011106
11107 switch (argvars[0].v_type)
11108 {
11109 case VAR_STRING:
11110 case VAR_FUNC:
11111 n = argvars[0].vval.v_string == NULL
11112 || *argvars[0].vval.v_string == NUL;
11113 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011114 case VAR_PARTIAL:
11115 n = FALSE;
11116 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011117 case VAR_NUMBER:
11118 n = argvars[0].vval.v_number == 0;
11119 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011120 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011121#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011122 n = argvars[0].vval.v_float == 0.0;
11123 break;
11124#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011125 case VAR_LIST:
11126 n = argvars[0].vval.v_list == NULL
11127 || argvars[0].vval.v_list->lv_first == NULL;
11128 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 case VAR_DICT:
11130 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011131 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011132 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011133 case VAR_SPECIAL:
11134 n = argvars[0].vval.v_number != VVAL_TRUE;
11135 break;
11136
Bram Moolenaar835dc632016-02-07 14:27:38 +010011137 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011138#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011139 n = argvars[0].vval.v_job == NULL
11140 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11141 break;
11142#endif
11143 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011144#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011145 n = argvars[0].vval.v_channel == NULL
11146 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011147 break;
11148#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011149 case VAR_UNKNOWN:
11150 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11151 n = TRUE;
11152 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011153 }
11154
11155 rettv->vval.v_number = n;
11156}
11157
11158/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159 * "escape({string}, {chars})" function
11160 */
11161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011162f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011163{
11164 char_u buf[NUMBUFLEN];
11165
Bram Moolenaar758711c2005-02-02 23:11:38 +000011166 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11167 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011168 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169}
11170
11171/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011172 * "eval()" function
11173 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011175f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011176{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011177 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011179 s = get_tv_string_chk(&argvars[0]);
11180 if (s != NULL)
11181 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011182
Bram Moolenaar615b9972015-01-14 17:15:05 +010011183 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011184 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11185 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011186 if (p != NULL && !aborting())
11187 EMSG2(_(e_invexpr2), p);
11188 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011189 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011190 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011191 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011192 else if (*s != NUL)
11193 EMSG(_(e_trailing));
11194}
11195
11196/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 * "eventhandler()" function
11198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011200f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203}
11204
11205/*
11206 * "executable()" function
11207 */
11208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011209f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011210{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011211 char_u *name = get_tv_string(&argvars[0]);
11212
11213 /* Check in $PATH and also check directly if there is a directory name. */
11214 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11215 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011216}
11217
11218/*
11219 * "exepath()" function
11220 */
11221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011222f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011223{
11224 char_u *p = NULL;
11225
Bram Moolenaarb5971142015-03-21 17:32:19 +010011226 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011227 rettv->v_type = VAR_STRING;
11228 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229}
11230
11231/*
11232 * "exists()" function
11233 */
11234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011235f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236{
11237 char_u *p;
11238 char_u *name;
11239 int n = FALSE;
11240 int len = 0;
11241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 if (*p == '$') /* environment variable */
11244 {
11245 /* first try "normal" environment variables (fast) */
11246 if (mch_getenv(p + 1) != NULL)
11247 n = TRUE;
11248 else
11249 {
11250 /* try expanding things like $VIM and ${HOME} */
11251 p = expand_env_save(p);
11252 if (p != NULL && *p != '$')
11253 n = TRUE;
11254 vim_free(p);
11255 }
11256 }
11257 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011259 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011260 if (*skipwhite(p) != NUL)
11261 n = FALSE; /* trailing garbage */
11262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011263 else if (*p == '*') /* internal or user defined function */
11264 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011265 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 }
11267 else if (*p == ':')
11268 {
11269 n = cmd_exists(p + 1);
11270 }
11271 else if (*p == '#')
11272 {
11273#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011274 if (p[1] == '#')
11275 n = autocmd_supported(p + 2);
11276 else
11277 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011278#endif
11279 }
11280 else /* internal variable */
11281 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011282 char_u *tofree;
11283 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011285 /* get_name_len() takes care of expanding curly braces */
11286 name = p;
11287 len = get_name_len(&p, &tofree, TRUE, FALSE);
11288 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011290 if (tofree != NULL)
11291 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011292 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011293 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011294 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011295 /* handle d.key, l[idx], f(expr) */
11296 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11297 if (n)
11298 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299 }
11300 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011301 if (*p != NUL)
11302 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011304 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011305 }
11306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011307 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308}
11309
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011310#ifdef FEAT_FLOAT
11311/*
11312 * "exp()" function
11313 */
11314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011315f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011316{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011317 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011318
11319 rettv->v_type = VAR_FLOAT;
11320 if (get_float_arg(argvars, &f) == OK)
11321 rettv->vval.v_float = exp(f);
11322 else
11323 rettv->vval.v_float = 0.0;
11324}
11325#endif
11326
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327/*
11328 * "expand()" function
11329 */
11330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011331f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332{
11333 char_u *s;
11334 int len;
11335 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011336 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011338 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011339 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011342 if (argvars[1].v_type != VAR_UNKNOWN
11343 && argvars[2].v_type != VAR_UNKNOWN
11344 && get_tv_number_chk(&argvars[2], &error)
11345 && !error)
11346 {
11347 rettv->v_type = VAR_LIST;
11348 rettv->vval.v_list = NULL;
11349 }
11350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011351 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 if (*s == '%' || *s == '#' || *s == '<')
11353 {
11354 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011355 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011357 if (rettv->v_type == VAR_LIST)
11358 {
11359 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11360 list_append_string(rettv->vval.v_list, result, -1);
11361 else
11362 vim_free(result);
11363 }
11364 else
11365 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366 }
11367 else
11368 {
11369 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011370 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011371 if (argvars[1].v_type != VAR_UNKNOWN
11372 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011373 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011374 if (!error)
11375 {
11376 ExpandInit(&xpc);
11377 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011378 if (p_wic)
11379 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011380 if (rettv->v_type == VAR_STRING)
11381 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11382 options, WILD_ALL);
11383 else if (rettv_list_alloc(rettv) != FAIL)
11384 {
11385 int i;
11386
11387 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11388 for (i = 0; i < xpc.xp_numfiles; i++)
11389 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11390 ExpandCleanup(&xpc);
11391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011392 }
11393 else
11394 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 }
11396}
11397
11398/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011399 * Go over all entries in "d2" and add them to "d1".
11400 * When "action" is "error" then a duplicate key is an error.
11401 * When "action" is "force" then a duplicate key is overwritten.
11402 * Otherwise duplicate keys are ignored ("action" is "keep").
11403 */
11404 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011405dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011406{
11407 dictitem_T *di1;
11408 hashitem_T *hi2;
11409 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011410 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011411
11412 todo = (int)d2->dv_hashtab.ht_used;
11413 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11414 {
11415 if (!HASHITEM_EMPTY(hi2))
11416 {
11417 --todo;
11418 di1 = dict_find(d1, hi2->hi_key, -1);
11419 if (d1->dv_scope != 0)
11420 {
11421 /* Disallow replacing a builtin function in l: and g:.
11422 * Check the key to be valid when adding to any
11423 * scope. */
11424 if (d1->dv_scope == VAR_DEF_SCOPE
11425 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11426 && var_check_func_name(hi2->hi_key,
11427 di1 == NULL))
11428 break;
11429 if (!valid_varname(hi2->hi_key))
11430 break;
11431 }
11432 if (di1 == NULL)
11433 {
11434 di1 = dictitem_copy(HI2DI(hi2));
11435 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11436 dictitem_free(di1);
11437 }
11438 else if (*action == 'e')
11439 {
11440 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11441 break;
11442 }
11443 else if (*action == 'f' && HI2DI(hi2) != di1)
11444 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011445 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11446 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011447 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011448 clear_tv(&di1->di_tv);
11449 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11450 }
11451 }
11452 }
11453}
11454
11455/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011456 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011457 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011458 */
11459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011460f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011461{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011462 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011463
Bram Moolenaare9a41262005-01-15 22:18:47 +000011464 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011465 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011466 list_T *l1, *l2;
11467 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011468 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011469 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011470
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471 l1 = argvars[0].vval.v_list;
11472 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011473 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011474 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 {
11476 if (argvars[2].v_type != VAR_UNKNOWN)
11477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 before = get_tv_number_chk(&argvars[2], &error);
11479 if (error)
11480 return; /* type error; errmsg already given */
11481
Bram Moolenaar758711c2005-02-02 23:11:38 +000011482 if (before == l1->lv_len)
11483 item = NULL;
11484 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011485 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011486 item = list_find(l1, before);
11487 if (item == NULL)
11488 {
11489 EMSGN(_(e_listidx), before);
11490 return;
11491 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011492 }
11493 }
11494 else
11495 item = NULL;
11496 list_extend(l1, l2, item);
11497
Bram Moolenaare9a41262005-01-15 22:18:47 +000011498 copy_tv(&argvars[0], rettv);
11499 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011500 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11502 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011503 dict_T *d1, *d2;
11504 char_u *action;
11505 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011506
11507 d1 = argvars[0].vval.v_dict;
11508 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011509 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011510 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011511 {
11512 /* Check the third argument. */
11513 if (argvars[2].v_type != VAR_UNKNOWN)
11514 {
11515 static char *(av[]) = {"keep", "force", "error"};
11516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 action = get_tv_string_chk(&argvars[2]);
11518 if (action == NULL)
11519 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 for (i = 0; i < 3; ++i)
11521 if (STRCMP(action, av[i]) == 0)
11522 break;
11523 if (i == 3)
11524 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011525 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011526 return;
11527 }
11528 }
11529 else
11530 action = (char_u *)"force";
11531
Bram Moolenaara9922d62013-05-30 13:01:18 +020011532 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011533
Bram Moolenaare9a41262005-01-15 22:18:47 +000011534 copy_tv(&argvars[0], rettv);
11535 }
11536 }
11537 else
11538 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011539}
11540
11541/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011542 * "feedkeys()" function
11543 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011545f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011546{
11547 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011548 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011549 char_u *keys, *flags;
11550 char_u nbuf[NUMBUFLEN];
11551 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011552 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011553 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011554 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011555
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011556 /* This is not allowed in the sandbox. If the commands would still be
11557 * executed in the sandbox it would be OK, but it probably happens later,
11558 * when "sandbox" is no longer set. */
11559 if (check_secure())
11560 return;
11561
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011562 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011563
11564 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011565 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011566 flags = get_tv_string_buf(&argvars[1], nbuf);
11567 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011568 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011569 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011570 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011571 case 'n': remap = FALSE; break;
11572 case 'm': remap = TRUE; break;
11573 case 't': typed = TRUE; break;
11574 case 'i': insert = TRUE; break;
11575 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011576 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011577 }
11578 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011579 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011580
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011581 if (*keys != NUL || execute)
11582 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011583 /* Need to escape K_SPECIAL and CSI before putting the string in the
11584 * typeahead buffer. */
11585 keys_esc = vim_strsave_escape_csi(keys);
11586 if (keys_esc != NULL)
11587 {
11588 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011589 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011590 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011591 if (vgetc_busy)
11592 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011593 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011594 {
11595 int save_msg_scroll = msg_scroll;
11596
11597 /* Avoid a 1 second delay when the keys start Insert mode. */
11598 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011599
Bram Moolenaar245c4102016-04-20 17:37:41 +020011600 if (!dangerous)
11601 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011602 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011603 if (!dangerous)
11604 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011605 msg_scroll |= save_msg_scroll;
11606 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011607 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011608 }
11609}
11610
11611/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011612 * "filereadable()" function
11613 */
11614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011615f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011617 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011618 char_u *p;
11619 int n;
11620
Bram Moolenaarc236c162008-07-13 17:41:49 +000011621#ifndef O_NONBLOCK
11622# define O_NONBLOCK 0
11623#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011625 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11626 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 {
11628 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011629 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011630 }
11631 else
11632 n = FALSE;
11633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011634 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011635}
11636
11637/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011638 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011639 * rights to write into.
11640 */
11641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011642f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011644 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011645}
11646
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011647 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011648findfilendir(
11649 typval_T *argvars UNUSED,
11650 typval_T *rettv,
11651 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011652{
11653#ifdef FEAT_SEARCHPATH
11654 char_u *fname;
11655 char_u *fresult = NULL;
11656 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11657 char_u *p;
11658 char_u pathbuf[NUMBUFLEN];
11659 int count = 1;
11660 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011661 int error = FALSE;
11662#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011663
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011664 rettv->vval.v_string = NULL;
11665 rettv->v_type = VAR_STRING;
11666
11667#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011669
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011670 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011671 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11673 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011674 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011675 else
11676 {
11677 if (*p != NUL)
11678 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011680 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011681 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011683 }
11684
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011685 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11686 error = TRUE;
11687
11688 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 do
11691 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011692 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011693 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011694 fresult = find_file_in_path_option(first ? fname : NULL,
11695 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011696 0, first, path,
11697 find_what,
11698 curbuf->b_ffname,
11699 find_what == FINDFILE_DIR
11700 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011701 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011702
11703 if (fresult != NULL && rettv->v_type == VAR_LIST)
11704 list_append_string(rettv->vval.v_list, fresult, -1);
11705
11706 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011707 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011708
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011709 if (rettv->v_type == VAR_STRING)
11710 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011711#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011712}
11713
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011714static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11715static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011716
11717/*
11718 * Implementation of map() and filter().
11719 */
11720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011721filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011722{
11723 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011724 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011725 listitem_T *li, *nli;
11726 list_T *l = NULL;
11727 dictitem_T *di;
11728 hashtab_T *ht;
11729 hashitem_T *hi;
11730 dict_T *d = NULL;
11731 typval_T save_val;
11732 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011733 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011734 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011735 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011736 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011737 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011738 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011739 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011740
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011742 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011743 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011744 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011745 return;
11746 }
11747 else if (argvars[0].v_type == VAR_DICT)
11748 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011749 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011750 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011751 return;
11752 }
11753 else
11754 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011755 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011756 return;
11757 }
11758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759 expr = get_tv_string_buf_chk(&argvars[1], buf);
11760 /* On type errors, the preceding call has already displayed an error
11761 * message. Avoid a misleading error message for an empty string that
11762 * was not passed as argument. */
11763 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 prepare_vimvar(VV_VAL, &save_val);
11766 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011767
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011768 /* We reset "did_emsg" to be able to detect whether an error
11769 * occurred during evaluation of the expression. */
11770 save_did_emsg = did_emsg;
11771 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011772
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011773 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011774 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011775 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 vimvars[VV_KEY].vv_type = VAR_STRING;
11777
11778 ht = &d->dv_hashtab;
11779 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011780 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011781 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011783 if (!HASHITEM_EMPTY(hi))
11784 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011785 int r;
11786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 --todo;
11788 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011789 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011790 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11791 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011792 break;
11793 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011794 r = filter_map_one(&di->di_tv, expr, map, &rem);
11795 clear_tv(&vimvars[VV_KEY].vv_tv);
11796 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 break;
11798 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011799 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011800 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11801 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011802 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011803 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011804 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 }
11806 }
11807 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 }
11809 else
11810 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011811 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011813 for (li = l->lv_first; li != NULL; li = nli)
11814 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011815 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011816 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011817 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011818 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011819 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011820 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011821 break;
11822 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011823 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011824 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011825 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011826 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011827
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011828 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011829 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011830
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011831 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011832 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011833
11834 copy_tv(&argvars[0], rettv);
11835}
11836
11837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011838filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011839{
Bram Moolenaar33570922005-01-25 22:26:29 +000011840 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011841 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011842 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843
Bram Moolenaar33570922005-01-25 22:26:29 +000011844 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011845 s = expr;
11846 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011847 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 if (*s != NUL) /* check for trailing chars after expr */
11849 {
11850 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011851 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011852 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011853 }
11854 if (map)
11855 {
11856 /* map(): replace the list item value */
11857 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011858 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011859 *tv = rettv;
11860 }
11861 else
11862 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011863 int error = FALSE;
11864
Bram Moolenaare9a41262005-01-15 22:18:47 +000011865 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011868 /* On type error, nothing has been removed; return FAIL to stop the
11869 * loop. The error message was given by get_tv_number_chk(). */
11870 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011871 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011873 retval = OK;
11874theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011876 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011877}
11878
11879/*
11880 * "filter()" function
11881 */
11882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011883f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011884{
11885 filter_map(argvars, rettv, FALSE);
11886}
11887
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011888/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011889 * "finddir({fname}[, {path}[, {count}]])" function
11890 */
11891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011892f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011893{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011894 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011895}
11896
11897/*
11898 * "findfile({fname}[, {path}[, {count}]])" function
11899 */
11900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011901f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011902{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011903 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904}
11905
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011906#ifdef FEAT_FLOAT
11907/*
11908 * "float2nr({float})" function
11909 */
11910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011911f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011912{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011913 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011914
11915 if (get_float_arg(argvars, &f) == OK)
11916 {
11917 if (f < -0x7fffffff)
11918 rettv->vval.v_number = -0x7fffffff;
11919 else if (f > 0x7fffffff)
11920 rettv->vval.v_number = 0x7fffffff;
11921 else
11922 rettv->vval.v_number = (varnumber_T)f;
11923 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011924}
11925
11926/*
11927 * "floor({float})" function
11928 */
11929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011930f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011931{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011932 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011933
11934 rettv->v_type = VAR_FLOAT;
11935 if (get_float_arg(argvars, &f) == OK)
11936 rettv->vval.v_float = floor(f);
11937 else
11938 rettv->vval.v_float = 0.0;
11939}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011940
11941/*
11942 * "fmod()" function
11943 */
11944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011945f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011946{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011947 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011948
11949 rettv->v_type = VAR_FLOAT;
11950 if (get_float_arg(argvars, &fx) == OK
11951 && get_float_arg(&argvars[1], &fy) == OK)
11952 rettv->vval.v_float = fmod(fx, fy);
11953 else
11954 rettv->vval.v_float = 0.0;
11955}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011956#endif
11957
Bram Moolenaar0d660222005-01-07 21:51:51 +000011958/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011959 * "fnameescape({string})" function
11960 */
11961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011962f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011963{
11964 rettv->vval.v_string = vim_strsave_fnameescape(
11965 get_tv_string(&argvars[0]), FALSE);
11966 rettv->v_type = VAR_STRING;
11967}
11968
11969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 * "fnamemodify({fname}, {mods})" function
11971 */
11972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011973f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974{
11975 char_u *fname;
11976 char_u *mods;
11977 int usedlen = 0;
11978 int len;
11979 char_u *fbuf = NULL;
11980 char_u buf[NUMBUFLEN];
11981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011982 fname = get_tv_string_chk(&argvars[0]);
11983 mods = get_tv_string_buf_chk(&argvars[1], buf);
11984 if (fname == NULL || mods == NULL)
11985 fname = NULL;
11986 else
11987 {
11988 len = (int)STRLEN(fname);
11989 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011996 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 vim_free(fbuf);
11998}
11999
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012000static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012001
12002/*
12003 * "foldclosed()" function
12004 */
12005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012006foldclosed_both(
12007 typval_T *argvars UNUSED,
12008 typval_T *rettv,
12009 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010{
12011#ifdef FEAT_FOLDING
12012 linenr_T lnum;
12013 linenr_T first, last;
12014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012015 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12017 {
12018 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12019 {
12020 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012021 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012023 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 return;
12025 }
12026 }
12027#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012029}
12030
12031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012032 * "foldclosed()" function
12033 */
12034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012035f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012036{
12037 foldclosed_both(argvars, rettv, FALSE);
12038}
12039
12040/*
12041 * "foldclosedend()" function
12042 */
12043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012044f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012045{
12046 foldclosed_both(argvars, rettv, TRUE);
12047}
12048
12049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012050 * "foldlevel()" function
12051 */
12052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012053f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
12055#ifdef FEAT_FOLDING
12056 linenr_T lnum;
12057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012058 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012060 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062}
12063
12064/*
12065 * "foldtext()" function
12066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012068f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012069{
12070#ifdef FEAT_FOLDING
12071 linenr_T lnum;
12072 char_u *s;
12073 char_u *r;
12074 int len;
12075 char *txt;
12076#endif
12077
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078 rettv->v_type = VAR_STRING;
12079 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012081 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12082 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12083 <= curbuf->b_ml.ml_line_count
12084 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 {
12086 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012087 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12088 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089 {
12090 if (!linewhite(lnum))
12091 break;
12092 ++lnum;
12093 }
12094
12095 /* Find interesting text in this line. */
12096 s = skipwhite(ml_get(lnum));
12097 /* skip C comment-start */
12098 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012101 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012102 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012103 {
12104 s = skipwhite(ml_get(lnum + 1));
12105 if (*s == '*')
12106 s = skipwhite(s + 1);
12107 }
12108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012109 txt = _("+-%s%3ld lines: ");
12110 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012111 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 + 20 /* for %3ld */
12113 + STRLEN(s))); /* concatenated */
12114 if (r != NULL)
12115 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012116 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12117 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12118 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012119 len = (int)STRLEN(r);
12120 STRCAT(r, s);
12121 /* remove 'foldmarker' and 'commentstring' */
12122 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012123 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012124 }
12125 }
12126#endif
12127}
12128
12129/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012130 * "foldtextresult(lnum)" function
12131 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012133f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012134{
12135#ifdef FEAT_FOLDING
12136 linenr_T lnum;
12137 char_u *text;
12138 char_u buf[51];
12139 foldinfo_T foldinfo;
12140 int fold_count;
12141#endif
12142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->v_type = VAR_STRING;
12144 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012145#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012147 /* treat illegal types and illegal string values for {lnum} the same */
12148 if (lnum < 0)
12149 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012150 fold_count = foldedCount(curwin, lnum, &foldinfo);
12151 if (fold_count > 0)
12152 {
12153 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12154 &foldinfo, buf);
12155 if (text == buf)
12156 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012157 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012158 }
12159#endif
12160}
12161
12162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012163 * "foreground()" function
12164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012166f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168#ifdef FEAT_GUI
12169 if (gui.in_use)
12170 gui_mch_set_foreground();
12171#else
12172# ifdef WIN32
12173 win32_set_foreground();
12174# endif
12175#endif
12176}
12177
12178/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012179 * "function()" function
12180 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012182f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012183{
12184 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012185 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012186 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012187 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012188
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012189 if (argvars[0].v_type == VAR_FUNC)
12190 {
12191 /* function(MyFunc, [arg], dict) */
12192 s = argvars[0].vval.v_string;
12193 }
12194 else if (argvars[0].v_type == VAR_PARTIAL
12195 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012196 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012197 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012198 arg_pt = argvars[0].vval.v_partial;
12199 s = arg_pt->pt_name;
12200 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012201 else
12202 {
12203 /* function('MyFunc', [arg], dict) */
12204 s = get_tv_string(&argvars[0]);
12205 use_string = TRUE;
12206 }
12207
12208 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012209 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012210 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012211 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12212 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012213 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012214 else
12215 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012216 int dict_idx = 0;
12217 int arg_idx = 0;
12218 list_T *list = NULL;
12219
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012220 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012221 {
12222 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012223 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012224
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012225 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12226 * also be called from another script. Using trans_function_name()
12227 * would also work, but some plugins depend on the name being
12228 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012229 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012230 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12231 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012232 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012233 STRCPY(name, sid_buf);
12234 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012235 }
12236 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012237 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012238 name = vim_strsave(s);
12239
12240 if (argvars[1].v_type != VAR_UNKNOWN)
12241 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012242 if (argvars[2].v_type != VAR_UNKNOWN)
12243 {
12244 /* function(name, [args], dict) */
12245 arg_idx = 1;
12246 dict_idx = 2;
12247 }
12248 else if (argvars[1].v_type == VAR_DICT)
12249 /* function(name, dict) */
12250 dict_idx = 1;
12251 else
12252 /* function(name, [args]) */
12253 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012254 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012255 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012256 if (argvars[dict_idx].v_type != VAR_DICT)
12257 {
12258 EMSG(_("E922: expected a dict"));
12259 vim_free(name);
12260 return;
12261 }
12262 if (argvars[dict_idx].vval.v_dict == NULL)
12263 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012264 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012265 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012266 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012267 if (argvars[arg_idx].v_type != VAR_LIST)
12268 {
12269 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12270 vim_free(name);
12271 return;
12272 }
12273 list = argvars[arg_idx].vval.v_list;
12274 if (list == NULL || list->lv_len == 0)
12275 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012276 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012277 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012278 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012279 {
12280 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012281
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012282 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012283 if (pt == NULL)
12284 vim_free(name);
12285 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012286 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012287 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012288 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012289 listitem_T *li;
12290 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012291 int arg_len = 0;
12292 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012293
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012294 if (arg_pt != NULL)
12295 arg_len = arg_pt->pt_argc;
12296 if (list != NULL)
12297 lv_len = list->lv_len;
12298 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012299 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012300 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012301 if (pt->pt_argv == NULL)
12302 {
12303 vim_free(pt);
12304 vim_free(name);
12305 return;
12306 }
12307 else
12308 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012309 for (i = 0; i < arg_len; i++)
12310 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12311 if (lv_len > 0)
12312 for (li = list->lv_first; li != NULL;
12313 li = li->li_next)
12314 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012315 }
12316 }
12317
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012318 /* For "function(dict.func, [], dict)" and "func" is a partial
12319 * use "dict". That is backwards compatible. */
12320 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012321 {
12322 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12323 ++pt->pt_dict->dv_refcount;
12324 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012325 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012326 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012327 pt->pt_dict = arg_pt->pt_dict;
12328 if (pt->pt_dict != NULL)
12329 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012330 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012331
12332 pt->pt_refcount = 1;
12333 pt->pt_name = name;
12334 func_ref(pt->pt_name);
12335 }
12336 rettv->v_type = VAR_PARTIAL;
12337 rettv->vval.v_partial = pt;
12338 }
12339 else
12340 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012341 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012342 rettv->v_type = VAR_FUNC;
12343 rettv->vval.v_string = name;
12344 func_ref(name);
12345 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012346 }
12347}
12348
12349/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012350 * "garbagecollect()" function
12351 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012354{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012355 /* This is postponed until we are back at the toplevel, because we may be
12356 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12357 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012358
12359 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12360 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012361}
12362
12363/*
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +020012364 * "garbagecollect_for_testing()" function
12365 */
12366 static void
12367f_garbagecollect_for_testing(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
12368{
12369 /* This is dangerous, any Lists and Dicts used internally may be freed
12370 * while still in use. */
12371 garbage_collect(TRUE);
12372}
12373
12374/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375 * "get()" function
12376 */
12377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012378f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012379{
Bram Moolenaar33570922005-01-25 22:26:29 +000012380 listitem_T *li;
12381 list_T *l;
12382 dictitem_T *di;
12383 dict_T *d;
12384 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012385
Bram Moolenaare9a41262005-01-15 22:18:47 +000012386 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012387 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012388 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012390 int error = FALSE;
12391
12392 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12393 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012394 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012395 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012396 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012397 else if (argvars[0].v_type == VAR_DICT)
12398 {
12399 if ((d = argvars[0].vval.v_dict) != NULL)
12400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012401 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012402 if (di != NULL)
12403 tv = &di->di_tv;
12404 }
12405 }
12406 else
12407 EMSG2(_(e_listdictarg), "get()");
12408
12409 if (tv == NULL)
12410 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012411 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012412 copy_tv(&argvars[2], rettv);
12413 }
12414 else
12415 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416}
12417
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012418static 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 +000012419
12420/*
12421 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012422 * Return a range (from start to end) of lines in rettv from the specified
12423 * buffer.
12424 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012425 */
12426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012427get_buffer_lines(
12428 buf_T *buf,
12429 linenr_T start,
12430 linenr_T end,
12431 int retlist,
12432 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012433{
12434 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012435
Bram Moolenaar959a1432013-12-14 12:17:38 +010012436 rettv->v_type = VAR_STRING;
12437 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012438 if (retlist && rettv_list_alloc(rettv) == FAIL)
12439 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012440
12441 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12442 return;
12443
12444 if (!retlist)
12445 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012446 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12447 p = ml_get_buf(buf, start, FALSE);
12448 else
12449 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012450 rettv->vval.v_string = vim_strsave(p);
12451 }
12452 else
12453 {
12454 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012455 return;
12456
12457 if (start < 1)
12458 start = 1;
12459 if (end > buf->b_ml.ml_line_count)
12460 end = buf->b_ml.ml_line_count;
12461 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012462 if (list_append_string(rettv->vval.v_list,
12463 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012464 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012465 }
12466}
12467
12468/*
12469 * "getbufline()" function
12470 */
12471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012472f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012473{
12474 linenr_T lnum;
12475 linenr_T end;
12476 buf_T *buf;
12477
12478 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12479 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012480 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012481 --emsg_off;
12482
Bram Moolenaar661b1822005-07-28 22:36:45 +000012483 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012484 if (argvars[2].v_type == VAR_UNKNOWN)
12485 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012486 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012487 end = get_tv_lnum_buf(&argvars[2], buf);
12488
Bram Moolenaar342337a2005-07-21 21:11:17 +000012489 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012490}
12491
Bram Moolenaar0d660222005-01-07 21:51:51 +000012492/*
12493 * "getbufvar()" function
12494 */
12495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012496f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012497{
12498 buf_T *buf;
12499 buf_T *save_curbuf;
12500 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012501 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012502 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012503
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012504 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12505 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012506 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012507 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012509 rettv->v_type = VAR_STRING;
12510 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012511
12512 if (buf != NULL && varname != NULL)
12513 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012514 /* set curbuf to be our buf, temporarily */
12515 save_curbuf = curbuf;
12516 curbuf = buf;
12517
Bram Moolenaar0d660222005-01-07 21:51:51 +000012518 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012519 {
12520 if (get_option_tv(&varname, rettv, TRUE) == OK)
12521 done = TRUE;
12522 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012523 else if (STRCMP(varname, "changedtick") == 0)
12524 {
12525 rettv->v_type = VAR_NUMBER;
12526 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012527 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012528 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012529 else
12530 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012531 /* Look up the variable. */
12532 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12533 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12534 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012535 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012536 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012538 done = TRUE;
12539 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012541
12542 /* restore previous notion of curbuf */
12543 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012544 }
12545
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012546 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12547 /* use the default value */
12548 copy_tv(&argvars[2], rettv);
12549
Bram Moolenaar0d660222005-01-07 21:51:51 +000012550 --emsg_off;
12551}
12552
12553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 * "getchar()" function
12555 */
12556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012557f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012558{
12559 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012560 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012561
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012562 /* Position the cursor. Needed after a message that ends in a space. */
12563 windgoto(msg_row, msg_col);
12564
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 ++no_mapping;
12566 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012567 for (;;)
12568 {
12569 if (argvars[0].v_type == VAR_UNKNOWN)
12570 /* getchar(): blocking wait. */
12571 n = safe_vgetc();
12572 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12573 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012574 n = vpeekc_any();
12575 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012576 /* illegal argument or getchar(0) and no char avail: return zero */
12577 n = 0;
12578 else
12579 /* getchar(0) and char avail: return char */
12580 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012581
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012582 if (n == K_IGNORE)
12583 continue;
12584 break;
12585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 --no_mapping;
12587 --allow_keys;
12588
Bram Moolenaar219b8702006-11-01 14:32:36 +000012589 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12590 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12591 vimvars[VV_MOUSE_COL].vv_nr = 0;
12592
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012593 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 if (IS_SPECIAL(n) || mod_mask != 0)
12595 {
12596 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12597 int i = 0;
12598
12599 /* Turn a special key into three bytes, plus modifier. */
12600 if (mod_mask != 0)
12601 {
12602 temp[i++] = K_SPECIAL;
12603 temp[i++] = KS_MODIFIER;
12604 temp[i++] = mod_mask;
12605 }
12606 if (IS_SPECIAL(n))
12607 {
12608 temp[i++] = K_SPECIAL;
12609 temp[i++] = K_SECOND(n);
12610 temp[i++] = K_THIRD(n);
12611 }
12612#ifdef FEAT_MBYTE
12613 else if (has_mbyte)
12614 i += (*mb_char2bytes)(n, temp + i);
12615#endif
12616 else
12617 temp[i++] = n;
12618 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 rettv->v_type = VAR_STRING;
12620 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012621
12622#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012623 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012624 {
12625 int row = mouse_row;
12626 int col = mouse_col;
12627 win_T *win;
12628 linenr_T lnum;
12629# ifdef FEAT_WINDOWS
12630 win_T *wp;
12631# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012632 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012633
12634 if (row >= 0 && col >= 0)
12635 {
12636 /* Find the window at the mouse coordinates and compute the
12637 * text position. */
12638 win = mouse_find_win(&row, &col);
12639 (void)mouse_comp_pos(win, &row, &col, &lnum);
12640# ifdef FEAT_WINDOWS
12641 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012642 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012643# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012644 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012645 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12646 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12647 }
12648 }
12649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650 }
12651}
12652
12653/*
12654 * "getcharmod()" function
12655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012657f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012659 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012660}
12661
12662/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012663 * "getcharsearch()" function
12664 */
12665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012666f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012667{
12668 if (rettv_dict_alloc(rettv) != FAIL)
12669 {
12670 dict_T *dict = rettv->vval.v_dict;
12671
12672 dict_add_nr_str(dict, "char", 0L, last_csearch());
12673 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12674 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12675 }
12676}
12677
12678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 * "getcmdline()" function
12680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012682f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 rettv->v_type = VAR_STRING;
12685 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686}
12687
12688/*
12689 * "getcmdpos()" function
12690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012692f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695}
12696
12697/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012698 * "getcmdtype()" function
12699 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012701f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012702{
12703 rettv->v_type = VAR_STRING;
12704 rettv->vval.v_string = alloc(2);
12705 if (rettv->vval.v_string != NULL)
12706 {
12707 rettv->vval.v_string[0] = get_cmdline_type();
12708 rettv->vval.v_string[1] = NUL;
12709 }
12710}
12711
12712/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012713 * "getcmdwintype()" function
12714 */
12715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012716f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012717{
12718 rettv->v_type = VAR_STRING;
12719 rettv->vval.v_string = NULL;
12720#ifdef FEAT_CMDWIN
12721 rettv->vval.v_string = alloc(2);
12722 if (rettv->vval.v_string != NULL)
12723 {
12724 rettv->vval.v_string[0] = cmdwin_type;
12725 rettv->vval.v_string[1] = NUL;
12726 }
12727#endif
12728}
12729
12730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 * "getcwd()" function
12732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012734f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012736 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012737 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012740 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012741
12742 wp = find_tabwin(&argvars[0], &argvars[1]);
12743 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012745 if (wp->w_localdir != NULL)
12746 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12747 else if(globaldir != NULL)
12748 rettv->vval.v_string = vim_strsave(globaldir);
12749 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012750 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012751 cwd = alloc(MAXPATHL);
12752 if (cwd != NULL)
12753 {
12754 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12755 rettv->vval.v_string = vim_strsave(cwd);
12756 vim_free(cwd);
12757 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012758 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012759#ifdef BACKSLASH_IN_FILENAME
12760 if (rettv->vval.v_string != NULL)
12761 slash_adjust(rettv->vval.v_string);
12762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 }
12764}
12765
12766/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012767 * "getfontname()" function
12768 */
12769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012770f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 rettv->v_type = VAR_STRING;
12773 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012774#ifdef FEAT_GUI
12775 if (gui.in_use)
12776 {
12777 GuiFont font;
12778 char_u *name = NULL;
12779
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012780 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012781 {
12782 /* Get the "Normal" font. Either the name saved by
12783 * hl_set_font_name() or from the font ID. */
12784 font = gui.norm_font;
12785 name = hl_get_font_name();
12786 }
12787 else
12788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012789 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012790 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12791 return;
12792 font = gui_mch_get_font(name, FALSE);
12793 if (font == NOFONT)
12794 return; /* Invalid font name, return empty string. */
12795 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012796 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012797 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012798 gui_mch_free_font(font);
12799 }
12800#endif
12801}
12802
12803/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012804 * "getfperm({fname})" function
12805 */
12806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012807f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012808{
12809 char_u *fname;
12810 struct stat st;
12811 char_u *perm = NULL;
12812 char_u flags[] = "rwx";
12813 int i;
12814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012818 if (mch_stat((char *)fname, &st) >= 0)
12819 {
12820 perm = vim_strsave((char_u *)"---------");
12821 if (perm != NULL)
12822 {
12823 for (i = 0; i < 9; i++)
12824 {
12825 if (st.st_mode & (1 << (8 - i)))
12826 perm[i] = flags[i % 3];
12827 }
12828 }
12829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012830 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012831}
12832
12833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 * "getfsize({fname})" function
12835 */
12836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012837f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838{
12839 char_u *fname;
12840 struct stat st;
12841
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845
12846 if (mch_stat((char *)fname, &st) >= 0)
12847 {
12848 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012849 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012853
12854 /* non-perfect check for overflow */
12855 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12856 rettv->vval.v_number = -2;
12857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 }
12859 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861}
12862
12863/*
12864 * "getftime({fname})" function
12865 */
12866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012867f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868{
12869 char_u *fname;
12870 struct stat st;
12871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873
12874 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
12880/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012881 * "getftype({fname})" function
12882 */
12883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012884f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012885{
12886 char_u *fname;
12887 struct stat st;
12888 char_u *type = NULL;
12889 char *t;
12890
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012892
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012894 if (mch_lstat((char *)fname, &st) >= 0)
12895 {
12896#ifdef S_ISREG
12897 if (S_ISREG(st.st_mode))
12898 t = "file";
12899 else if (S_ISDIR(st.st_mode))
12900 t = "dir";
12901# ifdef S_ISLNK
12902 else if (S_ISLNK(st.st_mode))
12903 t = "link";
12904# endif
12905# ifdef S_ISBLK
12906 else if (S_ISBLK(st.st_mode))
12907 t = "bdev";
12908# endif
12909# ifdef S_ISCHR
12910 else if (S_ISCHR(st.st_mode))
12911 t = "cdev";
12912# endif
12913# ifdef S_ISFIFO
12914 else if (S_ISFIFO(st.st_mode))
12915 t = "fifo";
12916# endif
12917# ifdef S_ISSOCK
12918 else if (S_ISSOCK(st.st_mode))
12919 t = "fifo";
12920# endif
12921 else
12922 t = "other";
12923#else
12924# ifdef S_IFMT
12925 switch (st.st_mode & S_IFMT)
12926 {
12927 case S_IFREG: t = "file"; break;
12928 case S_IFDIR: t = "dir"; break;
12929# ifdef S_IFLNK
12930 case S_IFLNK: t = "link"; break;
12931# endif
12932# ifdef S_IFBLK
12933 case S_IFBLK: t = "bdev"; break;
12934# endif
12935# ifdef S_IFCHR
12936 case S_IFCHR: t = "cdev"; break;
12937# endif
12938# ifdef S_IFIFO
12939 case S_IFIFO: t = "fifo"; break;
12940# endif
12941# ifdef S_IFSOCK
12942 case S_IFSOCK: t = "socket"; break;
12943# endif
12944 default: t = "other";
12945 }
12946# else
12947 if (mch_isdir(fname))
12948 t = "dir";
12949 else
12950 t = "file";
12951# endif
12952#endif
12953 type = vim_strsave((char_u *)t);
12954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012955 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012956}
12957
12958/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012959 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960 */
12961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012962f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012963{
12964 linenr_T lnum;
12965 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012966 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012967
12968 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012969 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012970 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012971 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012972 retlist = FALSE;
12973 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012974 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012975 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012976 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012977 retlist = TRUE;
12978 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012979
Bram Moolenaar342337a2005-07-21 21:11:17 +000012980 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012981}
12982
12983/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012984 * "getmatches()" function
12985 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012987f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012988{
12989#ifdef FEAT_SEARCH_EXTRA
12990 dict_T *dict;
12991 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012992 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012993
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012994 if (rettv_list_alloc(rettv) == OK)
12995 {
12996 while (cur != NULL)
12997 {
12998 dict = dict_alloc();
12999 if (dict == NULL)
13000 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013001 if (cur->match.regprog == NULL)
13002 {
13003 /* match added with matchaddpos() */
13004 for (i = 0; i < MAXPOSMATCH; ++i)
13005 {
13006 llpos_T *llpos;
13007 char buf[6];
13008 list_T *l;
13009
13010 llpos = &cur->pos.pos[i];
13011 if (llpos->lnum == 0)
13012 break;
13013 l = list_alloc();
13014 if (l == NULL)
13015 break;
13016 list_append_number(l, (varnumber_T)llpos->lnum);
13017 if (llpos->col > 0)
13018 {
13019 list_append_number(l, (varnumber_T)llpos->col);
13020 list_append_number(l, (varnumber_T)llpos->len);
13021 }
13022 sprintf(buf, "pos%d", i + 1);
13023 dict_add_list(dict, buf, l);
13024 }
13025 }
13026 else
13027 {
13028 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13029 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013030 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013031 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13032 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013033# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013034 if (cur->conceal_char)
13035 {
13036 char_u buf[MB_MAXBYTES + 1];
13037
13038 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13039 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13040 }
13041# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013042 list_append_dict(rettv->vval.v_list, dict);
13043 cur = cur->next;
13044 }
13045 }
13046#endif
13047}
13048
13049/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013050 * "getpid()" function
13051 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013053f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013054{
13055 rettv->vval.v_number = mch_get_pid();
13056}
13057
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013059getpos_both(
13060 typval_T *argvars,
13061 typval_T *rettv,
13062 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013063{
Bram Moolenaara5525202006-03-02 22:52:09 +000013064 pos_T *fp;
13065 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013066 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013067
13068 if (rettv_list_alloc(rettv) == OK)
13069 {
13070 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013071 if (getcurpos)
13072 fp = &curwin->w_cursor;
13073 else
13074 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013075 if (fnum != -1)
13076 list_append_number(l, (varnumber_T)fnum);
13077 else
13078 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013079 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13080 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013081 list_append_number(l, (fp != NULL)
13082 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013083 : (varnumber_T)0);
13084 list_append_number(l,
13085#ifdef FEAT_VIRTUALEDIT
13086 (fp != NULL) ? (varnumber_T)fp->coladd :
13087#endif
13088 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013089 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013090 {
13091 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013092 list_append_number(l, curwin->w_curswant == MAXCOL ?
13093 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013094 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013095 }
13096 else
13097 rettv->vval.v_number = FALSE;
13098}
13099
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013100
13101/*
13102 * "getcurpos()" function
13103 */
13104 static void
13105f_getcurpos(typval_T *argvars, typval_T *rettv)
13106{
13107 getpos_both(argvars, rettv, TRUE);
13108}
13109
13110/*
13111 * "getpos(string)" function
13112 */
13113 static void
13114f_getpos(typval_T *argvars, typval_T *rettv)
13115{
13116 getpos_both(argvars, rettv, FALSE);
13117}
13118
Bram Moolenaara5525202006-03-02 22:52:09 +000013119/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013120 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013121 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013123f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013124{
13125#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013126 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013127#endif
13128
Bram Moolenaar2641f772005-03-25 21:58:17 +000013129#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013130 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013131 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013132 wp = NULL;
13133 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13134 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013135 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013136 if (wp == NULL)
13137 return;
13138 }
13139
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013140 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013141 }
13142#endif
13143}
13144
13145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013146 * "getreg()" function
13147 */
13148 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013149f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150{
13151 char_u *strregname;
13152 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013153 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013154 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013155 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013157 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013158 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013159 strregname = get_tv_string_chk(&argvars[0]);
13160 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013161 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013162 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013163 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013164 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13165 return_list = get_tv_number_chk(&argvars[2], &error);
13166 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013169 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013170
13171 if (error)
13172 return;
13173
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174 regname = (strregname == NULL ? '"' : *strregname);
13175 if (regname == 0)
13176 regname = '"';
13177
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013178 if (return_list)
13179 {
13180 rettv->v_type = VAR_LIST;
13181 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13182 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013183 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013184 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013185 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013186 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013187 }
13188 else
13189 {
13190 rettv->v_type = VAR_STRING;
13191 rettv->vval.v_string = get_reg_contents(regname,
13192 arg2 ? GREG_EXPR_SRC : 0);
13193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194}
13195
13196/*
13197 * "getregtype()" function
13198 */
13199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013200f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201{
13202 char_u *strregname;
13203 int regname;
13204 char_u buf[NUMBUFLEN + 2];
13205 long reglen = 0;
13206
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013207 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013208 {
13209 strregname = get_tv_string_chk(&argvars[0]);
13210 if (strregname == NULL) /* type error; errmsg already given */
13211 {
13212 rettv->v_type = VAR_STRING;
13213 rettv->vval.v_string = NULL;
13214 return;
13215 }
13216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013217 else
13218 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013219 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220
13221 regname = (strregname == NULL ? '"' : *strregname);
13222 if (regname == 0)
13223 regname = '"';
13224
13225 buf[0] = NUL;
13226 buf[1] = NUL;
13227 switch (get_reg_type(regname, &reglen))
13228 {
13229 case MLINE: buf[0] = 'V'; break;
13230 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013231 case MBLOCK:
13232 buf[0] = Ctrl_V;
13233 sprintf((char *)buf + 1, "%ld", reglen + 1);
13234 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013235 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236 rettv->v_type = VAR_STRING;
13237 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013238}
13239
13240/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013241 * "gettabvar()" function
13242 */
13243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013244f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013245{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013246 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013247 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013248 dictitem_T *v;
13249 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013250 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013251
13252 rettv->v_type = VAR_STRING;
13253 rettv->vval.v_string = NULL;
13254
13255 varname = get_tv_string_chk(&argvars[1]);
13256 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13257 if (tp != NULL && varname != NULL)
13258 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013259 /* Set tp to be our tabpage, temporarily. Also set the window to the
13260 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013261 if (switch_win(&oldcurwin, &oldtabpage,
13262 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013263 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013264 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013265 /* look up the variable */
13266 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13267 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13268 if (v != NULL)
13269 {
13270 copy_tv(&v->di_tv, rettv);
13271 done = TRUE;
13272 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013273 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013274
13275 /* restore previous notion of curwin */
13276 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013277 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013278
13279 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013280 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013281}
13282
13283/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013284 * "gettabwinvar()" function
13285 */
13286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013287f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013288{
13289 getwinvar(argvars, rettv, 1);
13290}
13291
13292/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293 * "getwinposx()" function
13294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013296f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013297{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013298 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299#ifdef FEAT_GUI
13300 if (gui.in_use)
13301 {
13302 int x, y;
13303
13304 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013305 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 }
13307#endif
13308}
13309
13310/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013311 * "win_findbuf()" function
13312 */
13313 static void
13314f_win_findbuf(typval_T *argvars, typval_T *rettv)
13315{
13316 if (rettv_list_alloc(rettv) != FAIL)
13317 win_findbuf(argvars, rettv->vval.v_list);
13318}
13319
13320/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013321 * "win_getid()" function
13322 */
13323 static void
13324f_win_getid(typval_T *argvars, typval_T *rettv)
13325{
13326 rettv->vval.v_number = win_getid(argvars);
13327}
13328
13329/*
13330 * "win_gotoid()" function
13331 */
13332 static void
13333f_win_gotoid(typval_T *argvars, typval_T *rettv)
13334{
13335 rettv->vval.v_number = win_gotoid(argvars);
13336}
13337
13338/*
13339 * "win_id2tabwin()" function
13340 */
13341 static void
13342f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13343{
13344 if (rettv_list_alloc(rettv) != FAIL)
13345 win_id2tabwin(argvars, rettv->vval.v_list);
13346}
13347
13348/*
13349 * "win_id2win()" function
13350 */
13351 static void
13352f_win_id2win(typval_T *argvars, typval_T *rettv)
13353{
13354 rettv->vval.v_number = win_id2win(argvars);
13355}
13356
13357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 * "getwinposy()" function
13359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013361f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013364#ifdef FEAT_GUI
13365 if (gui.in_use)
13366 {
13367 int x, y;
13368
13369 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371 }
13372#endif
13373}
13374
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013375/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013376 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013377 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013378 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013379find_win_by_nr(
13380 typval_T *vp,
13381 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013382{
13383#ifdef FEAT_WINDOWS
13384 win_T *wp;
13385#endif
13386 int nr;
13387
13388 nr = get_tv_number_chk(vp, NULL);
13389
13390#ifdef FEAT_WINDOWS
13391 if (nr < 0)
13392 return NULL;
13393 if (nr == 0)
13394 return curwin;
13395
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013396 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13397 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013398 if (--nr <= 0)
13399 break;
13400 return wp;
13401#else
13402 if (nr == 0 || nr == 1)
13403 return curwin;
13404 return NULL;
13405#endif
13406}
13407
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013409 * Find window specified by "wvp" in tabpage "tvp".
13410 */
13411 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013412find_tabwin(
13413 typval_T *wvp, /* VAR_UNKNOWN for current window */
13414 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013415{
13416 win_T *wp = NULL;
13417 tabpage_T *tp = NULL;
13418 long n;
13419
13420 if (wvp->v_type != VAR_UNKNOWN)
13421 {
13422 if (tvp->v_type != VAR_UNKNOWN)
13423 {
13424 n = get_tv_number(tvp);
13425 if (n >= 0)
13426 tp = find_tabpage(n);
13427 }
13428 else
13429 tp = curtab;
13430
13431 if (tp != NULL)
13432 wp = find_win_by_nr(wvp, tp);
13433 }
13434 else
13435 wp = curwin;
13436
13437 return wp;
13438}
13439
13440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 * "getwinvar()" function
13442 */
13443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013444f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013446 getwinvar(argvars, rettv, 0);
13447}
13448
13449/*
13450 * getwinvar() and gettabwinvar()
13451 */
13452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013453getwinvar(
13454 typval_T *argvars,
13455 typval_T *rettv,
13456 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013457{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013458 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013460 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013461 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013462 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013463#ifdef FEAT_WINDOWS
13464 win_T *oldcurwin;
13465 tabpage_T *oldtabpage;
13466 int need_switch_win;
13467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013468
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013469#ifdef FEAT_WINDOWS
13470 if (off == 1)
13471 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13472 else
13473 tp = curtab;
13474#endif
13475 win = find_win_by_nr(&argvars[off], tp);
13476 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013479 rettv->v_type = VAR_STRING;
13480 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481
13482 if (win != NULL && varname != NULL)
13483 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013484#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013485 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013486 * otherwise the window is not valid. Only do this when needed,
13487 * autocommands get blocked. */
13488 need_switch_win = !(tp == curtab && win == curwin);
13489 if (!need_switch_win
13490 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13491#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013492 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013493 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013494 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013495 if (get_option_tv(&varname, rettv, 1) == OK)
13496 done = TRUE;
13497 }
13498 else
13499 {
13500 /* Look up the variable. */
13501 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13502 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13503 varname, FALSE);
13504 if (v != NULL)
13505 {
13506 copy_tv(&v->di_tv, rettv);
13507 done = TRUE;
13508 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013511
Bram Moolenaarba117c22015-09-29 16:53:22 +020013512#ifdef FEAT_WINDOWS
13513 if (need_switch_win)
13514 /* restore previous notion of curwin */
13515 restore_win(oldcurwin, oldtabpage, TRUE);
13516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 }
13518
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013519 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13520 /* use the default return value */
13521 copy_tv(&argvars[off + 2], rettv);
13522
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523 --emsg_off;
13524}
13525
13526/*
13527 * "glob()" function
13528 */
13529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013530f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013532 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013534 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013536 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013537 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013538 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013539 if (argvars[1].v_type != VAR_UNKNOWN)
13540 {
13541 if (get_tv_number_chk(&argvars[1], &error))
13542 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013544 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013545 if (get_tv_number_chk(&argvars[2], &error))
13546 {
13547 rettv->v_type = VAR_LIST;
13548 rettv->vval.v_list = NULL;
13549 }
13550 if (argvars[3].v_type != VAR_UNKNOWN
13551 && get_tv_number_chk(&argvars[3], &error))
13552 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013553 }
13554 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013555 if (!error)
13556 {
13557 ExpandInit(&xpc);
13558 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013559 if (p_wic)
13560 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013561 if (rettv->v_type == VAR_STRING)
13562 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013563 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013564 else if (rettv_list_alloc(rettv) != FAIL)
13565 {
13566 int i;
13567
13568 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13569 NULL, options, WILD_ALL_KEEP);
13570 for (i = 0; i < xpc.xp_numfiles; i++)
13571 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13572
13573 ExpandCleanup(&xpc);
13574 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013575 }
13576 else
13577 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578}
13579
13580/*
13581 * "globpath()" function
13582 */
13583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013584f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013586 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013588 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013589 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013590 garray_T ga;
13591 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013593 /* When the optional second argument is non-zero, don't remove matches
13594 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013595 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013596 if (argvars[2].v_type != VAR_UNKNOWN)
13597 {
13598 if (get_tv_number_chk(&argvars[2], &error))
13599 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013600 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013601 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013602 if (get_tv_number_chk(&argvars[3], &error))
13603 {
13604 rettv->v_type = VAR_LIST;
13605 rettv->vval.v_list = NULL;
13606 }
13607 if (argvars[4].v_type != VAR_UNKNOWN
13608 && get_tv_number_chk(&argvars[4], &error))
13609 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013610 }
13611 }
13612 if (file != NULL && !error)
13613 {
13614 ga_init2(&ga, (int)sizeof(char_u *), 10);
13615 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13616 if (rettv->v_type == VAR_STRING)
13617 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13618 else if (rettv_list_alloc(rettv) != FAIL)
13619 for (i = 0; i < ga.ga_len; ++i)
13620 list_append_string(rettv->vval.v_list,
13621 ((char_u **)(ga.ga_data))[i], -1);
13622 ga_clear_strings(&ga);
13623 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013624 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013625 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626}
13627
13628/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013629 * "glob2regpat()" function
13630 */
13631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013632f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013633{
13634 char_u *pat = get_tv_string_chk(&argvars[0]);
13635
13636 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013637 rettv->vval.v_string = (pat == NULL)
13638 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013639}
13640
13641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 * "has()" function
13643 */
13644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013645f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646{
13647 int i;
13648 char_u *name;
13649 int n = FALSE;
13650 static char *(has_list[]) =
13651 {
13652#ifdef AMIGA
13653 "amiga",
13654# ifdef FEAT_ARP
13655 "arp",
13656# endif
13657#endif
13658#ifdef __BEOS__
13659 "beos",
13660#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013661#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013662 "mac",
13663#endif
13664#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013665 "macunix", /* built with 'darwin' enabled */
13666#endif
13667#if defined(__APPLE__) && __APPLE__ == 1
13668 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670#ifdef __QNX__
13671 "qnx",
13672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673#ifdef UNIX
13674 "unix",
13675#endif
13676#ifdef VMS
13677 "vms",
13678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013679#ifdef WIN32
13680 "win32",
13681#endif
13682#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13683 "win32unix",
13684#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013685#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 "win64",
13687#endif
13688#ifdef EBCDIC
13689 "ebcdic",
13690#endif
13691#ifndef CASE_INSENSITIVE_FILENAME
13692 "fname_case",
13693#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013694#ifdef HAVE_ACL
13695 "acl",
13696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#ifdef FEAT_ARABIC
13698 "arabic",
13699#endif
13700#ifdef FEAT_AUTOCMD
13701 "autocmd",
13702#endif
13703#ifdef FEAT_BEVAL
13704 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013705# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13706 "balloon_multiline",
13707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#endif
13709#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13710 "builtin_terms",
13711# ifdef ALL_BUILTIN_TCAPS
13712 "all_builtin_terms",
13713# endif
13714#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013715#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13716 || defined(FEAT_GUI_W32) \
13717 || defined(FEAT_GUI_MOTIF))
13718 "browsefilter",
13719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef FEAT_BYTEOFF
13721 "byte_offset",
13722#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013723#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013724 "channel",
13725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726#ifdef FEAT_CINDENT
13727 "cindent",
13728#endif
13729#ifdef FEAT_CLIENTSERVER
13730 "clientserver",
13731#endif
13732#ifdef FEAT_CLIPBOARD
13733 "clipboard",
13734#endif
13735#ifdef FEAT_CMDL_COMPL
13736 "cmdline_compl",
13737#endif
13738#ifdef FEAT_CMDHIST
13739 "cmdline_hist",
13740#endif
13741#ifdef FEAT_COMMENTS
13742 "comments",
13743#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013744#ifdef FEAT_CONCEAL
13745 "conceal",
13746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747#ifdef FEAT_CRYPT
13748 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013749 "crypt-blowfish",
13750 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751#endif
13752#ifdef FEAT_CSCOPE
13753 "cscope",
13754#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013755#ifdef FEAT_CURSORBIND
13756 "cursorbind",
13757#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013758#ifdef CURSOR_SHAPE
13759 "cursorshape",
13760#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761#ifdef DEBUG
13762 "debug",
13763#endif
13764#ifdef FEAT_CON_DIALOG
13765 "dialog_con",
13766#endif
13767#ifdef FEAT_GUI_DIALOG
13768 "dialog_gui",
13769#endif
13770#ifdef FEAT_DIFF
13771 "diff",
13772#endif
13773#ifdef FEAT_DIGRAPHS
13774 "digraphs",
13775#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013776#ifdef FEAT_DIRECTX
13777 "directx",
13778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779#ifdef FEAT_DND
13780 "dnd",
13781#endif
13782#ifdef FEAT_EMACS_TAGS
13783 "emacs_tags",
13784#endif
13785 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013786 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#ifdef FEAT_SEARCH_EXTRA
13788 "extra_search",
13789#endif
13790#ifdef FEAT_FKMAP
13791 "farsi",
13792#endif
13793#ifdef FEAT_SEARCHPATH
13794 "file_in_path",
13795#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013796#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013797 "filterpipe",
13798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013799#ifdef FEAT_FIND_ID
13800 "find_in_path",
13801#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013802#ifdef FEAT_FLOAT
13803 "float",
13804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805#ifdef FEAT_FOLDING
13806 "folding",
13807#endif
13808#ifdef FEAT_FOOTER
13809 "footer",
13810#endif
13811#if !defined(USE_SYSTEM) && defined(UNIX)
13812 "fork",
13813#endif
13814#ifdef FEAT_GETTEXT
13815 "gettext",
13816#endif
13817#ifdef FEAT_GUI
13818 "gui",
13819#endif
13820#ifdef FEAT_GUI_ATHENA
13821# ifdef FEAT_GUI_NEXTAW
13822 "gui_neXtaw",
13823# else
13824 "gui_athena",
13825# endif
13826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#ifdef FEAT_GUI_GTK
13828 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013829# ifdef USE_GTK3
13830 "gui_gtk3",
13831# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013833# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013835#ifdef FEAT_GUI_GNOME
13836 "gui_gnome",
13837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838#ifdef FEAT_GUI_MAC
13839 "gui_mac",
13840#endif
13841#ifdef FEAT_GUI_MOTIF
13842 "gui_motif",
13843#endif
13844#ifdef FEAT_GUI_PHOTON
13845 "gui_photon",
13846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#ifdef FEAT_GUI_W32
13848 "gui_win32",
13849#endif
13850#ifdef FEAT_HANGULIN
13851 "hangul_input",
13852#endif
13853#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13854 "iconv",
13855#endif
13856#ifdef FEAT_INS_EXPAND
13857 "insert_expand",
13858#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013859#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013860 "job",
13861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862#ifdef FEAT_JUMPLIST
13863 "jumplist",
13864#endif
13865#ifdef FEAT_KEYMAP
13866 "keymap",
13867#endif
13868#ifdef FEAT_LANGMAP
13869 "langmap",
13870#endif
13871#ifdef FEAT_LIBCALL
13872 "libcall",
13873#endif
13874#ifdef FEAT_LINEBREAK
13875 "linebreak",
13876#endif
13877#ifdef FEAT_LISP
13878 "lispindent",
13879#endif
13880#ifdef FEAT_LISTCMDS
13881 "listcmds",
13882#endif
13883#ifdef FEAT_LOCALMAP
13884 "localmap",
13885#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013886#ifdef FEAT_LUA
13887# ifndef DYNAMIC_LUA
13888 "lua",
13889# endif
13890#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013891#ifdef FEAT_MENU
13892 "menu",
13893#endif
13894#ifdef FEAT_SESSION
13895 "mksession",
13896#endif
13897#ifdef FEAT_MODIFY_FNAME
13898 "modify_fname",
13899#endif
13900#ifdef FEAT_MOUSE
13901 "mouse",
13902#endif
13903#ifdef FEAT_MOUSESHAPE
13904 "mouseshape",
13905#endif
13906#if defined(UNIX) || defined(VMS)
13907# ifdef FEAT_MOUSE_DEC
13908 "mouse_dec",
13909# endif
13910# ifdef FEAT_MOUSE_GPM
13911 "mouse_gpm",
13912# endif
13913# ifdef FEAT_MOUSE_JSB
13914 "mouse_jsbterm",
13915# endif
13916# ifdef FEAT_MOUSE_NET
13917 "mouse_netterm",
13918# endif
13919# ifdef FEAT_MOUSE_PTERM
13920 "mouse_pterm",
13921# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013922# ifdef FEAT_MOUSE_SGR
13923 "mouse_sgr",
13924# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013925# ifdef FEAT_SYSMOUSE
13926 "mouse_sysmouse",
13927# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013928# ifdef FEAT_MOUSE_URXVT
13929 "mouse_urxvt",
13930# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931# ifdef FEAT_MOUSE_XTERM
13932 "mouse_xterm",
13933# endif
13934#endif
13935#ifdef FEAT_MBYTE
13936 "multi_byte",
13937#endif
13938#ifdef FEAT_MBYTE_IME
13939 "multi_byte_ime",
13940#endif
13941#ifdef FEAT_MULTI_LANG
13942 "multi_lang",
13943#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013944#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013945#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013946 "mzscheme",
13947#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949#ifdef FEAT_OLE
13950 "ole",
13951#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013952 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953#ifdef FEAT_PATH_EXTRA
13954 "path_extra",
13955#endif
13956#ifdef FEAT_PERL
13957#ifndef DYNAMIC_PERL
13958 "perl",
13959#endif
13960#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013961#ifdef FEAT_PERSISTENT_UNDO
13962 "persistent_undo",
13963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964#ifdef FEAT_PYTHON
13965#ifndef DYNAMIC_PYTHON
13966 "python",
13967#endif
13968#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013969#ifdef FEAT_PYTHON3
13970#ifndef DYNAMIC_PYTHON3
13971 "python3",
13972#endif
13973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974#ifdef FEAT_POSTSCRIPT
13975 "postscript",
13976#endif
13977#ifdef FEAT_PRINTER
13978 "printer",
13979#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013980#ifdef FEAT_PROFILE
13981 "profile",
13982#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013983#ifdef FEAT_RELTIME
13984 "reltime",
13985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013986#ifdef FEAT_QUICKFIX
13987 "quickfix",
13988#endif
13989#ifdef FEAT_RIGHTLEFT
13990 "rightleft",
13991#endif
13992#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13993 "ruby",
13994#endif
13995#ifdef FEAT_SCROLLBIND
13996 "scrollbind",
13997#endif
13998#ifdef FEAT_CMDL_INFO
13999 "showcmd",
14000 "cmdline_info",
14001#endif
14002#ifdef FEAT_SIGNS
14003 "signs",
14004#endif
14005#ifdef FEAT_SMARTINDENT
14006 "smartindent",
14007#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014008#ifdef STARTUPTIME
14009 "startuptime",
14010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef FEAT_STL_OPT
14012 "statusline",
14013#endif
14014#ifdef FEAT_SUN_WORKSHOP
14015 "sun_workshop",
14016#endif
14017#ifdef FEAT_NETBEANS_INTG
14018 "netbeans_intg",
14019#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014020#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014021 "spell",
14022#endif
14023#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 "syntax",
14025#endif
14026#if defined(USE_SYSTEM) || !defined(UNIX)
14027 "system",
14028#endif
14029#ifdef FEAT_TAG_BINS
14030 "tag_binary",
14031#endif
14032#ifdef FEAT_TAG_OLDSTATIC
14033 "tag_old_static",
14034#endif
14035#ifdef FEAT_TAG_ANYWHITE
14036 "tag_any_white",
14037#endif
14038#ifdef FEAT_TCL
14039# ifndef DYNAMIC_TCL
14040 "tcl",
14041# endif
14042#endif
14043#ifdef TERMINFO
14044 "terminfo",
14045#endif
14046#ifdef FEAT_TERMRESPONSE
14047 "termresponse",
14048#endif
Bram Moolenaar8a633e32016-04-21 21:10:14 +020014049#ifdef FEAT_TERMTRUECOLOR
14050 "termtruecolor",
14051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052#ifdef FEAT_TEXTOBJ
14053 "textobjects",
14054#endif
14055#ifdef HAVE_TGETENT
14056 "tgetent",
14057#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014058#ifdef FEAT_TIMERS
14059 "timers",
14060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061#ifdef FEAT_TITLE
14062 "title",
14063#endif
14064#ifdef FEAT_TOOLBAR
14065 "toolbar",
14066#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014067#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14068 "unnamedplus",
14069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070#ifdef FEAT_USR_CMDS
14071 "user-commands", /* was accidentally included in 5.4 */
14072 "user_commands",
14073#endif
14074#ifdef FEAT_VIMINFO
14075 "viminfo",
14076#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014077#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 "vertsplit",
14079#endif
14080#ifdef FEAT_VIRTUALEDIT
14081 "virtualedit",
14082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084#ifdef FEAT_VISUALEXTRA
14085 "visualextra",
14086#endif
14087#ifdef FEAT_VREPLACE
14088 "vreplace",
14089#endif
14090#ifdef FEAT_WILDIGN
14091 "wildignore",
14092#endif
14093#ifdef FEAT_WILDMENU
14094 "wildmenu",
14095#endif
14096#ifdef FEAT_WINDOWS
14097 "windows",
14098#endif
14099#ifdef FEAT_WAK
14100 "winaltkeys",
14101#endif
14102#ifdef FEAT_WRITEBACKUP
14103 "writebackup",
14104#endif
14105#ifdef FEAT_XIM
14106 "xim",
14107#endif
14108#ifdef FEAT_XFONTSET
14109 "xfontset",
14110#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014111#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014112 "xpm",
14113 "xpm_w32", /* for backward compatibility */
14114#else
14115# if defined(HAVE_XPM)
14116 "xpm",
14117# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119#ifdef USE_XSMP
14120 "xsmp",
14121#endif
14122#ifdef USE_XSMP_INTERACT
14123 "xsmp_interact",
14124#endif
14125#ifdef FEAT_XCLIPBOARD
14126 "xterm_clipboard",
14127#endif
14128#ifdef FEAT_XTERM_SAVE
14129 "xterm_save",
14130#endif
14131#if defined(UNIX) && defined(FEAT_X11)
14132 "X11",
14133#endif
14134 NULL
14135 };
14136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014137 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014138 for (i = 0; has_list[i] != NULL; ++i)
14139 if (STRICMP(name, has_list[i]) == 0)
14140 {
14141 n = TRUE;
14142 break;
14143 }
14144
14145 if (n == FALSE)
14146 {
14147 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014148 {
14149 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014150 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014151 && vim_isdigit(name[6])
14152 && vim_isdigit(name[8])
14153 && vim_isdigit(name[10]))
14154 {
14155 int major = atoi((char *)name + 6);
14156 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014157
14158 /* Expect "patch-9.9.01234". */
14159 n = (major < VIM_VERSION_MAJOR
14160 || (major == VIM_VERSION_MAJOR
14161 && (minor < VIM_VERSION_MINOR
14162 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014163 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014164 }
14165 else
14166 n = has_patch(atoi((char *)name + 5));
14167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 else if (STRICMP(name, "vim_starting") == 0)
14169 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014170#ifdef FEAT_MBYTE
14171 else if (STRICMP(name, "multi_byte_encoding") == 0)
14172 n = has_mbyte;
14173#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014174#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14175 else if (STRICMP(name, "balloon_multiline") == 0)
14176 n = multiline_balloon_available();
14177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178#ifdef DYNAMIC_TCL
14179 else if (STRICMP(name, "tcl") == 0)
14180 n = tcl_enabled(FALSE);
14181#endif
14182#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14183 else if (STRICMP(name, "iconv") == 0)
14184 n = iconv_enabled(FALSE);
14185#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014186#ifdef DYNAMIC_LUA
14187 else if (STRICMP(name, "lua") == 0)
14188 n = lua_enabled(FALSE);
14189#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014190#ifdef DYNAMIC_MZSCHEME
14191 else if (STRICMP(name, "mzscheme") == 0)
14192 n = mzscheme_enabled(FALSE);
14193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194#ifdef DYNAMIC_RUBY
14195 else if (STRICMP(name, "ruby") == 0)
14196 n = ruby_enabled(FALSE);
14197#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014198#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199#ifdef DYNAMIC_PYTHON
14200 else if (STRICMP(name, "python") == 0)
14201 n = python_enabled(FALSE);
14202#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014203#endif
14204#ifdef FEAT_PYTHON3
14205#ifdef DYNAMIC_PYTHON3
14206 else if (STRICMP(name, "python3") == 0)
14207 n = python3_enabled(FALSE);
14208#endif
14209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210#ifdef DYNAMIC_PERL
14211 else if (STRICMP(name, "perl") == 0)
14212 n = perl_enabled(FALSE);
14213#endif
14214#ifdef FEAT_GUI
14215 else if (STRICMP(name, "gui_running") == 0)
14216 n = (gui.in_use || gui.starting);
14217# ifdef FEAT_GUI_W32
14218 else if (STRICMP(name, "gui_win32s") == 0)
14219 n = gui_is_win32s();
14220# endif
14221# ifdef FEAT_BROWSE
14222 else if (STRICMP(name, "browse") == 0)
14223 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14224# endif
14225#endif
14226#ifdef FEAT_SYN_HL
14227 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014228 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229#endif
14230#if defined(WIN3264)
14231 else if (STRICMP(name, "win95") == 0)
14232 n = mch_windows95();
14233#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014234#ifdef FEAT_NETBEANS_INTG
14235 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014236 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 }
14239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014240 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241}
14242
14243/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014244 * "has_key()" function
14245 */
14246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014247f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014248{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014249 if (argvars[0].v_type != VAR_DICT)
14250 {
14251 EMSG(_(e_dictreq));
14252 return;
14253 }
14254 if (argvars[0].vval.v_dict == NULL)
14255 return;
14256
14257 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014258 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014259}
14260
14261/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014262 * "haslocaldir()" function
14263 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014265f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014266{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014267 win_T *wp = NULL;
14268
14269 wp = find_tabwin(&argvars[0], &argvars[1]);
14270 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014271}
14272
14273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014274 * "hasmapto()" function
14275 */
14276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014277f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278{
14279 char_u *name;
14280 char_u *mode;
14281 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014282 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014284 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014285 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 mode = (char_u *)"nvo";
14287 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014289 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014290 if (argvars[2].v_type != VAR_UNKNOWN)
14291 abbr = get_tv_number(&argvars[2]);
14292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
Bram Moolenaar2c932302006-03-18 21:42:09 +000014294 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014297 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298}
14299
14300/*
14301 * "histadd()" function
14302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014304f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305{
14306#ifdef FEAT_CMDHIST
14307 int histype;
14308 char_u *str;
14309 char_u buf[NUMBUFLEN];
14310#endif
14311
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014312 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 if (check_restricted() || check_secure())
14314 return;
14315#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14317 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318 if (histype >= 0)
14319 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 if (*str != NUL)
14322 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014323 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014325 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 return;
14327 }
14328 }
14329#endif
14330}
14331
14332/*
14333 * "histdel()" function
14334 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014336f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337{
14338#ifdef FEAT_CMDHIST
14339 int n;
14340 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014341 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014343 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14344 if (str == NULL)
14345 n = 0;
14346 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014347 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014348 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014349 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014352 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353 else
14354 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014355 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014356 get_tv_string_buf(&argvars[1], buf));
14357 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358#endif
14359}
14360
14361/*
14362 * "histget()" function
14363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014365f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366{
14367#ifdef FEAT_CMDHIST
14368 int type;
14369 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014370 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014372 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14373 if (str == NULL)
14374 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014376 {
14377 type = get_histtype(str);
14378 if (argvars[1].v_type == VAR_UNKNOWN)
14379 idx = get_history_idx(type);
14380 else
14381 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14382 /* -1 on type error */
14383 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389}
14390
14391/*
14392 * "histnr()" function
14393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014395f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014396{
14397 int i;
14398
14399#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014400 char_u *history = get_tv_string_chk(&argvars[0]);
14401
14402 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 if (i >= HIST_CMD && i < HIST_COUNT)
14404 i = get_history_idx(i);
14405 else
14406#endif
14407 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409}
14410
14411/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 * "highlightID(name)" function
14413 */
14414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014416{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418}
14419
14420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014421 * "highlight_exists()" function
14422 */
14423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014424f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014425{
14426 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14427}
14428
14429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 * "hostname()" function
14431 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014433f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434{
14435 char_u hostname[256];
14436
14437 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014438 rettv->v_type = VAR_STRING;
14439 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440}
14441
14442/*
14443 * iconv() function
14444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014446f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447{
14448#ifdef FEAT_MBYTE
14449 char_u buf1[NUMBUFLEN];
14450 char_u buf2[NUMBUFLEN];
14451 char_u *from, *to, *str;
14452 vimconv_T vimconv;
14453#endif
14454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014455 rettv->v_type = VAR_STRING;
14456 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457
14458#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459 str = get_tv_string(&argvars[0]);
14460 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14461 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 vimconv.vc_type = CONV_NONE;
14463 convert_setup(&vimconv, from, to);
14464
14465 /* If the encodings are equal, no conversion needed. */
14466 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470
14471 convert_setup(&vimconv, NULL, NULL);
14472 vim_free(from);
14473 vim_free(to);
14474#endif
14475}
14476
14477/*
14478 * "indent()" function
14479 */
14480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014481f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482{
14483 linenr_T lnum;
14484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014485 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014487 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014489 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490}
14491
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014492/*
14493 * "index()" function
14494 */
14495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014496f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014497{
Bram Moolenaar33570922005-01-25 22:26:29 +000014498 list_T *l;
14499 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014500 long idx = 0;
14501 int ic = FALSE;
14502
14503 rettv->vval.v_number = -1;
14504 if (argvars[0].v_type != VAR_LIST)
14505 {
14506 EMSG(_(e_listreq));
14507 return;
14508 }
14509 l = argvars[0].vval.v_list;
14510 if (l != NULL)
14511 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014512 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014513 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014514 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014515 int error = FALSE;
14516
Bram Moolenaar758711c2005-02-02 23:11:38 +000014517 /* Start at specified item. Use the cached index that list_find()
14518 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014520 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014521 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014522 ic = get_tv_number_chk(&argvars[3], &error);
14523 if (error)
14524 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014526
Bram Moolenaar758711c2005-02-02 23:11:38 +000014527 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014528 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014529 {
14530 rettv->vval.v_number = idx;
14531 break;
14532 }
14533 }
14534}
14535
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536static int inputsecret_flag = 0;
14537
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014538static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014539
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014541 * This function is used by f_input() and f_inputdialog() functions. The third
14542 * argument to f_input() specifies the type of completion to use at the
14543 * prompt. The third argument to f_inputdialog() specifies the value to return
14544 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545 */
14546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014547get_user_input(
14548 typval_T *argvars,
14549 typval_T *rettv,
14550 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014551{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014552 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553 char_u *p = NULL;
14554 int c;
14555 char_u buf[NUMBUFLEN];
14556 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014557 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014558 int xp_type = EXPAND_NOTHING;
14559 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014561 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014562 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014563
14564#ifdef NO_CONSOLE_INPUT
14565 /* While starting up, there is no place to enter text. */
14566 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568#endif
14569
14570 cmd_silent = FALSE; /* Want to see the prompt. */
14571 if (prompt != NULL)
14572 {
14573 /* Only the part of the message after the last NL is considered as
14574 * prompt for the command line */
14575 p = vim_strrchr(prompt, '\n');
14576 if (p == NULL)
14577 p = prompt;
14578 else
14579 {
14580 ++p;
14581 c = *p;
14582 *p = NUL;
14583 msg_start();
14584 msg_clr_eos();
14585 msg_puts_attr(prompt, echo_attr);
14586 msg_didout = FALSE;
14587 msg_starthere();
14588 *p = c;
14589 }
14590 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 if (argvars[1].v_type != VAR_UNKNOWN)
14593 {
14594 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14595 if (defstr != NULL)
14596 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014598 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014599 {
14600 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014601 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014602 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014603
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014604 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014605 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014606
Bram Moolenaar4463f292005-09-25 22:20:24 +000014607 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14608 if (xp_name == NULL)
14609 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014610
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014611 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014612
Bram Moolenaar4463f292005-09-25 22:20:24 +000014613 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14614 &xp_arg) == FAIL)
14615 return;
14616 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014617 }
14618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014620 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014621 int save_ex_normal_busy = ex_normal_busy;
14622 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014623 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014624 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14625 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014626 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014627 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014628 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014629 && argvars[1].v_type != VAR_UNKNOWN
14630 && argvars[2].v_type != VAR_UNKNOWN)
14631 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14632 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014633
14634 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014636 /* since the user typed this, no need to wait for return */
14637 need_wait_return = FALSE;
14638 msg_didout = FALSE;
14639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014640 cmd_silent = cmd_silent_save;
14641}
14642
14643/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014644 * "input()" function
14645 * Also handles inputsecret() when inputsecret is set.
14646 */
14647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014648f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014649{
14650 get_user_input(argvars, rettv, FALSE);
14651}
14652
14653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654 * "inputdialog()" function
14655 */
14656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014657f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658{
14659#if defined(FEAT_GUI_TEXTDIALOG)
14660 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14661 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14662 {
14663 char_u *message;
14664 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 message = get_tv_string_chk(&argvars[0]);
14668 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014669 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014670 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014671 else
14672 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014673 if (message != NULL && defstr != NULL
14674 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014675 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 else
14678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014679 if (message != NULL && defstr != NULL
14680 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014681 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014682 rettv->vval.v_string = vim_strsave(
14683 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014687 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 }
14689 else
14690#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014691 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692}
14693
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014694/*
14695 * "inputlist()" function
14696 */
14697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014698f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014699{
14700 listitem_T *li;
14701 int selected;
14702 int mouse_used;
14703
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014704#ifdef NO_CONSOLE_INPUT
14705 /* While starting up, there is no place to enter text. */
14706 if (no_console_input())
14707 return;
14708#endif
14709 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14710 {
14711 EMSG2(_(e_listarg), "inputlist()");
14712 return;
14713 }
14714
14715 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014716 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014717 lines_left = Rows; /* avoid more prompt */
14718 msg_scroll = TRUE;
14719 msg_clr_eos();
14720
14721 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14722 {
14723 msg_puts(get_tv_string(&li->li_tv));
14724 msg_putchar('\n');
14725 }
14726
14727 /* Ask for choice. */
14728 selected = prompt_for_number(&mouse_used);
14729 if (mouse_used)
14730 selected -= lines_left;
14731
14732 rettv->vval.v_number = selected;
14733}
14734
14735
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14737
14738/*
14739 * "inputrestore()" function
14740 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014742f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014743{
14744 if (ga_userinput.ga_len > 0)
14745 {
14746 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014747 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14748 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014749 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014750 }
14751 else if (p_verbose > 1)
14752 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014753 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014754 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 }
14756}
14757
14758/*
14759 * "inputsave()" function
14760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014762f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014763{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014764 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765 if (ga_grow(&ga_userinput, 1) == OK)
14766 {
14767 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14768 + ga_userinput.ga_len);
14769 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014770 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014771 }
14772 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014773 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014774}
14775
14776/*
14777 * "inputsecret()" function
14778 */
14779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014780f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014781{
14782 ++cmdline_star;
14783 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014784 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014785 --cmdline_star;
14786 --inputsecret_flag;
14787}
14788
14789/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014790 * "insert()" function
14791 */
14792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014793f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014794{
14795 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014796 listitem_T *item;
14797 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014798 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014799
14800 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014801 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014802 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014803 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014804 {
14805 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014806 before = get_tv_number_chk(&argvars[2], &error);
14807 if (error)
14808 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014809
Bram Moolenaar758711c2005-02-02 23:11:38 +000014810 if (before == l->lv_len)
14811 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014812 else
14813 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014814 item = list_find(l, before);
14815 if (item == NULL)
14816 {
14817 EMSGN(_(e_listidx), before);
14818 l = NULL;
14819 }
14820 }
14821 if (l != NULL)
14822 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014823 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014824 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014825 }
14826 }
14827}
14828
14829/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014830 * "invert(expr)" function
14831 */
14832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014833f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014834{
14835 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14836}
14837
14838/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839 * "isdirectory()" function
14840 */
14841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014842f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014844 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845}
14846
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014847/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014848 * "islocked()" function
14849 */
14850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014851f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014852{
14853 lval_T lv;
14854 char_u *end;
14855 dictitem_T *di;
14856
14857 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014858 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14859 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014860 if (end != NULL && lv.ll_name != NULL)
14861 {
14862 if (*end != NUL)
14863 EMSG(_(e_trailing));
14864 else
14865 {
14866 if (lv.ll_tv == NULL)
14867 {
14868 if (check_changedtick(lv.ll_name))
14869 rettv->vval.v_number = 1; /* always locked */
14870 else
14871 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014872 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014873 if (di != NULL)
14874 {
14875 /* Consider a variable locked when:
14876 * 1. the variable itself is locked
14877 * 2. the value of the variable is locked.
14878 * 3. the List or Dict value is locked.
14879 */
14880 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14881 || tv_islocked(&di->di_tv));
14882 }
14883 }
14884 }
14885 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014886 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014887 else if (lv.ll_newkey != NULL)
14888 EMSG2(_(e_dictkey), lv.ll_newkey);
14889 else if (lv.ll_list != NULL)
14890 /* List item. */
14891 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14892 else
14893 /* Dictionary item. */
14894 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14895 }
14896 }
14897
14898 clear_lval(&lv);
14899}
14900
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014901#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14902/*
14903 * "isnan()" function
14904 */
14905 static void
14906f_isnan(typval_T *argvars, typval_T *rettv)
14907{
14908 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14909 && isnan(argvars[0].vval.v_float);
14910}
14911#endif
14912
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014913static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014914
14915/*
14916 * Turn a dict into a list:
14917 * "what" == 0: list of keys
14918 * "what" == 1: list of values
14919 * "what" == 2: list of items
14920 */
14921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014922dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014923{
Bram Moolenaar33570922005-01-25 22:26:29 +000014924 list_T *l2;
14925 dictitem_T *di;
14926 hashitem_T *hi;
14927 listitem_T *li;
14928 listitem_T *li2;
14929 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014930 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014931
Bram Moolenaar8c711452005-01-14 21:53:12 +000014932 if (argvars[0].v_type != VAR_DICT)
14933 {
14934 EMSG(_(e_dictreq));
14935 return;
14936 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014937 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014938 return;
14939
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014940 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014941 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014943 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014944 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014945 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014946 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014947 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014948 --todo;
14949 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014950
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014951 li = listitem_alloc();
14952 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014953 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014954 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014955
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014956 if (what == 0)
14957 {
14958 /* keys() */
14959 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014960 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014961 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14962 }
14963 else if (what == 1)
14964 {
14965 /* values() */
14966 copy_tv(&di->di_tv, &li->li_tv);
14967 }
14968 else
14969 {
14970 /* items() */
14971 l2 = list_alloc();
14972 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014973 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014974 li->li_tv.vval.v_list = l2;
14975 if (l2 == NULL)
14976 break;
14977 ++l2->lv_refcount;
14978
14979 li2 = listitem_alloc();
14980 if (li2 == NULL)
14981 break;
14982 list_append(l2, li2);
14983 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014984 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014985 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14986
14987 li2 = listitem_alloc();
14988 if (li2 == NULL)
14989 break;
14990 list_append(l2, li2);
14991 copy_tv(&di->di_tv, &li2->li_tv);
14992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014993 }
14994 }
14995}
14996
14997/*
14998 * "items(dict)" function
14999 */
15000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015001f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015002{
15003 dict_list(argvars, rettv, 2);
15004}
15005
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015006#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015007/*
15008 * Get the job from the argument.
15009 * Returns NULL if the job is invalid.
15010 */
15011 static job_T *
15012get_job_arg(typval_T *tv)
15013{
15014 job_T *job;
15015
15016 if (tv->v_type != VAR_JOB)
15017 {
15018 EMSG2(_(e_invarg2), get_tv_string(tv));
15019 return NULL;
15020 }
15021 job = tv->vval.v_job;
15022
15023 if (job == NULL)
15024 EMSG(_("E916: not a valid job"));
15025 return job;
15026}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015027
Bram Moolenaar835dc632016-02-07 14:27:38 +010015028/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015029 * "job_getchannel()" function
15030 */
15031 static void
15032f_job_getchannel(typval_T *argvars, typval_T *rettv)
15033{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015034 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015035
Bram Moolenaar65edff82016-02-21 16:40:11 +010015036 if (job != NULL)
15037 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015038 rettv->v_type = VAR_CHANNEL;
15039 rettv->vval.v_channel = job->jv_channel;
15040 if (job->jv_channel != NULL)
15041 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015042 }
15043}
15044
15045/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015046 * "job_info()" function
15047 */
15048 static void
15049f_job_info(typval_T *argvars, typval_T *rettv)
15050{
15051 job_T *job = get_job_arg(&argvars[0]);
15052
15053 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15054 job_info(job, rettv->vval.v_dict);
15055}
15056
15057/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015058 * "job_setoptions()" function
15059 */
15060 static void
15061f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15062{
15063 job_T *job = get_job_arg(&argvars[0]);
15064 jobopt_T opt;
15065
15066 if (job == NULL)
15067 return;
15068 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015069 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15070 job_set_options(job, &opt);
15071 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015072}
15073
15074/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015075 * "job_start()" function
15076 */
15077 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015078f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015079{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015080 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015081 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015082}
15083
15084/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015085 * "job_status()" function
15086 */
15087 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015088f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015089{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015090 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015091
Bram Moolenaar65edff82016-02-21 16:40:11 +010015092 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015093 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015094 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015095 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015096 }
15097}
15098
15099/*
15100 * "job_stop()" function
15101 */
15102 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015103f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015104{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015105 job_T *job = get_job_arg(&argvars[0]);
15106
15107 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015108 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015109}
15110#endif
15111
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015113 * "join()" function
15114 */
15115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015116f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015117{
15118 garray_T ga;
15119 char_u *sep;
15120
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015121 if (argvars[0].v_type != VAR_LIST)
15122 {
15123 EMSG(_(e_listreq));
15124 return;
15125 }
15126 if (argvars[0].vval.v_list == NULL)
15127 return;
15128 if (argvars[1].v_type == VAR_UNKNOWN)
15129 sep = (char_u *)" ";
15130 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015131 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015132
15133 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134
15135 if (sep != NULL)
15136 {
15137 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015138 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015139 ga_append(&ga, NUL);
15140 rettv->vval.v_string = (char_u *)ga.ga_data;
15141 }
15142 else
15143 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015144}
15145
15146/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015147 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015148 */
15149 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015150f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015151{
15152 js_read_T reader;
15153
15154 reader.js_buf = get_tv_string(&argvars[0]);
15155 reader.js_fill = NULL;
15156 reader.js_used = 0;
15157 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15158 EMSG(_(e_invarg));
15159}
15160
15161/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015162 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015163 */
15164 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015165f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015166{
15167 rettv->v_type = VAR_STRING;
15168 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15169}
15170
15171/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015172 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015173 */
15174 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015175f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015176{
15177 js_read_T reader;
15178
15179 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015180 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015181 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015182 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015183 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015184}
15185
15186/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015187 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015188 */
15189 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015190f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015191{
15192 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015193 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015194}
15195
15196/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015197 * "keys()" function
15198 */
15199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015200f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015201{
15202 dict_list(argvars, rettv, 0);
15203}
15204
15205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 * "last_buffer_nr()" function.
15207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015209f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015210{
15211 int n = 0;
15212 buf_T *buf;
15213
15214 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15215 if (n < buf->b_fnum)
15216 n = buf->b_fnum;
15217
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015218 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015219}
15220
15221/*
15222 * "len()" function
15223 */
15224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015225f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015226{
15227 switch (argvars[0].v_type)
15228 {
15229 case VAR_STRING:
15230 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015231 rettv->vval.v_number = (varnumber_T)STRLEN(
15232 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233 break;
15234 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015235 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015236 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015237 case VAR_DICT:
15238 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15239 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015240 case VAR_UNKNOWN:
15241 case VAR_SPECIAL:
15242 case VAR_FLOAT:
15243 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015244 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015245 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015246 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015247 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015248 break;
15249 }
15250}
15251
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015252static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015253
15254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015255libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015256{
15257#ifdef FEAT_LIBCALL
15258 char_u *string_in;
15259 char_u **string_result;
15260 int nr_result;
15261#endif
15262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015263 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015264 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015265 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015266
15267 if (check_restricted() || check_secure())
15268 return;
15269
15270#ifdef FEAT_LIBCALL
15271 /* The first two args must be strings, otherwise its meaningless */
15272 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15273 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015274 string_in = NULL;
15275 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015276 string_in = argvars[2].vval.v_string;
15277 if (type == VAR_NUMBER)
15278 string_result = NULL;
15279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015280 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015281 if (mch_libcall(argvars[0].vval.v_string,
15282 argvars[1].vval.v_string,
15283 string_in,
15284 argvars[2].vval.v_number,
15285 string_result,
15286 &nr_result) == OK
15287 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015289 }
15290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291}
15292
15293/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015294 * "libcall()" function
15295 */
15296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015297f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015298{
15299 libcall_common(argvars, rettv, VAR_STRING);
15300}
15301
15302/*
15303 * "libcallnr()" function
15304 */
15305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015306f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015307{
15308 libcall_common(argvars, rettv, VAR_NUMBER);
15309}
15310
15311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015312 * "line(string)" function
15313 */
15314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015315f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015316{
15317 linenr_T lnum = 0;
15318 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015319 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015320
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015321 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015322 if (fp != NULL)
15323 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325}
15326
15327/*
15328 * "line2byte(lnum)" function
15329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015331f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332{
15333#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015334 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335#else
15336 linenr_T lnum;
15337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015338 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015340 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015342 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15343 if (rettv->vval.v_number >= 0)
15344 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345#endif
15346}
15347
15348/*
15349 * "lispindent(lnum)" function
15350 */
15351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015352f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353{
15354#ifdef FEAT_LISP
15355 pos_T pos;
15356 linenr_T lnum;
15357
15358 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015359 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15361 {
15362 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 curwin->w_cursor = pos;
15365 }
15366 else
15367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015368 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015369}
15370
15371/*
15372 * "localtime()" function
15373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015375f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015377 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015378}
15379
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015380static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015381
15382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015383get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384{
15385 char_u *keys;
15386 char_u *which;
15387 char_u buf[NUMBUFLEN];
15388 char_u *keys_buf = NULL;
15389 char_u *rhs;
15390 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015391 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015392 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015393 mapblock_T *mp;
15394 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395
15396 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->v_type = VAR_STRING;
15398 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015400 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015401 if (*keys == NUL)
15402 return;
15403
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015404 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015406 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015407 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015408 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015409 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015410 if (argvars[3].v_type != VAR_UNKNOWN)
15411 get_dict = get_tv_number(&argvars[3]);
15412 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 else
15415 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015416 if (which == NULL)
15417 return;
15418
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 mode = get_map_mode(&which, 0);
15420
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015421 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015422 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015424
15425 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015427 /* Return a string. */
15428 if (rhs != NULL)
15429 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430
Bram Moolenaarbd743252010-10-20 21:23:33 +020015431 }
15432 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15433 {
15434 /* Return a dictionary. */
15435 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15436 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15437 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438
Bram Moolenaarbd743252010-10-20 21:23:33 +020015439 dict_add_nr_str(dict, "lhs", 0L, lhs);
15440 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15441 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15442 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15443 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15444 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15445 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015446 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015447 dict_add_nr_str(dict, "mode", 0L, mapmode);
15448
15449 vim_free(lhs);
15450 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 }
15452}
15453
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015454#ifdef FEAT_FLOAT
15455/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015456 * "log()" function
15457 */
15458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015459f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015460{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015461 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015462
15463 rettv->v_type = VAR_FLOAT;
15464 if (get_float_arg(argvars, &f) == OK)
15465 rettv->vval.v_float = log(f);
15466 else
15467 rettv->vval.v_float = 0.0;
15468}
15469
15470/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015471 * "log10()" function
15472 */
15473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015474f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015475{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015476 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015477
15478 rettv->v_type = VAR_FLOAT;
15479 if (get_float_arg(argvars, &f) == OK)
15480 rettv->vval.v_float = log10(f);
15481 else
15482 rettv->vval.v_float = 0.0;
15483}
15484#endif
15485
Bram Moolenaar1dced572012-04-05 16:54:08 +020015486#ifdef FEAT_LUA
15487/*
15488 * "luaeval()" function
15489 */
15490 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015491f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015492{
15493 char_u *str;
15494 char_u buf[NUMBUFLEN];
15495
15496 str = get_tv_string_buf(&argvars[0], buf);
15497 do_luaeval(str, argvars + 1, rettv);
15498}
15499#endif
15500
Bram Moolenaar071d4272004-06-13 20:20:40 +000015501/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015502 * "map()" function
15503 */
15504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015505f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015506{
15507 filter_map(argvars, rettv, TRUE);
15508}
15509
15510/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 */
15513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015514f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015516 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015517}
15518
15519/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015520 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 */
15522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015523f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015524{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015525 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526}
15527
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015528static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529
15530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015531find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015533 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015534 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015535 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015536 char_u *pat;
15537 regmatch_T regmatch;
15538 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015539 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015540 char_u *save_cpo;
15541 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015542 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015543 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015544 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015545 list_T *l = NULL;
15546 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015547 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015548 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015549
15550 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15551 save_cpo = p_cpo;
15552 p_cpo = (char_u *)"";
15553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015554 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015555 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015556 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015557 /* type 3: return empty list when there are no matches.
15558 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015559 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015560 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015561 if (type == 4
15562 && (list_append_string(rettv->vval.v_list,
15563 (char_u *)"", 0) == FAIL
15564 || list_append_number(rettv->vval.v_list,
15565 (varnumber_T)-1) == FAIL
15566 || list_append_number(rettv->vval.v_list,
15567 (varnumber_T)-1) == FAIL
15568 || list_append_number(rettv->vval.v_list,
15569 (varnumber_T)-1) == FAIL))
15570 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015571 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015572 rettv->vval.v_list = NULL;
15573 goto theend;
15574 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015575 }
15576 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015578 rettv->v_type = VAR_STRING;
15579 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015582 if (argvars[0].v_type == VAR_LIST)
15583 {
15584 if ((l = argvars[0].vval.v_list) == NULL)
15585 goto theend;
15586 li = l->lv_first;
15587 }
15588 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015589 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015590 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015591 len = (long)STRLEN(str);
15592 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015594 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15595 if (pat == NULL)
15596 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015597
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015598 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 int error = FALSE;
15601
15602 start = get_tv_number_chk(&argvars[2], &error);
15603 if (error)
15604 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015605 if (l != NULL)
15606 {
15607 li = list_find(l, start);
15608 if (li == NULL)
15609 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015610 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015611 }
15612 else
15613 {
15614 if (start < 0)
15615 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015616 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015617 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015618 /* When "count" argument is there ignore matches before "start",
15619 * otherwise skip part of the string. Differs when pattern is "^"
15620 * or "\<". */
15621 if (argvars[3].v_type != VAR_UNKNOWN)
15622 startcol = start;
15623 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015624 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015625 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015626 len -= start;
15627 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015628 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015629
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015630 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015631 nth = get_tv_number_chk(&argvars[3], &error);
15632 if (error)
15633 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015634 }
15635
15636 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15637 if (regmatch.regprog != NULL)
15638 {
15639 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015640
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015641 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015642 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015643 if (l != NULL)
15644 {
15645 if (li == NULL)
15646 {
15647 match = FALSE;
15648 break;
15649 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015650 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015651 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015652 if (str == NULL)
15653 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015654 }
15655
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015656 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015657
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015658 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015659 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015660 if (l == NULL && !match)
15661 break;
15662
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015663 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664 if (l != NULL)
15665 {
15666 li = li->li_next;
15667 ++idx;
15668 }
15669 else
15670 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015671#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015672 startcol = (colnr_T)(regmatch.startp[0]
15673 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015674#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015675 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015676#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015677 if (startcol > (colnr_T)len
15678 || str + startcol <= regmatch.startp[0])
15679 {
15680 match = FALSE;
15681 break;
15682 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015683 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015684 }
15685
15686 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015688 if (type == 4)
15689 {
15690 listitem_T *li1 = rettv->vval.v_list->lv_first;
15691 listitem_T *li2 = li1->li_next;
15692 listitem_T *li3 = li2->li_next;
15693 listitem_T *li4 = li3->li_next;
15694
15695 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15696 (int)(regmatch.endp[0] - regmatch.startp[0]));
15697 li3->li_tv.vval.v_number =
15698 (varnumber_T)(regmatch.startp[0] - expr);
15699 li4->li_tv.vval.v_number =
15700 (varnumber_T)(regmatch.endp[0] - expr);
15701 if (l != NULL)
15702 li2->li_tv.vval.v_number = (varnumber_T)idx;
15703 }
15704 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015705 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015706 int i;
15707
15708 /* return list with matched string and submatches */
15709 for (i = 0; i < NSUBEXP; ++i)
15710 {
15711 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015712 {
15713 if (list_append_string(rettv->vval.v_list,
15714 (char_u *)"", 0) == FAIL)
15715 break;
15716 }
15717 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015718 regmatch.startp[i],
15719 (int)(regmatch.endp[i] - regmatch.startp[i]))
15720 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015721 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015722 }
15723 }
15724 else if (type == 2)
15725 {
15726 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015727 if (l != NULL)
15728 copy_tv(&li->li_tv, rettv);
15729 else
15730 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015732 }
15733 else if (l != NULL)
15734 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 else
15736 {
15737 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739 (varnumber_T)(regmatch.startp[0] - str);
15740 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015741 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015743 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015744 }
15745 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015746 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 }
15748
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015749 if (type == 4 && l == NULL)
15750 /* matchstrpos() without a list: drop the second item. */
15751 listitem_remove(rettv->vval.v_list,
15752 rettv->vval.v_list->lv_first->li_next);
15753
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015755 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015756 p_cpo = save_cpo;
15757}
15758
15759/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015760 * "match()" function
15761 */
15762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015763f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015764{
15765 find_some_match(argvars, rettv, 1);
15766}
15767
15768/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015769 * "matchadd()" function
15770 */
15771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015772f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015773{
15774#ifdef FEAT_SEARCH_EXTRA
15775 char_u buf[NUMBUFLEN];
15776 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15777 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15778 int prio = 10; /* default priority */
15779 int id = -1;
15780 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015781 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015782
15783 rettv->vval.v_number = -1;
15784
15785 if (grp == NULL || pat == NULL)
15786 return;
15787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015788 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015789 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015790 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015791 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015792 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015793 if (argvars[4].v_type != VAR_UNKNOWN)
15794 {
15795 if (argvars[4].v_type != VAR_DICT)
15796 {
15797 EMSG(_(e_dictreq));
15798 return;
15799 }
15800 if (dict_find(argvars[4].vval.v_dict,
15801 (char_u *)"conceal", -1) != NULL)
15802 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15803 (char_u *)"conceal", FALSE);
15804 }
15805 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015806 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015807 if (error == TRUE)
15808 return;
15809 if (id >= 1 && id <= 3)
15810 {
15811 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15812 return;
15813 }
15814
Bram Moolenaar6561d522015-07-21 15:48:27 +020015815 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15816 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015817#endif
15818}
15819
15820/*
15821 * "matchaddpos()" function
15822 */
15823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015824f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015825{
15826#ifdef FEAT_SEARCH_EXTRA
15827 char_u buf[NUMBUFLEN];
15828 char_u *group;
15829 int prio = 10;
15830 int id = -1;
15831 int error = FALSE;
15832 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015833 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015834
15835 rettv->vval.v_number = -1;
15836
15837 group = get_tv_string_buf_chk(&argvars[0], buf);
15838 if (group == NULL)
15839 return;
15840
15841 if (argvars[1].v_type != VAR_LIST)
15842 {
15843 EMSG2(_(e_listarg), "matchaddpos()");
15844 return;
15845 }
15846 l = argvars[1].vval.v_list;
15847 if (l == NULL)
15848 return;
15849
15850 if (argvars[2].v_type != VAR_UNKNOWN)
15851 {
15852 prio = get_tv_number_chk(&argvars[2], &error);
15853 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015854 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015855 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015856 if (argvars[4].v_type != VAR_UNKNOWN)
15857 {
15858 if (argvars[4].v_type != VAR_DICT)
15859 {
15860 EMSG(_(e_dictreq));
15861 return;
15862 }
15863 if (dict_find(argvars[4].vval.v_dict,
15864 (char_u *)"conceal", -1) != NULL)
15865 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15866 (char_u *)"conceal", FALSE);
15867 }
15868 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015869 }
15870 if (error == TRUE)
15871 return;
15872
15873 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15874 if (id == 1 || id == 2)
15875 {
15876 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15877 return;
15878 }
15879
Bram Moolenaar6561d522015-07-21 15:48:27 +020015880 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15881 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015882#endif
15883}
15884
15885/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015886 * "matcharg()" function
15887 */
15888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015889f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015890{
15891 if (rettv_list_alloc(rettv) == OK)
15892 {
15893#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015894 int id = get_tv_number(&argvars[0]);
15895 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015896
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015897 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015898 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015899 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15900 {
15901 list_append_string(rettv->vval.v_list,
15902 syn_id2name(m->hlg_id), -1);
15903 list_append_string(rettv->vval.v_list, m->pattern, -1);
15904 }
15905 else
15906 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015907 list_append_string(rettv->vval.v_list, NULL, -1);
15908 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015909 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015910 }
15911#endif
15912 }
15913}
15914
15915/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015916 * "matchdelete()" function
15917 */
15918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015919f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015920{
15921#ifdef FEAT_SEARCH_EXTRA
15922 rettv->vval.v_number = match_delete(curwin,
15923 (int)get_tv_number(&argvars[0]), TRUE);
15924#endif
15925}
15926
15927/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015928 * "matchend()" function
15929 */
15930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015931f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015932{
15933 find_some_match(argvars, rettv, 0);
15934}
15935
15936/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015937 * "matchlist()" function
15938 */
15939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015940f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015941{
15942 find_some_match(argvars, rettv, 3);
15943}
15944
15945/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015946 * "matchstr()" function
15947 */
15948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015949f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015950{
15951 find_some_match(argvars, rettv, 2);
15952}
15953
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015954/*
15955 * "matchstrpos()" function
15956 */
15957 static void
15958f_matchstrpos(typval_T *argvars, typval_T *rettv)
15959{
15960 find_some_match(argvars, rettv, 4);
15961}
15962
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015963static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015964
15965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015966max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015967{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015968 long n = 0;
15969 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015970 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015971
15972 if (argvars[0].v_type == VAR_LIST)
15973 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015974 list_T *l;
15975 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015976
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015977 l = argvars[0].vval.v_list;
15978 if (l != NULL)
15979 {
15980 li = l->lv_first;
15981 if (li != NULL)
15982 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015983 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015984 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015985 {
15986 li = li->li_next;
15987 if (li == NULL)
15988 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015989 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015990 if (domax ? i > n : i < n)
15991 n = i;
15992 }
15993 }
15994 }
15995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015996 else if (argvars[0].v_type == VAR_DICT)
15997 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015999 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016001 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016002
16003 d = argvars[0].vval.v_dict;
16004 if (d != NULL)
16005 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016006 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016008 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016009 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016010 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016011 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016012 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016013 if (first)
16014 {
16015 n = i;
16016 first = FALSE;
16017 }
16018 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016019 n = i;
16020 }
16021 }
16022 }
16023 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016024 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016025 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016027}
16028
16029/*
16030 * "max()" function
16031 */
16032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016033f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016034{
16035 max_min(argvars, rettv, TRUE);
16036}
16037
16038/*
16039 * "min()" function
16040 */
16041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016042f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016043{
16044 max_min(argvars, rettv, FALSE);
16045}
16046
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016047static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016048
16049/*
16050 * Create the directory in which "dir" is located, and higher levels when
16051 * needed.
16052 */
16053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016054mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016055{
16056 char_u *p;
16057 char_u *updir;
16058 int r = FAIL;
16059
16060 /* Get end of directory name in "dir".
16061 * We're done when it's "/" or "c:/". */
16062 p = gettail_sep(dir);
16063 if (p <= get_past_head(dir))
16064 return OK;
16065
16066 /* If the directory exists we're done. Otherwise: create it.*/
16067 updir = vim_strnsave(dir, (int)(p - dir));
16068 if (updir == NULL)
16069 return FAIL;
16070 if (mch_isdir(updir))
16071 r = OK;
16072 else if (mkdir_recurse(updir, prot) == OK)
16073 r = vim_mkdir_emsg(updir, prot);
16074 vim_free(updir);
16075 return r;
16076}
16077
16078#ifdef vim_mkdir
16079/*
16080 * "mkdir()" function
16081 */
16082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016083f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016084{
16085 char_u *dir;
16086 char_u buf[NUMBUFLEN];
16087 int prot = 0755;
16088
16089 rettv->vval.v_number = FAIL;
16090 if (check_restricted() || check_secure())
16091 return;
16092
16093 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016094 if (*dir == NUL)
16095 rettv->vval.v_number = FAIL;
16096 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016097 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016098 if (*gettail(dir) == NUL)
16099 /* remove trailing slashes */
16100 *gettail_sep(dir) = NUL;
16101
16102 if (argvars[1].v_type != VAR_UNKNOWN)
16103 {
16104 if (argvars[2].v_type != VAR_UNKNOWN)
16105 prot = get_tv_number_chk(&argvars[2], NULL);
16106 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16107 mkdir_recurse(dir, prot);
16108 }
16109 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016110 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016111}
16112#endif
16113
Bram Moolenaar0d660222005-01-07 21:51:51 +000016114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016115 * "mode()" function
16116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016119{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016120 char_u buf[3];
16121
16122 buf[1] = NUL;
16123 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016124
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 if (VIsual_active)
16126 {
16127 if (VIsual_select)
16128 buf[0] = VIsual_mode + 's' - 'v';
16129 else
16130 buf[0] = VIsual_mode;
16131 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016132 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016133 || State == CONFIRM)
16134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016135 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016136 if (State == ASKMORE)
16137 buf[1] = 'm';
16138 else if (State == CONFIRM)
16139 buf[1] = '?';
16140 }
16141 else if (State == EXTERNCMD)
16142 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016143 else if (State & INSERT)
16144 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016145#ifdef FEAT_VREPLACE
16146 if (State & VREPLACE_FLAG)
16147 {
16148 buf[0] = 'R';
16149 buf[1] = 'v';
16150 }
16151 else
16152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 if (State & REPLACE_FLAG)
16154 buf[0] = 'R';
16155 else
16156 buf[0] = 'i';
16157 }
16158 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016161 if (exmode_active)
16162 buf[1] = 'v';
16163 }
16164 else if (exmode_active)
16165 {
16166 buf[0] = 'c';
16167 buf[1] = 'e';
16168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016172 if (finish_op)
16173 buf[1] = 'o';
16174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016176 /* Clear out the minor mode when the argument is not a non-zero number or
16177 * non-empty string. */
16178 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016179 buf[1] = NUL;
16180
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016181 rettv->vval.v_string = vim_strsave(buf);
16182 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183}
16184
Bram Moolenaar429fa852013-04-15 12:27:36 +020016185#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016186/*
16187 * "mzeval()" function
16188 */
16189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016190f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016191{
16192 char_u *str;
16193 char_u buf[NUMBUFLEN];
16194
16195 str = get_tv_string_buf(&argvars[0], buf);
16196 do_mzeval(str, rettv);
16197}
Bram Moolenaar75676462013-01-30 14:55:42 +010016198
16199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016200mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016201{
16202 typval_T argvars[3];
16203
16204 argvars[0].v_type = VAR_STRING;
16205 argvars[0].vval.v_string = name;
16206 copy_tv(args, &argvars[1]);
16207 argvars[2].v_type = VAR_UNKNOWN;
16208 f_call(argvars, rettv);
16209 clear_tv(&argvars[1]);
16210}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016211#endif
16212
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016214 * "nextnonblank()" function
16215 */
16216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016217f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016218{
16219 linenr_T lnum;
16220
16221 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016223 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016224 {
16225 lnum = 0;
16226 break;
16227 }
16228 if (*skipwhite(ml_get(lnum)) != NUL)
16229 break;
16230 }
16231 rettv->vval.v_number = lnum;
16232}
16233
16234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016235 * "nr2char()" function
16236 */
16237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016238f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239{
16240 char_u buf[NUMBUFLEN];
16241
16242#ifdef FEAT_MBYTE
16243 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016244 {
16245 int utf8 = 0;
16246
16247 if (argvars[1].v_type != VAR_UNKNOWN)
16248 utf8 = get_tv_number_chk(&argvars[1], NULL);
16249 if (utf8)
16250 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16251 else
16252 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 else
16255#endif
16256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258 buf[1] = NUL;
16259 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016260 rettv->v_type = VAR_STRING;
16261 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016262}
16263
16264/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016265 * "or(expr, expr)" function
16266 */
16267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016268f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016269{
16270 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16271 | get_tv_number_chk(&argvars[1], NULL);
16272}
16273
16274/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016275 * "pathshorten()" function
16276 */
16277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016278f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016279{
16280 char_u *p;
16281
16282 rettv->v_type = VAR_STRING;
16283 p = get_tv_string_chk(&argvars[0]);
16284 if (p == NULL)
16285 rettv->vval.v_string = NULL;
16286 else
16287 {
16288 p = vim_strsave(p);
16289 rettv->vval.v_string = p;
16290 if (p != NULL)
16291 shorten_dir(p);
16292 }
16293}
16294
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016295#ifdef FEAT_PERL
16296/*
16297 * "perleval()" function
16298 */
16299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016300f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016301{
16302 char_u *str;
16303 char_u buf[NUMBUFLEN];
16304
16305 str = get_tv_string_buf(&argvars[0], buf);
16306 do_perleval(str, rettv);
16307}
16308#endif
16309
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016310#ifdef FEAT_FLOAT
16311/*
16312 * "pow()" function
16313 */
16314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016315f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016316{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016317 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016318
16319 rettv->v_type = VAR_FLOAT;
16320 if (get_float_arg(argvars, &fx) == OK
16321 && get_float_arg(&argvars[1], &fy) == OK)
16322 rettv->vval.v_float = pow(fx, fy);
16323 else
16324 rettv->vval.v_float = 0.0;
16325}
16326#endif
16327
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016329 * "prevnonblank()" function
16330 */
16331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016332f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016333{
16334 linenr_T lnum;
16335
16336 lnum = get_tv_lnum(argvars);
16337 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16338 lnum = 0;
16339 else
16340 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16341 --lnum;
16342 rettv->vval.v_number = lnum;
16343}
16344
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016345/* This dummy va_list is here because:
16346 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16347 * - locally in the function results in a "used before set" warning
16348 * - using va_start() to initialize it gives "function with fixed args" error */
16349static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016350
Bram Moolenaar8c711452005-01-14 21:53:12 +000016351/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016352 * "printf()" function
16353 */
16354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016355f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016356{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016357 char_u buf[NUMBUFLEN];
16358 int len;
16359 char_u *s;
16360 int saved_did_emsg = did_emsg;
16361 char *fmt;
16362
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016363 rettv->v_type = VAR_STRING;
16364 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016365
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016366 /* Get the required length, allocate the buffer and do it for real. */
16367 did_emsg = FALSE;
16368 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16369 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16370 if (!did_emsg)
16371 {
16372 s = alloc(len + 1);
16373 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016374 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016375 rettv->vval.v_string = s;
16376 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016377 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016378 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016379 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016380}
16381
16382/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016383 * "pumvisible()" function
16384 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016386f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016387{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016388#ifdef FEAT_INS_EXPAND
16389 if (pum_visible())
16390 rettv->vval.v_number = 1;
16391#endif
16392}
16393
Bram Moolenaardb913952012-06-29 12:54:53 +020016394#ifdef FEAT_PYTHON3
16395/*
16396 * "py3eval()" function
16397 */
16398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016399f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016400{
16401 char_u *str;
16402 char_u buf[NUMBUFLEN];
16403
16404 str = get_tv_string_buf(&argvars[0], buf);
16405 do_py3eval(str, rettv);
16406}
16407#endif
16408
16409#ifdef FEAT_PYTHON
16410/*
16411 * "pyeval()" function
16412 */
16413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016414f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016415{
16416 char_u *str;
16417 char_u buf[NUMBUFLEN];
16418
16419 str = get_tv_string_buf(&argvars[0], buf);
16420 do_pyeval(str, rettv);
16421}
16422#endif
16423
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016424/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016425 * "range()" function
16426 */
16427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016428f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016429{
16430 long start;
16431 long end;
16432 long stride = 1;
16433 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016434 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016436 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016437 if (argvars[1].v_type == VAR_UNKNOWN)
16438 {
16439 end = start - 1;
16440 start = 0;
16441 }
16442 else
16443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016444 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016446 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016447 }
16448
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016449 if (error)
16450 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016451 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016452 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016453 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016454 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016455 else
16456 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016457 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016458 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016459 if (list_append_number(rettv->vval.v_list,
16460 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016461 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016462 }
16463}
16464
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016465/*
16466 * "readfile()" function
16467 */
16468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016470{
16471 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016472 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016473 char_u *fname;
16474 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016475 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16476 int io_size = sizeof(buf);
16477 int readlen; /* size of last fread() */
16478 char_u *prev = NULL; /* previously read bytes, if any */
16479 long prevlen = 0; /* length of data in prev */
16480 long prevsize = 0; /* size of prev buffer */
16481 long maxline = MAXLNUM;
16482 long cnt = 0;
16483 char_u *p; /* position in buf */
16484 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016485
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016486 if (argvars[1].v_type != VAR_UNKNOWN)
16487 {
16488 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16489 binary = TRUE;
16490 if (argvars[2].v_type != VAR_UNKNOWN)
16491 maxline = get_tv_number(&argvars[2]);
16492 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016493
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016494 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016495 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016496
16497 /* Always open the file in binary mode, library functions have a mind of
16498 * their own about CR-LF conversion. */
16499 fname = get_tv_string(&argvars[0]);
16500 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16501 {
16502 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16503 return;
16504 }
16505
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016506 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016507 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016508 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016509
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016510 /* This for loop processes what was read, but is also entered at end
16511 * of file so that either:
16512 * - an incomplete line gets written
16513 * - a "binary" file gets an empty line at the end if it ends in a
16514 * newline. */
16515 for (p = buf, start = buf;
16516 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16517 ++p)
16518 {
16519 if (*p == '\n' || readlen <= 0)
16520 {
16521 listitem_T *li;
16522 char_u *s = NULL;
16523 long_u len = p - start;
16524
16525 /* Finished a line. Remove CRs before NL. */
16526 if (readlen > 0 && !binary)
16527 {
16528 while (len > 0 && start[len - 1] == '\r')
16529 --len;
16530 /* removal may cross back to the "prev" string */
16531 if (len == 0)
16532 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16533 --prevlen;
16534 }
16535 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016536 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016537 else
16538 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016539 /* Change "prev" buffer to be the right size. This way
16540 * the bytes are only copied once, and very long lines are
16541 * allocated only once. */
16542 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016543 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016544 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016545 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016546 prev = NULL; /* the list will own the string */
16547 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548 }
16549 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016550 if (s == NULL)
16551 {
16552 do_outofmem_msg((long_u) prevlen + len + 1);
16553 failed = TRUE;
16554 break;
16555 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016557 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016558 {
16559 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016560 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016561 break;
16562 }
16563 li->li_tv.v_type = VAR_STRING;
16564 li->li_tv.v_lock = 0;
16565 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016566 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016567
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016568 start = p + 1; /* step over newline */
16569 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016570 break;
16571 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016572 else if (*p == NUL)
16573 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016574#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016575 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16576 * when finding the BF and check the previous two bytes. */
16577 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016578 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016579 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16580 * + 1, these may be in the "prev" string. */
16581 char_u back1 = p >= buf + 1 ? p[-1]
16582 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16583 char_u back2 = p >= buf + 2 ? p[-2]
16584 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16585 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016586
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016587 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016588 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016589 char_u *dest = p - 2;
16590
16591 /* Usually a BOM is at the beginning of a file, and so at
16592 * the beginning of a line; then we can just step over it.
16593 */
16594 if (start == dest)
16595 start = p + 1;
16596 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016597 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016598 /* have to shuffle buf to close gap */
16599 int adjust_prevlen = 0;
16600
16601 if (dest < buf)
16602 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016603 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016604 dest = buf;
16605 }
16606 if (readlen > p - buf + 1)
16607 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16608 readlen -= 3 - adjust_prevlen;
16609 prevlen -= adjust_prevlen;
16610 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016611 }
16612 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016613 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016614#endif
16615 } /* for */
16616
16617 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16618 break;
16619 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016620 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016621 /* There's part of a line in buf, store it in "prev". */
16622 if (p - start + prevlen >= prevsize)
16623 {
16624 /* need bigger "prev" buffer */
16625 char_u *newprev;
16626
16627 /* A common use case is ordinary text files and "prev" gets a
16628 * fragment of a line, so the first allocation is made
16629 * small, to avoid repeatedly 'allocing' large and
16630 * 'reallocing' small. */
16631 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016632 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016633 else
16634 {
16635 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016636 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016637 prevsize = grow50pc > growmin ? grow50pc : growmin;
16638 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016639 newprev = prev == NULL ? alloc(prevsize)
16640 : vim_realloc(prev, prevsize);
16641 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016642 {
16643 do_outofmem_msg((long_u)prevsize);
16644 failed = TRUE;
16645 break;
16646 }
16647 prev = newprev;
16648 }
16649 /* Add the line part to end of "prev". */
16650 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016651 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016652 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016653 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016654
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016655 /*
16656 * For a negative line count use only the lines at the end of the file,
16657 * free the rest.
16658 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016659 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016660 while (cnt > -maxline)
16661 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016662 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016663 --cnt;
16664 }
16665
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016666 if (failed)
16667 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016668 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016669 /* readfile doc says an empty list is returned on error */
16670 rettv->vval.v_list = list_alloc();
16671 }
16672
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016673 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016674 fclose(fd);
16675}
16676
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016677#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016678static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016679
16680/*
16681 * Convert a List to proftime_T.
16682 * Return FAIL when there is something wrong.
16683 */
16684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016685list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016686{
16687 long n1, n2;
16688 int error = FALSE;
16689
16690 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16691 || arg->vval.v_list->lv_len != 2)
16692 return FAIL;
16693 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16694 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16695# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016696 tm->HighPart = n1;
16697 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016698# else
16699 tm->tv_sec = n1;
16700 tm->tv_usec = n2;
16701# endif
16702 return error ? FAIL : OK;
16703}
16704#endif /* FEAT_RELTIME */
16705
16706/*
16707 * "reltime()" function
16708 */
16709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016710f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016711{
16712#ifdef FEAT_RELTIME
16713 proftime_T res;
16714 proftime_T start;
16715
16716 if (argvars[0].v_type == VAR_UNKNOWN)
16717 {
16718 /* No arguments: get current time. */
16719 profile_start(&res);
16720 }
16721 else if (argvars[1].v_type == VAR_UNKNOWN)
16722 {
16723 if (list2proftime(&argvars[0], &res) == FAIL)
16724 return;
16725 profile_end(&res);
16726 }
16727 else
16728 {
16729 /* Two arguments: compute the difference. */
16730 if (list2proftime(&argvars[0], &start) == FAIL
16731 || list2proftime(&argvars[1], &res) == FAIL)
16732 return;
16733 profile_sub(&res, &start);
16734 }
16735
16736 if (rettv_list_alloc(rettv) == OK)
16737 {
16738 long n1, n2;
16739
16740# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016741 n1 = res.HighPart;
16742 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016743# else
16744 n1 = res.tv_sec;
16745 n2 = res.tv_usec;
16746# endif
16747 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16748 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16749 }
16750#endif
16751}
16752
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016753#ifdef FEAT_FLOAT
16754/*
16755 * "reltimefloat()" function
16756 */
16757 static void
16758f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16759{
16760# ifdef FEAT_RELTIME
16761 proftime_T tm;
16762# endif
16763
16764 rettv->v_type = VAR_FLOAT;
16765 rettv->vval.v_float = 0;
16766# ifdef FEAT_RELTIME
16767 if (list2proftime(&argvars[0], &tm) == OK)
16768 rettv->vval.v_float = profile_float(&tm);
16769# endif
16770}
16771#endif
16772
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016773/*
16774 * "reltimestr()" function
16775 */
16776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016777f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016778{
16779#ifdef FEAT_RELTIME
16780 proftime_T tm;
16781#endif
16782
16783 rettv->v_type = VAR_STRING;
16784 rettv->vval.v_string = NULL;
16785#ifdef FEAT_RELTIME
16786 if (list2proftime(&argvars[0], &tm) == OK)
16787 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16788#endif
16789}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016790
Bram Moolenaar0d660222005-01-07 21:51:51 +000016791#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016792static void make_connection(void);
16793static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016794
16795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016796make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016797{
16798 if (X_DISPLAY == NULL
16799# ifdef FEAT_GUI
16800 && !gui.in_use
16801# endif
16802 )
16803 {
16804 x_force_connect = TRUE;
16805 setup_term_clip();
16806 x_force_connect = FALSE;
16807 }
16808}
16809
16810 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016811check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016812{
16813 make_connection();
16814 if (X_DISPLAY == NULL)
16815 {
16816 EMSG(_("E240: No connection to Vim server"));
16817 return FAIL;
16818 }
16819 return OK;
16820}
16821#endif
16822
16823#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016824static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016825
16826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016827remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016828{
16829 char_u *server_name;
16830 char_u *keys;
16831 char_u *r = NULL;
16832 char_u buf[NUMBUFLEN];
16833# ifdef WIN32
16834 HWND w;
16835# else
16836 Window w;
16837# endif
16838
16839 if (check_restricted() || check_secure())
16840 return;
16841
16842# ifdef FEAT_X11
16843 if (check_connection() == FAIL)
16844 return;
16845# endif
16846
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016847 server_name = get_tv_string_chk(&argvars[0]);
16848 if (server_name == NULL)
16849 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016850 keys = get_tv_string_buf(&argvars[1], buf);
16851# ifdef WIN32
16852 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16853# else
16854 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16855 < 0)
16856# endif
16857 {
16858 if (r != NULL)
16859 EMSG(r); /* sending worked but evaluation failed */
16860 else
16861 EMSG2(_("E241: Unable to send to %s"), server_name);
16862 return;
16863 }
16864
16865 rettv->vval.v_string = r;
16866
16867 if (argvars[2].v_type != VAR_UNKNOWN)
16868 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016869 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016870 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016871 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016872
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016873 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 v.di_tv.v_type = VAR_STRING;
16875 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016876 idvar = get_tv_string_chk(&argvars[2]);
16877 if (idvar != NULL)
16878 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016879 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880 }
16881}
16882#endif
16883
16884/*
16885 * "remote_expr()" function
16886 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016888f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016889{
16890 rettv->v_type = VAR_STRING;
16891 rettv->vval.v_string = NULL;
16892#ifdef FEAT_CLIENTSERVER
16893 remote_common(argvars, rettv, TRUE);
16894#endif
16895}
16896
16897/*
16898 * "remote_foreground()" function
16899 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016901f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016902{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016903#ifdef FEAT_CLIENTSERVER
16904# ifdef WIN32
16905 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016906 {
16907 char_u *server_name = get_tv_string_chk(&argvars[0]);
16908
16909 if (server_name != NULL)
16910 serverForeground(server_name);
16911 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912# else
16913 /* Send a foreground() expression to the server. */
16914 argvars[1].v_type = VAR_STRING;
16915 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16916 argvars[2].v_type = VAR_UNKNOWN;
16917 remote_common(argvars, rettv, TRUE);
16918 vim_free(argvars[1].vval.v_string);
16919# endif
16920#endif
16921}
16922
Bram Moolenaar0d660222005-01-07 21:51:51 +000016923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016924f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016925{
16926#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016927 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016928 char_u *s = NULL;
16929# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016930 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016931# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016932 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933
16934 if (check_restricted() || check_secure())
16935 {
16936 rettv->vval.v_number = -1;
16937 return;
16938 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016939 serverid = get_tv_string_chk(&argvars[0]);
16940 if (serverid == NULL)
16941 {
16942 rettv->vval.v_number = -1;
16943 return; /* type error; errmsg already given */
16944 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016945# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016946 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016947 if (n == 0)
16948 rettv->vval.v_number = -1;
16949 else
16950 {
16951 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16952 rettv->vval.v_number = (s != NULL);
16953 }
16954# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955 if (check_connection() == FAIL)
16956 return;
16957
16958 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016960# endif
16961
16962 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16963 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016964 char_u *retvar;
16965
Bram Moolenaar33570922005-01-25 22:26:29 +000016966 v.di_tv.v_type = VAR_STRING;
16967 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016968 retvar = get_tv_string_chk(&argvars[1]);
16969 if (retvar != NULL)
16970 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016971 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972 }
16973#else
16974 rettv->vval.v_number = -1;
16975#endif
16976}
16977
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980{
16981 char_u *r = NULL;
16982
16983#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016984 char_u *serverid = get_tv_string_chk(&argvars[0]);
16985
16986 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 {
16988# ifdef WIN32
16989 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016990 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016992 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993 if (n != 0)
16994 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16995 if (r == NULL)
16996# else
16997 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016998 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999# endif
17000 EMSG(_("E277: Unable to read a server reply"));
17001 }
17002#endif
17003 rettv->v_type = VAR_STRING;
17004 rettv->vval.v_string = r;
17005}
17006
17007/*
17008 * "remote_send()" function
17009 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017011f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012{
17013 rettv->v_type = VAR_STRING;
17014 rettv->vval.v_string = NULL;
17015#ifdef FEAT_CLIENTSERVER
17016 remote_common(argvars, rettv, FALSE);
17017#endif
17018}
17019
17020/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017021 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017022 */
17023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017024f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017025{
Bram Moolenaar33570922005-01-25 22:26:29 +000017026 list_T *l;
17027 listitem_T *item, *item2;
17028 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017029 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017030 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017031 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017032 dict_T *d;
17033 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017034 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017035
Bram Moolenaar8c711452005-01-14 21:53:12 +000017036 if (argvars[0].v_type == VAR_DICT)
17037 {
17038 if (argvars[2].v_type != VAR_UNKNOWN)
17039 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017040 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017041 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017043 key = get_tv_string_chk(&argvars[1]);
17044 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017046 di = dict_find(d, key, -1);
17047 if (di == NULL)
17048 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017049 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17050 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 {
17052 *rettv = di->di_tv;
17053 init_tv(&di->di_tv);
17054 dictitem_remove(d, di);
17055 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017056 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017057 }
17058 }
17059 else if (argvars[0].v_type != VAR_LIST)
17060 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017061 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017062 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017063 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 int error = FALSE;
17065
17066 idx = get_tv_number_chk(&argvars[1], &error);
17067 if (error)
17068 ; /* type error: do nothing, errmsg already given */
17069 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017070 EMSGN(_(e_listidx), idx);
17071 else
17072 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017073 if (argvars[2].v_type == VAR_UNKNOWN)
17074 {
17075 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017076 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017077 *rettv = item->li_tv;
17078 vim_free(item);
17079 }
17080 else
17081 {
17082 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017083 end = get_tv_number_chk(&argvars[2], &error);
17084 if (error)
17085 ; /* type error: do nothing */
17086 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017087 EMSGN(_(e_listidx), end);
17088 else
17089 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017090 int cnt = 0;
17091
17092 for (li = item; li != NULL; li = li->li_next)
17093 {
17094 ++cnt;
17095 if (li == item2)
17096 break;
17097 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017098 if (li == NULL) /* didn't find "item2" after "item" */
17099 EMSG(_(e_invrange));
17100 else
17101 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017102 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017103 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017104 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017105 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017106 l->lv_first = item;
17107 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017108 item->li_prev = NULL;
17109 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017110 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017111 }
17112 }
17113 }
17114 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115 }
17116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017117}
17118
17119/*
17120 * "rename({from}, {to})" function
17121 */
17122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017123f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017124{
17125 char_u buf[NUMBUFLEN];
17126
17127 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017128 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017130 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17131 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017132}
17133
17134/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017135 * "repeat()" function
17136 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017138f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017139{
17140 char_u *p;
17141 int n;
17142 int slen;
17143 int len;
17144 char_u *r;
17145 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017146
17147 n = get_tv_number(&argvars[1]);
17148 if (argvars[0].v_type == VAR_LIST)
17149 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017150 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017151 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017152 if (list_extend(rettv->vval.v_list,
17153 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017154 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017155 }
17156 else
17157 {
17158 p = get_tv_string(&argvars[0]);
17159 rettv->v_type = VAR_STRING;
17160 rettv->vval.v_string = NULL;
17161
17162 slen = (int)STRLEN(p);
17163 len = slen * n;
17164 if (len <= 0)
17165 return;
17166
17167 r = alloc(len + 1);
17168 if (r != NULL)
17169 {
17170 for (i = 0; i < n; i++)
17171 mch_memmove(r + i * slen, p, (size_t)slen);
17172 r[len] = NUL;
17173 }
17174
17175 rettv->vval.v_string = r;
17176 }
17177}
17178
17179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 * "resolve()" function
17181 */
17182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017183f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184{
17185 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017186#ifdef HAVE_READLINK
17187 char_u *buf = NULL;
17188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017189
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017190 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191#ifdef FEAT_SHORTCUT
17192 {
17193 char_u *v = NULL;
17194
17195 v = mch_resolve_shortcut(p);
17196 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017197 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200 }
17201#else
17202# ifdef HAVE_READLINK
17203 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017204 char_u *cpy;
17205 int len;
17206 char_u *remain = NULL;
17207 char_u *q;
17208 int is_relative_to_current = FALSE;
17209 int has_trailing_pathsep = FALSE;
17210 int limit = 100;
17211
17212 p = vim_strsave(p);
17213
17214 if (p[0] == '.' && (vim_ispathsep(p[1])
17215 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17216 is_relative_to_current = TRUE;
17217
17218 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017219 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017221 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017222 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224
17225 q = getnextcomp(p);
17226 if (*q != NUL)
17227 {
17228 /* Separate the first path component in "p", and keep the
17229 * remainder (beginning with the path separator). */
17230 remain = vim_strsave(q - 1);
17231 q[-1] = NUL;
17232 }
17233
Bram Moolenaard9462e32011-04-11 21:35:11 +020017234 buf = alloc(MAXPATHL + 1);
17235 if (buf == NULL)
17236 goto fail;
17237
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 for (;;)
17239 {
17240 for (;;)
17241 {
17242 len = readlink((char *)p, (char *)buf, MAXPATHL);
17243 if (len <= 0)
17244 break;
17245 buf[len] = NUL;
17246
17247 if (limit-- == 0)
17248 {
17249 vim_free(p);
17250 vim_free(remain);
17251 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017252 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017253 goto fail;
17254 }
17255
17256 /* Ensure that the result will have a trailing path separator
17257 * if the argument has one. */
17258 if (remain == NULL && has_trailing_pathsep)
17259 add_pathsep(buf);
17260
17261 /* Separate the first path component in the link value and
17262 * concatenate the remainders. */
17263 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17264 if (*q != NUL)
17265 {
17266 if (remain == NULL)
17267 remain = vim_strsave(q - 1);
17268 else
17269 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017270 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 if (cpy != NULL)
17272 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017273 vim_free(remain);
17274 remain = cpy;
17275 }
17276 }
17277 q[-1] = NUL;
17278 }
17279
17280 q = gettail(p);
17281 if (q > p && *q == NUL)
17282 {
17283 /* Ignore trailing path separator. */
17284 q[-1] = NUL;
17285 q = gettail(p);
17286 }
17287 if (q > p && !mch_isFullName(buf))
17288 {
17289 /* symlink is relative to directory of argument */
17290 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17291 if (cpy != NULL)
17292 {
17293 STRCPY(cpy, p);
17294 STRCPY(gettail(cpy), buf);
17295 vim_free(p);
17296 p = cpy;
17297 }
17298 }
17299 else
17300 {
17301 vim_free(p);
17302 p = vim_strsave(buf);
17303 }
17304 }
17305
17306 if (remain == NULL)
17307 break;
17308
17309 /* Append the first path component of "remain" to "p". */
17310 q = getnextcomp(remain + 1);
17311 len = q - remain - (*q != NUL);
17312 cpy = vim_strnsave(p, STRLEN(p) + len);
17313 if (cpy != NULL)
17314 {
17315 STRNCAT(cpy, remain, len);
17316 vim_free(p);
17317 p = cpy;
17318 }
17319 /* Shorten "remain". */
17320 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017321 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017322 else
17323 {
17324 vim_free(remain);
17325 remain = NULL;
17326 }
17327 }
17328
17329 /* If the result is a relative path name, make it explicitly relative to
17330 * the current directory if and only if the argument had this form. */
17331 if (!vim_ispathsep(*p))
17332 {
17333 if (is_relative_to_current
17334 && *p != NUL
17335 && !(p[0] == '.'
17336 && (p[1] == NUL
17337 || vim_ispathsep(p[1])
17338 || (p[1] == '.'
17339 && (p[2] == NUL
17340 || vim_ispathsep(p[2]))))))
17341 {
17342 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017343 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017344 if (cpy != NULL)
17345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346 vim_free(p);
17347 p = cpy;
17348 }
17349 }
17350 else if (!is_relative_to_current)
17351 {
17352 /* Strip leading "./". */
17353 q = p;
17354 while (q[0] == '.' && vim_ispathsep(q[1]))
17355 q += 2;
17356 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017357 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017358 }
17359 }
17360
17361 /* Ensure that the result will have no trailing path separator
17362 * if the argument had none. But keep "/" or "//". */
17363 if (!has_trailing_pathsep)
17364 {
17365 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017366 if (after_pathsep(p, q))
17367 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 }
17369
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017370 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 }
17372# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017373 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374# endif
17375#endif
17376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017377 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017378
17379#ifdef HAVE_READLINK
17380fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017381 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017383 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384}
17385
17386/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017387 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 */
17389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017390f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391{
Bram Moolenaar33570922005-01-25 22:26:29 +000017392 list_T *l;
17393 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394
Bram Moolenaar0d660222005-01-07 21:51:51 +000017395 if (argvars[0].v_type != VAR_LIST)
17396 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017397 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017398 && !tv_check_lock(l->lv_lock,
17399 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017400 {
17401 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017402 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017403 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017404 while (li != NULL)
17405 {
17406 ni = li->li_prev;
17407 list_append(l, li);
17408 li = ni;
17409 }
17410 rettv->vval.v_list = l;
17411 rettv->v_type = VAR_LIST;
17412 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017413 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415}
17416
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017417#define SP_NOMOVE 0x01 /* don't move cursor */
17418#define SP_REPEAT 0x02 /* repeat to find outer pair */
17419#define SP_RETCOUNT 0x04 /* return matchcount */
17420#define SP_SETPCMARK 0x08 /* set previous context mark */
17421#define SP_START 0x10 /* accept match at start position */
17422#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17423#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017424#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017425
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017426static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017427
17428/*
17429 * Get flags for a search function.
17430 * Possibly sets "p_ws".
17431 * Returns BACKWARD, FORWARD or zero (for an error).
17432 */
17433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017434get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017435{
17436 int dir = FORWARD;
17437 char_u *flags;
17438 char_u nbuf[NUMBUFLEN];
17439 int mask;
17440
17441 if (varp->v_type != VAR_UNKNOWN)
17442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017443 flags = get_tv_string_buf_chk(varp, nbuf);
17444 if (flags == NULL)
17445 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017446 while (*flags != NUL)
17447 {
17448 switch (*flags)
17449 {
17450 case 'b': dir = BACKWARD; break;
17451 case 'w': p_ws = TRUE; break;
17452 case 'W': p_ws = FALSE; break;
17453 default: mask = 0;
17454 if (flagsp != NULL)
17455 switch (*flags)
17456 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017457 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017458 case 'e': mask = SP_END; break;
17459 case 'm': mask = SP_RETCOUNT; break;
17460 case 'n': mask = SP_NOMOVE; break;
17461 case 'p': mask = SP_SUBPAT; break;
17462 case 'r': mask = SP_REPEAT; break;
17463 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017464 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017465 }
17466 if (mask == 0)
17467 {
17468 EMSG2(_(e_invarg2), flags);
17469 dir = 0;
17470 }
17471 else
17472 *flagsp |= mask;
17473 }
17474 if (dir == 0)
17475 break;
17476 ++flags;
17477 }
17478 }
17479 return dir;
17480}
17481
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017483 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017485 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017486search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017488 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017489 char_u *pat;
17490 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017491 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 int save_p_ws = p_ws;
17493 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017494 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017495 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017496 proftime_T tm;
17497#ifdef FEAT_RELTIME
17498 long time_limit = 0;
17499#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017500 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017501 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017503 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017504 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017505 if (dir == 0)
17506 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017507 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017508 if (flags & SP_START)
17509 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017510 if (flags & SP_END)
17511 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017512 if (flags & SP_COLUMN)
17513 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017514
Bram Moolenaar76929292008-01-06 19:07:36 +000017515 /* Optional arguments: line number to stop searching and timeout. */
17516 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017517 {
17518 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17519 if (lnum_stop < 0)
17520 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017521#ifdef FEAT_RELTIME
17522 if (argvars[3].v_type != VAR_UNKNOWN)
17523 {
17524 time_limit = get_tv_number_chk(&argvars[3], NULL);
17525 if (time_limit < 0)
17526 goto theend;
17527 }
17528#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017529 }
17530
Bram Moolenaar76929292008-01-06 19:07:36 +000017531#ifdef FEAT_RELTIME
17532 /* Set the time limit, if there is one. */
17533 profile_setlimit(time_limit, &tm);
17534#endif
17535
Bram Moolenaar231334e2005-07-25 20:46:57 +000017536 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017537 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017538 * Check to make sure only those flags are set.
17539 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17540 * flags cannot be set. Check for that condition also.
17541 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017542 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017543 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017546 goto theend;
17547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017549 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017550 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017551 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017552 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017553 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017554 if (flags & SP_SUBPAT)
17555 retval = subpatnum;
17556 else
17557 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017558 if (flags & SP_SETPCMARK)
17559 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017561 if (match_pos != NULL)
17562 {
17563 /* Store the match cursor position */
17564 match_pos->lnum = pos.lnum;
17565 match_pos->col = pos.col + 1;
17566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 /* "/$" will put the cursor after the end of the line, may need to
17568 * correct that here */
17569 check_cursor();
17570 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017571
17572 /* If 'n' flag is used: restore cursor position. */
17573 if (flags & SP_NOMOVE)
17574 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017575 else
17576 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017577theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017579
17580 return retval;
17581}
17582
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017583#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017584
17585/*
17586 * round() is not in C90, use ceil() or floor() instead.
17587 */
17588 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017589vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017590{
17591 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17592}
17593
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017594/*
17595 * "round({float})" function
17596 */
17597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017598f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017599{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017600 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017601
17602 rettv->v_type = VAR_FLOAT;
17603 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017604 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017605 else
17606 rettv->vval.v_float = 0.0;
17607}
17608#endif
17609
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017610/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017611 * "screenattr()" function
17612 */
17613 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017614f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017615{
17616 int row;
17617 int col;
17618 int c;
17619
17620 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17621 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17622 if (row < 0 || row >= screen_Rows
17623 || col < 0 || col >= screen_Columns)
17624 c = -1;
17625 else
17626 c = ScreenAttrs[LineOffset[row] + col];
17627 rettv->vval.v_number = c;
17628}
17629
17630/*
17631 * "screenchar()" function
17632 */
17633 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017634f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017635{
17636 int row;
17637 int col;
17638 int off;
17639 int c;
17640
17641 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17642 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17643 if (row < 0 || row >= screen_Rows
17644 || col < 0 || col >= screen_Columns)
17645 c = -1;
17646 else
17647 {
17648 off = LineOffset[row] + col;
17649#ifdef FEAT_MBYTE
17650 if (enc_utf8 && ScreenLinesUC[off] != 0)
17651 c = ScreenLinesUC[off];
17652 else
17653#endif
17654 c = ScreenLines[off];
17655 }
17656 rettv->vval.v_number = c;
17657}
17658
17659/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017660 * "screencol()" function
17661 *
17662 * First column is 1 to be consistent with virtcol().
17663 */
17664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017665f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017666{
17667 rettv->vval.v_number = screen_screencol() + 1;
17668}
17669
17670/*
17671 * "screenrow()" function
17672 */
17673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017674f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017675{
17676 rettv->vval.v_number = screen_screenrow() + 1;
17677}
17678
17679/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017680 * "search()" function
17681 */
17682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017683f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017684{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017685 int flags = 0;
17686
17687 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688}
17689
Bram Moolenaar071d4272004-06-13 20:20:40 +000017690/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017691 * "searchdecl()" function
17692 */
17693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017694f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017695{
17696 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017697 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017698 int error = FALSE;
17699 char_u *name;
17700
17701 rettv->vval.v_number = 1; /* default: FAIL */
17702
17703 name = get_tv_string_chk(&argvars[0]);
17704 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017705 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017706 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017707 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17708 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17709 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017710 if (!error && name != NULL)
17711 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017712 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017713}
17714
17715/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017716 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017719searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720{
17721 char_u *spat, *mpat, *epat;
17722 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 int dir;
17725 int flags = 0;
17726 char_u nbuf1[NUMBUFLEN];
17727 char_u nbuf2[NUMBUFLEN];
17728 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017730 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017731 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017732
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017734 spat = get_tv_string_chk(&argvars[0]);
17735 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17736 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17737 if (spat == NULL || mpat == NULL || epat == NULL)
17738 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017739
Bram Moolenaar071d4272004-06-13 20:20:40 +000017740 /* Handle the optional fourth argument: flags */
17741 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017742 if (dir == 0)
17743 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017744
17745 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017746 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17747 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017748 if ((flags & (SP_END | SP_SUBPAT)) != 0
17749 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017750 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017751 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017752 goto theend;
17753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017755 /* Using 'r' implies 'W', otherwise it doesn't work. */
17756 if (flags & SP_REPEAT)
17757 p_ws = FALSE;
17758
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017759 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017760 if (argvars[3].v_type == VAR_UNKNOWN
17761 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 skip = (char_u *)"";
17763 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017764 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017765 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017766 if (argvars[5].v_type != VAR_UNKNOWN)
17767 {
17768 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17769 if (lnum_stop < 0)
17770 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017771#ifdef FEAT_RELTIME
17772 if (argvars[6].v_type != VAR_UNKNOWN)
17773 {
17774 time_limit = get_tv_number_chk(&argvars[6], NULL);
17775 if (time_limit < 0)
17776 goto theend;
17777 }
17778#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017779 }
17780 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017781 if (skip == NULL)
17782 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017784 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017785 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017786
17787theend:
17788 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017789
17790 return retval;
17791}
17792
17793/*
17794 * "searchpair()" function
17795 */
17796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017797f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017798{
17799 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17800}
17801
17802/*
17803 * "searchpairpos()" function
17804 */
17805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017806f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017807{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017808 pos_T match_pos;
17809 int lnum = 0;
17810 int col = 0;
17811
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017812 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017813 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017814
17815 if (searchpair_cmn(argvars, &match_pos) > 0)
17816 {
17817 lnum = match_pos.lnum;
17818 col = match_pos.col;
17819 }
17820
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017821 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17822 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017823}
17824
17825/*
17826 * Search for a start/middle/end thing.
17827 * Used by searchpair(), see its documentation for the details.
17828 * Returns 0 or -1 for no match,
17829 */
17830 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017831do_searchpair(
17832 char_u *spat, /* start pattern */
17833 char_u *mpat, /* middle pattern */
17834 char_u *epat, /* end pattern */
17835 int dir, /* BACKWARD or FORWARD */
17836 char_u *skip, /* skip expression */
17837 int flags, /* SP_SETPCMARK and other SP_ values */
17838 pos_T *match_pos,
17839 linenr_T lnum_stop, /* stop at this line if not zero */
17840 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017841{
17842 char_u *save_cpo;
17843 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17844 long retval = 0;
17845 pos_T pos;
17846 pos_T firstpos;
17847 pos_T foundpos;
17848 pos_T save_cursor;
17849 pos_T save_pos;
17850 int n;
17851 int r;
17852 int nest = 1;
17853 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017854 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017855 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017856
17857 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17858 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017859 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017860
Bram Moolenaar76929292008-01-06 19:07:36 +000017861#ifdef FEAT_RELTIME
17862 /* Set the time limit, if there is one. */
17863 profile_setlimit(time_limit, &tm);
17864#endif
17865
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017866 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17867 * start/middle/end (pat3, for the top pair). */
17868 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17869 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17870 if (pat2 == NULL || pat3 == NULL)
17871 goto theend;
17872 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17873 if (*mpat == NUL)
17874 STRCPY(pat3, pat2);
17875 else
17876 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17877 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017878 if (flags & SP_START)
17879 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017880
Bram Moolenaar071d4272004-06-13 20:20:40 +000017881 save_cursor = curwin->w_cursor;
17882 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017883 clearpos(&firstpos);
17884 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 pat = pat3;
17886 for (;;)
17887 {
17888 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017889 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17891 /* didn't find it or found the first match again: FAIL */
17892 break;
17893
17894 if (firstpos.lnum == 0)
17895 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017896 if (equalpos(pos, foundpos))
17897 {
17898 /* Found the same position again. Can happen with a pattern that
17899 * has "\zs" at the end and searching backwards. Advance one
17900 * character and try again. */
17901 if (dir == BACKWARD)
17902 decl(&pos);
17903 else
17904 incl(&pos);
17905 }
17906 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017908 /* clear the start flag to avoid getting stuck here */
17909 options &= ~SEARCH_START;
17910
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911 /* If the skip pattern matches, ignore this match. */
17912 if (*skip != NUL)
17913 {
17914 save_pos = curwin->w_cursor;
17915 curwin->w_cursor = pos;
17916 r = eval_to_bool(skip, &err, NULL, FALSE);
17917 curwin->w_cursor = save_pos;
17918 if (err)
17919 {
17920 /* Evaluating {skip} caused an error, break here. */
17921 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017922 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923 break;
17924 }
17925 if (r)
17926 continue;
17927 }
17928
17929 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17930 {
17931 /* Found end when searching backwards or start when searching
17932 * forward: nested pair. */
17933 ++nest;
17934 pat = pat2; /* nested, don't search for middle */
17935 }
17936 else
17937 {
17938 /* Found end when searching forward or start when searching
17939 * backward: end of (nested) pair; or found middle in outer pair. */
17940 if (--nest == 1)
17941 pat = pat3; /* outer level, search for middle */
17942 }
17943
17944 if (nest == 0)
17945 {
17946 /* Found the match: return matchcount or line number. */
17947 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017948 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017950 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017951 if (flags & SP_SETPCMARK)
17952 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 curwin->w_cursor = pos;
17954 if (!(flags & SP_REPEAT))
17955 break;
17956 nest = 1; /* search for next unmatched */
17957 }
17958 }
17959
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017960 if (match_pos != NULL)
17961 {
17962 /* Store the match cursor position */
17963 match_pos->lnum = curwin->w_cursor.lnum;
17964 match_pos->col = curwin->w_cursor.col + 1;
17965 }
17966
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017968 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 curwin->w_cursor = save_cursor;
17970
17971theend:
17972 vim_free(pat2);
17973 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017974 if (p_cpo == empty_option)
17975 p_cpo = save_cpo;
17976 else
17977 /* Darn, evaluating the {skip} expression changed the value. */
17978 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017979
17980 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017981}
17982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017983/*
17984 * "searchpos()" function
17985 */
17986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017987f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017988{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017989 pos_T match_pos;
17990 int lnum = 0;
17991 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017992 int n;
17993 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017994
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017995 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017996 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017997
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017998 n = search_cmn(argvars, &match_pos, &flags);
17999 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018000 {
18001 lnum = match_pos.lnum;
18002 col = match_pos.col;
18003 }
18004
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018005 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18006 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018007 if (flags & SP_SUBPAT)
18008 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018009}
18010
Bram Moolenaar0d660222005-01-07 21:51:51 +000018011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018012f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018014#ifdef FEAT_CLIENTSERVER
18015 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018016 char_u *server = get_tv_string_chk(&argvars[0]);
18017 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018018
Bram Moolenaar0d660222005-01-07 21:51:51 +000018019 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018020 if (server == NULL || reply == NULL)
18021 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018022 if (check_restricted() || check_secure())
18023 return;
18024# ifdef FEAT_X11
18025 if (check_connection() == FAIL)
18026 return;
18027# endif
18028
18029 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018031 EMSG(_("E258: Unable to send to client"));
18032 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018034 rettv->vval.v_number = 0;
18035#else
18036 rettv->vval.v_number = -1;
18037#endif
18038}
18039
Bram Moolenaar0d660222005-01-07 21:51:51 +000018040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018041f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018042{
18043 char_u *r = NULL;
18044
18045#ifdef FEAT_CLIENTSERVER
18046# ifdef WIN32
18047 r = serverGetVimNames();
18048# else
18049 make_connection();
18050 if (X_DISPLAY != NULL)
18051 r = serverGetVimNames(X_DISPLAY);
18052# endif
18053#endif
18054 rettv->v_type = VAR_STRING;
18055 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056}
18057
18058/*
18059 * "setbufvar()" function
18060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018062f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063{
18064 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018066 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018067 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 char_u nbuf[NUMBUFLEN];
18069
18070 if (check_restricted() || check_secure())
18071 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018072 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18073 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018074 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075 varp = &argvars[2];
18076
18077 if (buf != NULL && varname != NULL && varp != NULL)
18078 {
18079 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018080 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018081
18082 if (*varname == '&')
18083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018084 long numval;
18085 char_u *strval;
18086 int error = FALSE;
18087
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018089 numval = get_tv_number_chk(varp, &error);
18090 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018091 if (!error && strval != NULL)
18092 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018093 }
18094 else
18095 {
18096 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18097 if (bufvarname != NULL)
18098 {
18099 STRCPY(bufvarname, "b:");
18100 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018101 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102 vim_free(bufvarname);
18103 }
18104 }
18105
18106 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109}
18110
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018112f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018113{
18114 dict_T *d;
18115 dictitem_T *di;
18116 char_u *csearch;
18117
18118 if (argvars[0].v_type != VAR_DICT)
18119 {
18120 EMSG(_(e_dictreq));
18121 return;
18122 }
18123
18124 if ((d = argvars[0].vval.v_dict) != NULL)
18125 {
18126 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18127 if (csearch != NULL)
18128 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018129#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018130 if (enc_utf8)
18131 {
18132 int pcc[MAX_MCO];
18133 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018134
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018135 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18136 }
18137 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018138#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018139 set_last_csearch(PTR2CHAR(csearch),
18140 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018141 }
18142
18143 di = dict_find(d, (char_u *)"forward", -1);
18144 if (di != NULL)
18145 set_csearch_direction(get_tv_number(&di->di_tv)
18146 ? FORWARD : BACKWARD);
18147
18148 di = dict_find(d, (char_u *)"until", -1);
18149 if (di != NULL)
18150 set_csearch_until(!!get_tv_number(&di->di_tv));
18151 }
18152}
18153
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154/*
18155 * "setcmdpos()" function
18156 */
18157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018158f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018159{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018160 int pos = (int)get_tv_number(&argvars[0]) - 1;
18161
18162 if (pos >= 0)
18163 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164}
18165
18166/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018167 * "setfperm({fname}, {mode})" function
18168 */
18169 static void
18170f_setfperm(typval_T *argvars, typval_T *rettv)
18171{
18172 char_u *fname;
18173 char_u modebuf[NUMBUFLEN];
18174 char_u *mode_str;
18175 int i;
18176 int mask;
18177 int mode = 0;
18178
18179 rettv->vval.v_number = 0;
18180 fname = get_tv_string_chk(&argvars[0]);
18181 if (fname == NULL)
18182 return;
18183 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18184 if (mode_str == NULL)
18185 return;
18186 if (STRLEN(mode_str) != 9)
18187 {
18188 EMSG2(_(e_invarg2), mode_str);
18189 return;
18190 }
18191
18192 mask = 1;
18193 for (i = 8; i >= 0; --i)
18194 {
18195 if (mode_str[i] != '-')
18196 mode |= mask;
18197 mask = mask << 1;
18198 }
18199 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18200}
18201
18202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018203 * "setline()" function
18204 */
18205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018206f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207{
18208 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018209 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018210 list_T *l = NULL;
18211 listitem_T *li = NULL;
18212 long added = 0;
18213 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018215 lnum = get_tv_lnum(&argvars[0]);
18216 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018218 l = argvars[1].vval.v_list;
18219 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018221 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018222 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018223
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018224 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018225 for (;;)
18226 {
18227 if (l != NULL)
18228 {
18229 /* list argument, get next string */
18230 if (li == NULL)
18231 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018232 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018233 li = li->li_next;
18234 }
18235
18236 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018238 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018239
18240 /* When coming here from Insert mode, sync undo, so that this can be
18241 * undone separately from what was previously inserted. */
18242 if (u_sync_once == 2)
18243 {
18244 u_sync_once = 1; /* notify that u_sync() was called */
18245 u_sync(TRUE);
18246 }
18247
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018248 if (lnum <= curbuf->b_ml.ml_line_count)
18249 {
18250 /* existing line, replace it */
18251 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18252 {
18253 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018254 if (lnum == curwin->w_cursor.lnum)
18255 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018256 rettv->vval.v_number = 0; /* OK */
18257 }
18258 }
18259 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18260 {
18261 /* lnum is one past the last line, append the line */
18262 ++added;
18263 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18264 rettv->vval.v_number = 0; /* OK */
18265 }
18266
18267 if (l == NULL) /* only one string argument */
18268 break;
18269 ++lnum;
18270 }
18271
18272 if (added > 0)
18273 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274}
18275
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018276static 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 +000018277
Bram Moolenaar071d4272004-06-13 20:20:40 +000018278/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018279 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018280 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018282set_qf_ll_list(
18283 win_T *wp UNUSED,
18284 typval_T *list_arg UNUSED,
18285 typval_T *action_arg UNUSED,
18286 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018287{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018288#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018289 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018290 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018291 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018292#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018293
Bram Moolenaar2641f772005-03-25 21:58:17 +000018294 rettv->vval.v_number = -1;
18295
18296#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018297 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018298 EMSG(_(e_listreq));
18299 else
18300 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018301 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018302
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018303 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018304 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018305 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018306 if (act == NULL)
18307 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018308 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018309 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018310 else
18311 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018312 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018313 else if (action_arg->v_type == VAR_UNKNOWN)
18314 action = ' ';
18315 else
18316 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018317
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018318 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018319 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018320 rettv->vval.v_number = 0;
18321 }
18322#endif
18323}
18324
18325/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018326 * "setloclist()" function
18327 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018329f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018330{
18331 win_T *win;
18332
18333 rettv->vval.v_number = -1;
18334
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018335 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018336 if (win != NULL)
18337 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18338}
18339
18340/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018341 * "setmatches()" function
18342 */
18343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018344f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018345{
18346#ifdef FEAT_SEARCH_EXTRA
18347 list_T *l;
18348 listitem_T *li;
18349 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018350 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018351
18352 rettv->vval.v_number = -1;
18353 if (argvars[0].v_type != VAR_LIST)
18354 {
18355 EMSG(_(e_listreq));
18356 return;
18357 }
18358 if ((l = argvars[0].vval.v_list) != NULL)
18359 {
18360
18361 /* To some extent make sure that we are dealing with a list from
18362 * "getmatches()". */
18363 li = l->lv_first;
18364 while (li != NULL)
18365 {
18366 if (li->li_tv.v_type != VAR_DICT
18367 || (d = li->li_tv.vval.v_dict) == NULL)
18368 {
18369 EMSG(_(e_invarg));
18370 return;
18371 }
18372 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018373 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18374 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018375 && dict_find(d, (char_u *)"priority", -1) != NULL
18376 && dict_find(d, (char_u *)"id", -1) != NULL))
18377 {
18378 EMSG(_(e_invarg));
18379 return;
18380 }
18381 li = li->li_next;
18382 }
18383
18384 clear_matches(curwin);
18385 li = l->lv_first;
18386 while (li != NULL)
18387 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018388 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018389 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018390 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018391 char_u *group;
18392 int priority;
18393 int id;
18394 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018395
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018396 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018397 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18398 {
18399 if (s == NULL)
18400 {
18401 s = list_alloc();
18402 if (s == NULL)
18403 return;
18404 }
18405
18406 /* match from matchaddpos() */
18407 for (i = 1; i < 9; i++)
18408 {
18409 sprintf((char *)buf, (char *)"pos%d", i);
18410 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18411 {
18412 if (di->di_tv.v_type != VAR_LIST)
18413 return;
18414
18415 list_append_tv(s, &di->di_tv);
18416 s->lv_refcount++;
18417 }
18418 else
18419 break;
18420 }
18421 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018422
18423 group = get_dict_string(d, (char_u *)"group", FALSE);
18424 priority = (int)get_dict_number(d, (char_u *)"priority");
18425 id = (int)get_dict_number(d, (char_u *)"id");
18426 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18427 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18428 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018429 if (i == 0)
18430 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018431 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018432 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018433 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018434 }
18435 else
18436 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018437 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018438 list_unref(s);
18439 s = NULL;
18440 }
18441
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018442 li = li->li_next;
18443 }
18444 rettv->vval.v_number = 0;
18445 }
18446#endif
18447}
18448
18449/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018450 * "setpos()" function
18451 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018453f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018454{
18455 pos_T pos;
18456 int fnum;
18457 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018458 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018459
Bram Moolenaar08250432008-02-13 11:42:46 +000018460 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018461 name = get_tv_string_chk(argvars);
18462 if (name != NULL)
18463 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018464 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018465 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018466 if (--pos.col < 0)
18467 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018468 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018469 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018470 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018471 if (fnum == curbuf->b_fnum)
18472 {
18473 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018474 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018475 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018476 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018477 curwin->w_set_curswant = FALSE;
18478 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018479 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018480 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018481 }
18482 else
18483 EMSG(_(e_invarg));
18484 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018485 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18486 {
18487 /* set mark */
18488 if (setmark_pos(name[1], &pos, fnum) == OK)
18489 rettv->vval.v_number = 0;
18490 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018491 else
18492 EMSG(_(e_invarg));
18493 }
18494 }
18495}
18496
18497/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018498 * "setqflist()" function
18499 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018501f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018502{
18503 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18504}
18505
18506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018507 * "setreg()" function
18508 */
18509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018510f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018511{
18512 int regname;
18513 char_u *strregname;
18514 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018515 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018516 int append;
18517 char_u yank_type;
18518 long block_len;
18519
18520 block_len = -1;
18521 yank_type = MAUTO;
18522 append = FALSE;
18523
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018524 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018525 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018526
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018527 if (strregname == NULL)
18528 return; /* type error; errmsg already given */
18529 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018530 if (regname == 0 || regname == '@')
18531 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018532
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018533 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018535 stropt = get_tv_string_chk(&argvars[2]);
18536 if (stropt == NULL)
18537 return; /* type error */
18538 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018539 switch (*stropt)
18540 {
18541 case 'a': case 'A': /* append */
18542 append = TRUE;
18543 break;
18544 case 'v': case 'c': /* character-wise selection */
18545 yank_type = MCHAR;
18546 break;
18547 case 'V': case 'l': /* line-wise selection */
18548 yank_type = MLINE;
18549 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 case 'b': case Ctrl_V: /* block-wise selection */
18551 yank_type = MBLOCK;
18552 if (VIM_ISDIGIT(stropt[1]))
18553 {
18554 ++stropt;
18555 block_len = getdigits(&stropt) - 1;
18556 --stropt;
18557 }
18558 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 }
18560 }
18561
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018562 if (argvars[1].v_type == VAR_LIST)
18563 {
18564 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018565 char_u **allocval;
18566 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018567 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018568 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018569 int len = argvars[1].vval.v_list->lv_len;
18570 listitem_T *li;
18571
Bram Moolenaar7d647822014-04-05 21:28:56 +020018572 /* First half: use for pointers to result lines; second half: use for
18573 * pointers to allocated copies. */
18574 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018575 if (lstval == NULL)
18576 return;
18577 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018578 allocval = lstval + len + 2;
18579 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018580
18581 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18582 li = li->li_next)
18583 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018584 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018585 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018586 goto free_lstval;
18587 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018588 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018589 /* Need to make a copy, next get_tv_string_buf_chk() will
18590 * overwrite the string. */
18591 strval = vim_strsave(buf);
18592 if (strval == NULL)
18593 goto free_lstval;
18594 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018595 }
18596 *curval++ = strval;
18597 }
18598 *curval++ = NULL;
18599
18600 write_reg_contents_lst(regname, lstval, -1,
18601 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018602free_lstval:
18603 while (curallocval > allocval)
18604 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018605 vim_free(lstval);
18606 }
18607 else
18608 {
18609 strval = get_tv_string_chk(&argvars[1]);
18610 if (strval == NULL)
18611 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018612 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018613 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018614 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018615 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616}
18617
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018618/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018619 * "settabvar()" function
18620 */
18621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018622f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018623{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018624#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018625 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018626 tabpage_T *tp;
18627#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018628 char_u *varname, *tabvarname;
18629 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018630
18631 rettv->vval.v_number = 0;
18632
18633 if (check_restricted() || check_secure())
18634 return;
18635
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018636#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018637 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018638#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018639 varname = get_tv_string_chk(&argvars[1]);
18640 varp = &argvars[2];
18641
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018642 if (varname != NULL && varp != NULL
18643#ifdef FEAT_WINDOWS
18644 && tp != NULL
18645#endif
18646 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018647 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018648#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018649 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018650 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018651#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018652
18653 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18654 if (tabvarname != NULL)
18655 {
18656 STRCPY(tabvarname, "t:");
18657 STRCPY(tabvarname + 2, varname);
18658 set_var(tabvarname, varp, TRUE);
18659 vim_free(tabvarname);
18660 }
18661
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018662#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018663 /* Restore current tabpage */
18664 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018665 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018666#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018667 }
18668}
18669
18670/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018671 * "settabwinvar()" function
18672 */
18673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018674f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018675{
18676 setwinvar(argvars, rettv, 1);
18677}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678
18679/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018680 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018683f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018685 setwinvar(argvars, rettv, 0);
18686}
18687
18688/*
18689 * "setwinvar()" and "settabwinvar()" functions
18690 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018691
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018693setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018694{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 win_T *win;
18696#ifdef FEAT_WINDOWS
18697 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018698 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018699 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700#endif
18701 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018702 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018704 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705
18706 if (check_restricted() || check_secure())
18707 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018708
18709#ifdef FEAT_WINDOWS
18710 if (off == 1)
18711 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18712 else
18713 tp = curtab;
18714#endif
18715 win = find_win_by_nr(&argvars[off], tp);
18716 varname = get_tv_string_chk(&argvars[off + 1]);
18717 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
18719 if (win != NULL && varname != NULL && varp != NULL)
18720 {
18721#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018722 need_switch_win = !(tp == curtab && win == curwin);
18723 if (!need_switch_win
18724 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018727 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018729 long numval;
18730 char_u *strval;
18731 int error = FALSE;
18732
18733 ++varname;
18734 numval = get_tv_number_chk(varp, &error);
18735 strval = get_tv_string_buf_chk(varp, nbuf);
18736 if (!error && strval != NULL)
18737 set_option_value(varname, numval, strval, OPT_LOCAL);
18738 }
18739 else
18740 {
18741 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18742 if (winvarname != NULL)
18743 {
18744 STRCPY(winvarname, "w:");
18745 STRCPY(winvarname + 2, varname);
18746 set_var(winvarname, varp, TRUE);
18747 vim_free(winvarname);
18748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 }
18750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018752 if (need_switch_win)
18753 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754#endif
18755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756}
18757
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018758#ifdef FEAT_CRYPT
18759/*
18760 * "sha256({string})" function
18761 */
18762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018763f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018764{
18765 char_u *p;
18766
18767 p = get_tv_string(&argvars[0]);
18768 rettv->vval.v_string = vim_strsave(
18769 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18770 rettv->v_type = VAR_STRING;
18771}
18772#endif /* FEAT_CRYPT */
18773
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018775 * "shellescape({string})" function
18776 */
18777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018778f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018779{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018780 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018781 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018782 rettv->v_type = VAR_STRING;
18783}
18784
18785/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018786 * shiftwidth() function
18787 */
18788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018789f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018790{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018791 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018792}
18793
18794/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018795 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 */
18797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018798f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018800 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
Bram Moolenaar0d660222005-01-07 21:51:51 +000018802 p = get_tv_string(&argvars[0]);
18803 rettv->vval.v_string = vim_strsave(p);
18804 simplify_filename(rettv->vval.v_string); /* simplify in place */
18805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806}
18807
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018808#ifdef FEAT_FLOAT
18809/*
18810 * "sin()" function
18811 */
18812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018813f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018814{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018815 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018816
18817 rettv->v_type = VAR_FLOAT;
18818 if (get_float_arg(argvars, &f) == OK)
18819 rettv->vval.v_float = sin(f);
18820 else
18821 rettv->vval.v_float = 0.0;
18822}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018823
18824/*
18825 * "sinh()" function
18826 */
18827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018828f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018829{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018830 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018831
18832 rettv->v_type = VAR_FLOAT;
18833 if (get_float_arg(argvars, &f) == OK)
18834 rettv->vval.v_float = sinh(f);
18835 else
18836 rettv->vval.v_float = 0.0;
18837}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018838#endif
18839
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840static int
18841#ifdef __BORLANDC__
18842 _RTLENTRYF
18843#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018844 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845static int
18846#ifdef __BORLANDC__
18847 _RTLENTRYF
18848#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018849 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018850
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018851/* struct used in the array that's given to qsort() */
18852typedef struct
18853{
18854 listitem_T *item;
18855 int idx;
18856} sortItem_T;
18857
Bram Moolenaar0b962472016-02-22 22:51:33 +010018858/* struct storing information about current sort */
18859typedef struct
18860{
18861 int item_compare_ic;
18862 int item_compare_numeric;
18863 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018864#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018865 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018866#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018867 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018868 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018869 dict_T *item_compare_selfdict;
18870 int item_compare_func_err;
18871 int item_compare_keep_zero;
18872} sortinfo_T;
18873static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018874static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018875#define ITEM_COMPARE_FAIL 999
18876
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018878 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880 static int
18881#ifdef __BORLANDC__
18882_RTLENTRYF
18883#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018884item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018885{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018886 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018887 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018889 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018890 int res;
18891 char_u numbuf1[NUMBUFLEN];
18892 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018894 si1 = (sortItem_T *)s1;
18895 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018896 tv1 = &si1->item->li_tv;
18897 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018898
Bram Moolenaar0b962472016-02-22 22:51:33 +010018899 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018900 {
18901 long v1 = get_tv_number(tv1);
18902 long v2 = get_tv_number(tv2);
18903
18904 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18905 }
18906
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018907#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018908 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018909 {
18910 float_T v1 = get_tv_float(tv1);
18911 float_T v2 = get_tv_float(tv2);
18912
18913 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18914 }
18915#endif
18916
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018917 /* tv2string() puts quotes around a string and allocates memory. Don't do
18918 * that for string variables. Use a single quote when comparing with a
18919 * non-string to do what the docs promise. */
18920 if (tv1->v_type == VAR_STRING)
18921 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018922 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018923 p1 = (char_u *)"'";
18924 else
18925 p1 = tv1->vval.v_string;
18926 }
18927 else
18928 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18929 if (tv2->v_type == VAR_STRING)
18930 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018931 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018932 p2 = (char_u *)"'";
18933 else
18934 p2 = tv2->vval.v_string;
18935 }
18936 else
18937 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018938 if (p1 == NULL)
18939 p1 = (char_u *)"";
18940 if (p2 == NULL)
18941 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018942 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018943 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018944 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018945 res = STRICMP(p1, p2);
18946 else
18947 res = STRCMP(p1, p2);
18948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018949 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018950 {
18951 double n1, n2;
18952 n1 = strtod((char *)p1, (char **)&p1);
18953 n2 = strtod((char *)p2, (char **)&p2);
18954 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18955 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018956
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018957 /* When the result would be zero, compare the item indexes. Makes the
18958 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018959 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018960 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018961
Bram Moolenaar0d660222005-01-07 21:51:51 +000018962 vim_free(tofree1);
18963 vim_free(tofree2);
18964 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018965}
18966
18967 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018968#ifdef __BORLANDC__
18969_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018971item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018972{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018973 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018974 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018975 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018976 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018977 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018978 char_u *func_name;
18979 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018981 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018982 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018983 return 0;
18984
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018985 si1 = (sortItem_T *)s1;
18986 si2 = (sortItem_T *)s2;
18987
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018988 if (partial == NULL)
18989 func_name = sortinfo->item_compare_func;
18990 else
18991 func_name = partial->pt_name;
18992
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018993 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018994 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018995 copy_tv(&si1->item->li_tv, &argv[0]);
18996 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018997
18998 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018999 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019000 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019001 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019002 clear_tv(&argv[0]);
19003 clear_tv(&argv[1]);
19004
19005 if (res == FAIL)
19006 res = ITEM_COMPARE_FAIL;
19007 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019008 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19009 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019010 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019012
19013 /* When the result would be zero, compare the pointers themselves. Makes
19014 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019015 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019016 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019017
Bram Moolenaar0d660222005-01-07 21:51:51 +000019018 return res;
19019}
19020
19021/*
19022 * "sort({list})" function
19023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019025do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026{
Bram Moolenaar33570922005-01-25 22:26:29 +000019027 list_T *l;
19028 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019029 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019030 sortinfo_T *old_sortinfo;
19031 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032 long len;
19033 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019034
Bram Moolenaar0b962472016-02-22 22:51:33 +010019035 /* Pointer to current info struct used in compare function. Save and
19036 * restore the current one for nested calls. */
19037 old_sortinfo = sortinfo;
19038 sortinfo = &info;
19039
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019041 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 else
19043 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019044 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019045 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019046 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19047 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019048 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019049 rettv->vval.v_list = l;
19050 rettv->v_type = VAR_LIST;
19051 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019052
Bram Moolenaar0d660222005-01-07 21:51:51 +000019053 len = list_len(l);
19054 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019055 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019056
Bram Moolenaar0b962472016-02-22 22:51:33 +010019057 info.item_compare_ic = FALSE;
19058 info.item_compare_numeric = FALSE;
19059 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019060#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019061 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019062#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019063 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019064 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019065 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 if (argvars[1].v_type != VAR_UNKNOWN)
19067 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019068 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019069 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019070 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019071 else if (argvars[1].v_type == VAR_PARTIAL)
19072 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 else
19074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019075 int error = FALSE;
19076
19077 i = get_tv_number_chk(&argvars[1], &error);
19078 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019079 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019081 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019082 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019084 else if (i != 0)
19085 {
19086 EMSG(_(e_invarg));
19087 goto theend;
19088 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019089 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019090 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019091 if (*info.item_compare_func == NUL)
19092 {
19093 /* empty string means default sort */
19094 info.item_compare_func = NULL;
19095 }
19096 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019097 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 info.item_compare_func = NULL;
19099 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019100 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019101 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019102 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019103 info.item_compare_func = NULL;
19104 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019105 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019106#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019107 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019108 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 info.item_compare_func = NULL;
19110 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019111 }
19112#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019113 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019114 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019115 info.item_compare_func = NULL;
19116 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019117 }
19118 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019119 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019120
19121 if (argvars[2].v_type != VAR_UNKNOWN)
19122 {
19123 /* optional third argument: {dict} */
19124 if (argvars[2].v_type != VAR_DICT)
19125 {
19126 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019128 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019129 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019130 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019134 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019135 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137
Bram Moolenaar327aa022014-03-25 18:24:23 +010019138 i = 0;
19139 if (sort)
19140 {
19141 /* sort(): ptrs will be the list to sort */
19142 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019143 {
19144 ptrs[i].item = li;
19145 ptrs[i].idx = i;
19146 ++i;
19147 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019148
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 info.item_compare_func_err = FALSE;
19150 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019151 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019152 if ((info.item_compare_func != NULL
19153 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019154 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019156 EMSG(_("E702: Sort compare function failed"));
19157 else
19158 {
19159 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019160 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019161 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019162 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019163 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019164
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019166 {
19167 /* Clear the List and append the items in sorted order. */
19168 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19169 l->lv_len = 0;
19170 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019171 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019172 }
19173 }
19174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019176 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019177 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019178
19179 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019180 info.item_compare_func_err = FALSE;
19181 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019182 item_compare_func_ptr = info.item_compare_func != NULL
19183 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019184 ? item_compare2 : item_compare;
19185
19186 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19187 li = li->li_next)
19188 {
19189 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19190 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019191 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019192 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019193 {
19194 EMSG(_("E882: Uniq compare function failed"));
19195 break;
19196 }
19197 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198
Bram Moolenaar0b962472016-02-22 22:51:33 +010019199 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019200 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019201 while (--i >= 0)
19202 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019203 li = ptrs[i].item->li_next;
19204 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019205 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019206 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019207 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019208 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019209 list_fix_watch(l, li);
19210 listitem_free(li);
19211 l->lv_len--;
19212 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019213 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019214 }
19215
19216 vim_free(ptrs);
19217 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218theend:
19219 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220}
19221
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019222/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019223 * "sort({list})" function
19224 */
19225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019226f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019227{
19228 do_sort_uniq(argvars, rettv, TRUE);
19229}
19230
19231/*
19232 * "uniq({list})" function
19233 */
19234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019235f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019236{
19237 do_sort_uniq(argvars, rettv, FALSE);
19238}
19239
19240/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019241 * "soundfold({word})" function
19242 */
19243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019244f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019245{
19246 char_u *s;
19247
19248 rettv->v_type = VAR_STRING;
19249 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019250#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019251 rettv->vval.v_string = eval_soundfold(s);
19252#else
19253 rettv->vval.v_string = vim_strsave(s);
19254#endif
19255}
19256
19257/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019258 * "spellbadword()" function
19259 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019261f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019262{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019263 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019264 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019265 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019266
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019267 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019268 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019269
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019270#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019271 if (argvars[0].v_type == VAR_UNKNOWN)
19272 {
19273 /* Find the start and length of the badly spelled word. */
19274 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19275 if (len != 0)
19276 word = ml_get_cursor();
19277 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019278 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019279 {
19280 char_u *str = get_tv_string_chk(&argvars[0]);
19281 int capcol = -1;
19282
19283 if (str != NULL)
19284 {
19285 /* Check the argument for spelling. */
19286 while (*str != NUL)
19287 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019288 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019289 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019290 {
19291 word = str;
19292 break;
19293 }
19294 str += len;
19295 }
19296 }
19297 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019298#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019299
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019300 list_append_string(rettv->vval.v_list, word, len);
19301 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019302 attr == HLF_SPB ? "bad" :
19303 attr == HLF_SPR ? "rare" :
19304 attr == HLF_SPL ? "local" :
19305 attr == HLF_SPC ? "caps" :
19306 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019307}
19308
19309/*
19310 * "spellsuggest()" function
19311 */
19312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019313f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019314{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019315#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019316 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019317 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019318 int maxcount;
19319 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019320 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019321 listitem_T *li;
19322 int need_capital = FALSE;
19323#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019324
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019325 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019326 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019328#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019329 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019330 {
19331 str = get_tv_string(&argvars[0]);
19332 if (argvars[1].v_type != VAR_UNKNOWN)
19333 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019334 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335 if (maxcount <= 0)
19336 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019337 if (argvars[2].v_type != VAR_UNKNOWN)
19338 {
19339 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19340 if (typeerr)
19341 return;
19342 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343 }
19344 else
19345 maxcount = 25;
19346
Bram Moolenaar4770d092006-01-12 23:22:24 +000019347 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019348
19349 for (i = 0; i < ga.ga_len; ++i)
19350 {
19351 str = ((char_u **)ga.ga_data)[i];
19352
19353 li = listitem_alloc();
19354 if (li == NULL)
19355 vim_free(str);
19356 else
19357 {
19358 li->li_tv.v_type = VAR_STRING;
19359 li->li_tv.v_lock = 0;
19360 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019361 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019362 }
19363 }
19364 ga_clear(&ga);
19365 }
19366#endif
19367}
19368
Bram Moolenaar0d660222005-01-07 21:51:51 +000019369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019370f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019371{
19372 char_u *str;
19373 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019374 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019375 regmatch_T regmatch;
19376 char_u patbuf[NUMBUFLEN];
19377 char_u *save_cpo;
19378 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019379 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019380 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019381 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019382
19383 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19384 save_cpo = p_cpo;
19385 p_cpo = (char_u *)"";
19386
19387 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019388 if (argvars[1].v_type != VAR_UNKNOWN)
19389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019390 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19391 if (pat == NULL)
19392 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019393 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019394 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019395 }
19396 if (pat == NULL || *pat == NUL)
19397 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019398
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019399 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019400 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019401 if (typeerr)
19402 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403
Bram Moolenaar0d660222005-01-07 21:51:51 +000019404 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19405 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019406 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019407 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019408 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019409 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019410 if (*str == NUL)
19411 match = FALSE; /* empty item at the end */
19412 else
19413 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019414 if (match)
19415 end = regmatch.startp[0];
19416 else
19417 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019418 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19419 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019421 if (list_append_string(rettv->vval.v_list, str,
19422 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019423 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 }
19425 if (!match)
19426 break;
19427 /* Advance to just after the match. */
19428 if (regmatch.endp[0] > str)
19429 col = 0;
19430 else
19431 {
19432 /* Don't get stuck at the same match. */
19433#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019434 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019435#else
19436 col = 1;
19437#endif
19438 }
19439 str = regmatch.endp[0];
19440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019441
Bram Moolenaar473de612013-06-08 18:19:48 +020019442 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019444
Bram Moolenaar0d660222005-01-07 21:51:51 +000019445 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019448#ifdef FEAT_FLOAT
19449/*
19450 * "sqrt()" function
19451 */
19452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019453f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019454{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019455 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019456
19457 rettv->v_type = VAR_FLOAT;
19458 if (get_float_arg(argvars, &f) == OK)
19459 rettv->vval.v_float = sqrt(f);
19460 else
19461 rettv->vval.v_float = 0.0;
19462}
19463
19464/*
19465 * "str2float()" function
19466 */
19467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019468f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019469{
19470 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19471
19472 if (*p == '+')
19473 p = skipwhite(p + 1);
19474 (void)string2float(p, &rettv->vval.v_float);
19475 rettv->v_type = VAR_FLOAT;
19476}
19477#endif
19478
Bram Moolenaar2c932302006-03-18 21:42:09 +000019479/*
19480 * "str2nr()" function
19481 */
19482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019483f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019484{
19485 int base = 10;
19486 char_u *p;
19487 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019488 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019489
19490 if (argvars[1].v_type != VAR_UNKNOWN)
19491 {
19492 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019493 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019494 {
19495 EMSG(_(e_invarg));
19496 return;
19497 }
19498 }
19499
19500 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019501 if (*p == '+')
19502 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019503 switch (base)
19504 {
19505 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19506 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19507 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19508 default: what = 0;
19509 }
19510 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019511 rettv->vval.v_number = n;
19512}
19513
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514#ifdef HAVE_STRFTIME
19515/*
19516 * "strftime({format}[, {time}])" function
19517 */
19518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019519f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019520{
19521 char_u result_buf[256];
19522 struct tm *curtime;
19523 time_t seconds;
19524 char_u *p;
19525
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019526 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019528 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019529 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019530 seconds = time(NULL);
19531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019532 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533 curtime = localtime(&seconds);
19534 /* MSVC returns NULL for an invalid value of seconds. */
19535 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019536 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537 else
19538 {
19539# ifdef FEAT_MBYTE
19540 vimconv_T conv;
19541 char_u *enc;
19542
19543 conv.vc_type = CONV_NONE;
19544 enc = enc_locale();
19545 convert_setup(&conv, p_enc, enc);
19546 if (conv.vc_type != CONV_NONE)
19547 p = string_convert(&conv, p, NULL);
19548# endif
19549 if (p != NULL)
19550 (void)strftime((char *)result_buf, sizeof(result_buf),
19551 (char *)p, curtime);
19552 else
19553 result_buf[0] = NUL;
19554
19555# ifdef FEAT_MBYTE
19556 if (conv.vc_type != CONV_NONE)
19557 vim_free(p);
19558 convert_setup(&conv, enc, p_enc);
19559 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019560 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 else
19562# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019563 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564
19565# ifdef FEAT_MBYTE
19566 /* Release conversion descriptors */
19567 convert_setup(&conv, NULL, NULL);
19568 vim_free(enc);
19569# endif
19570 }
19571}
19572#endif
19573
19574/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019575 * "strgetchar()" function
19576 */
19577 static void
19578f_strgetchar(typval_T *argvars, typval_T *rettv)
19579{
19580 char_u *str;
19581 int len;
19582 int error = FALSE;
19583 int charidx;
19584
19585 rettv->vval.v_number = -1;
19586 str = get_tv_string_chk(&argvars[0]);
19587 if (str == NULL)
19588 return;
19589 len = (int)STRLEN(str);
19590 charidx = get_tv_number_chk(&argvars[1], &error);
19591 if (error)
19592 return;
19593#ifdef FEAT_MBYTE
19594 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019595 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019596
19597 while (charidx >= 0 && byteidx < len)
19598 {
19599 if (charidx == 0)
19600 {
19601 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19602 break;
19603 }
19604 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019605 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019606 }
19607 }
19608#else
19609 if (charidx < len)
19610 rettv->vval.v_number = str[charidx];
19611#endif
19612}
19613
19614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019615 * "stridx()" function
19616 */
19617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019618f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619{
19620 char_u buf[NUMBUFLEN];
19621 char_u *needle;
19622 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019623 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019625 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019627 needle = get_tv_string_chk(&argvars[1]);
19628 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019629 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019630 if (needle == NULL || haystack == NULL)
19631 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632
Bram Moolenaar33570922005-01-25 22:26:29 +000019633 if (argvars[2].v_type != VAR_UNKNOWN)
19634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019635 int error = FALSE;
19636
19637 start_idx = get_tv_number_chk(&argvars[2], &error);
19638 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019639 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019640 if (start_idx >= 0)
19641 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019642 }
19643
19644 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19645 if (pos != NULL)
19646 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647}
19648
19649/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019650 * "string()" function
19651 */
19652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019653f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019654{
19655 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019656 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019657
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019658 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019659 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19660 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019661 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019662 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019663 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019664}
19665
19666/*
19667 * "strlen()" function
19668 */
19669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019670f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 rettv->vval.v_number = (varnumber_T)(STRLEN(
19673 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674}
19675
19676/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019677 * "strchars()" function
19678 */
19679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019680f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019681{
19682 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019683 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019684#ifdef FEAT_MBYTE
19685 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019686 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019687#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019688
19689 if (argvars[1].v_type != VAR_UNKNOWN)
19690 skipcc = get_tv_number_chk(&argvars[1], NULL);
19691 if (skipcc < 0 || skipcc > 1)
19692 EMSG(_(e_invarg));
19693 else
19694 {
19695#ifdef FEAT_MBYTE
19696 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19697 while (*s != NUL)
19698 {
19699 func_mb_ptr2char_adv(&s);
19700 ++len;
19701 }
19702 rettv->vval.v_number = len;
19703#else
19704 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19705#endif
19706 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019707}
19708
19709/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019710 * "strdisplaywidth()" function
19711 */
19712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019713f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019714{
19715 char_u *s = get_tv_string(&argvars[0]);
19716 int col = 0;
19717
19718 if (argvars[1].v_type != VAR_UNKNOWN)
19719 col = get_tv_number(&argvars[1]);
19720
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019721 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019722}
19723
19724/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019725 * "strwidth()" function
19726 */
19727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019728f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019729{
19730 char_u *s = get_tv_string(&argvars[0]);
19731
19732 rettv->vval.v_number = (varnumber_T)(
19733#ifdef FEAT_MBYTE
19734 mb_string2cells(s, -1)
19735#else
19736 STRLEN(s)
19737#endif
19738 );
19739}
19740
19741/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019742 * "strcharpart()" function
19743 */
19744 static void
19745f_strcharpart(typval_T *argvars, typval_T *rettv)
19746{
19747#ifdef FEAT_MBYTE
19748 char_u *p;
19749 int nchar;
19750 int nbyte = 0;
19751 int charlen;
19752 int len = 0;
19753 int slen;
19754 int error = FALSE;
19755
19756 p = get_tv_string(&argvars[0]);
19757 slen = (int)STRLEN(p);
19758
19759 nchar = get_tv_number_chk(&argvars[1], &error);
19760 if (!error)
19761 {
19762 if (nchar > 0)
19763 while (nchar > 0 && nbyte < slen)
19764 {
19765 nbyte += mb_char2len(p[nbyte]);
19766 --nchar;
19767 }
19768 else
19769 nbyte = nchar;
19770 if (argvars[2].v_type != VAR_UNKNOWN)
19771 {
19772 charlen = get_tv_number(&argvars[2]);
19773 while (charlen > 0 && nbyte + len < slen)
19774 {
19775 len += mb_char2len(p[nbyte + len]);
19776 --charlen;
19777 }
19778 }
19779 else
19780 len = slen - nbyte; /* default: all bytes that are available. */
19781 }
19782
19783 /*
19784 * Only return the overlap between the specified part and the actual
19785 * string.
19786 */
19787 if (nbyte < 0)
19788 {
19789 len += nbyte;
19790 nbyte = 0;
19791 }
19792 else if (nbyte > slen)
19793 nbyte = slen;
19794 if (len < 0)
19795 len = 0;
19796 else if (nbyte + len > slen)
19797 len = slen - nbyte;
19798
19799 rettv->v_type = VAR_STRING;
19800 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19801#else
19802 f_strpart(argvars, rettv);
19803#endif
19804}
19805
19806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 * "strpart()" function
19808 */
19809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019810f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811{
19812 char_u *p;
19813 int n;
19814 int len;
19815 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019816 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019818 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819 slen = (int)STRLEN(p);
19820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019821 n = get_tv_number_chk(&argvars[1], &error);
19822 if (error)
19823 len = 0;
19824 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019825 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826 else
19827 len = slen - n; /* default len: all bytes that are available. */
19828
19829 /*
19830 * Only return the overlap between the specified part and the actual
19831 * string.
19832 */
19833 if (n < 0)
19834 {
19835 len += n;
19836 n = 0;
19837 }
19838 else if (n > slen)
19839 n = slen;
19840 if (len < 0)
19841 len = 0;
19842 else if (n + len > slen)
19843 len = slen - n;
19844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019845 rettv->v_type = VAR_STRING;
19846 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019847}
19848
19849/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019850 * "strridx()" function
19851 */
19852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019853f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019854{
19855 char_u buf[NUMBUFLEN];
19856 char_u *needle;
19857 char_u *haystack;
19858 char_u *rest;
19859 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019860 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019861
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019862 needle = get_tv_string_chk(&argvars[1]);
19863 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019864
19865 rettv->vval.v_number = -1;
19866 if (needle == NULL || haystack == NULL)
19867 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019868
19869 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019870 if (argvars[2].v_type != VAR_UNKNOWN)
19871 {
19872 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019873 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019874 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019875 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019876 }
19877 else
19878 end_idx = haystack_len;
19879
Bram Moolenaar0d660222005-01-07 21:51:51 +000019880 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019881 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019882 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019883 lastmatch = haystack + end_idx;
19884 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019885 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019886 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019887 for (rest = haystack; *rest != '\0'; ++rest)
19888 {
19889 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019890 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019891 break;
19892 lastmatch = rest;
19893 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019894 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019895
19896 if (lastmatch == NULL)
19897 rettv->vval.v_number = -1;
19898 else
19899 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19900}
19901
19902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903 * "strtrans()" function
19904 */
19905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019906f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019908 rettv->v_type = VAR_STRING;
19909 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019910}
19911
19912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019913 * "submatch()" function
19914 */
19915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019916f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019917{
Bram Moolenaar41571762014-04-02 19:00:58 +020019918 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019919 int no;
19920 int retList = 0;
19921
19922 no = (int)get_tv_number_chk(&argvars[0], &error);
19923 if (error)
19924 return;
19925 error = FALSE;
19926 if (argvars[1].v_type != VAR_UNKNOWN)
19927 retList = get_tv_number_chk(&argvars[1], &error);
19928 if (error)
19929 return;
19930
19931 if (retList == 0)
19932 {
19933 rettv->v_type = VAR_STRING;
19934 rettv->vval.v_string = reg_submatch(no);
19935 }
19936 else
19937 {
19938 rettv->v_type = VAR_LIST;
19939 rettv->vval.v_list = reg_submatch_list(no);
19940 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019941}
19942
19943/*
19944 * "substitute()" function
19945 */
19946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019947f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019948{
19949 char_u patbuf[NUMBUFLEN];
19950 char_u subbuf[NUMBUFLEN];
19951 char_u flagsbuf[NUMBUFLEN];
19952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019953 char_u *str = get_tv_string_chk(&argvars[0]);
19954 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19955 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19956 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19957
Bram Moolenaar0d660222005-01-07 21:51:51 +000019958 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019959 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19960 rettv->vval.v_string = NULL;
19961 else
19962 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019963}
19964
19965/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019966 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019969f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970{
19971 int id = 0;
19972#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019973 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 long col;
19975 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019976 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019978 lnum = get_tv_lnum(argvars); /* -1 on type error */
19979 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19980 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019982 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019983 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019984 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985#endif
19986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019987 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988}
19989
19990/*
19991 * "synIDattr(id, what [, mode])" function
19992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019994f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
19996 char_u *p = NULL;
19997#ifdef FEAT_SYN_HL
19998 int id;
19999 char_u *what;
20000 char_u *mode;
20001 char_u modebuf[NUMBUFLEN];
20002 int modec;
20003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 id = get_tv_number(&argvars[0]);
20005 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020006 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020008 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020010 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 modec = 0; /* replace invalid with current */
20012 }
20013 else
20014 {
20015#ifdef FEAT_GUI
20016 if (gui.in_use)
20017 modec = 'g';
20018 else
20019#endif
20020 if (t_colors > 1)
20021 modec = 'c';
20022 else
20023 modec = 't';
20024 }
20025
20026
20027 switch (TOLOWER_ASC(what[0]))
20028 {
20029 case 'b':
20030 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20031 p = highlight_color(id, what, modec);
20032 else /* bold */
20033 p = highlight_has_attr(id, HL_BOLD, modec);
20034 break;
20035
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020036 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 p = highlight_color(id, what, modec);
20038 break;
20039
20040 case 'i':
20041 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20042 p = highlight_has_attr(id, HL_INVERSE, modec);
20043 else /* italic */
20044 p = highlight_has_attr(id, HL_ITALIC, modec);
20045 break;
20046
20047 case 'n': /* name */
20048 p = get_highlight_name(NULL, id - 1);
20049 break;
20050
20051 case 'r': /* reverse */
20052 p = highlight_has_attr(id, HL_INVERSE, modec);
20053 break;
20054
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020055 case 's':
20056 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20057 p = highlight_color(id, what, modec);
20058 else /* standout */
20059 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020060 break;
20061
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020062 case 'u':
20063 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20064 /* underline */
20065 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20066 else
20067 /* undercurl */
20068 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 break;
20070 }
20071
20072 if (p != NULL)
20073 p = vim_strsave(p);
20074#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020075 rettv->v_type = VAR_STRING;
20076 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077}
20078
20079/*
20080 * "synIDtrans(id)" function
20081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020083f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084{
20085 int id;
20086
20087#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020089
20090 if (id > 0)
20091 id = syn_get_final_id(id);
20092 else
20093#endif
20094 id = 0;
20095
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020097}
20098
20099/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020100 * "synconcealed(lnum, col)" function
20101 */
20102 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020103f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020104{
20105#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20106 long lnum;
20107 long col;
20108 int syntax_flags = 0;
20109 int cchar;
20110 int matchid = 0;
20111 char_u str[NUMBUFLEN];
20112#endif
20113
20114 rettv->v_type = VAR_LIST;
20115 rettv->vval.v_list = NULL;
20116
20117#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20118 lnum = get_tv_lnum(argvars); /* -1 on type error */
20119 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20120
20121 vim_memset(str, NUL, sizeof(str));
20122
20123 if (rettv_list_alloc(rettv) != FAIL)
20124 {
20125 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20126 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20127 && curwin->w_p_cole > 0)
20128 {
20129 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20130 syntax_flags = get_syntax_info(&matchid);
20131
20132 /* get the conceal character */
20133 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20134 {
20135 cchar = syn_get_sub_char();
20136 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20137 cchar = lcs_conceal;
20138 if (cchar != NUL)
20139 {
20140# ifdef FEAT_MBYTE
20141 if (has_mbyte)
20142 (*mb_char2bytes)(cchar, str);
20143 else
20144# endif
20145 str[0] = cchar;
20146 }
20147 }
20148 }
20149
20150 list_append_number(rettv->vval.v_list,
20151 (syntax_flags & HL_CONCEAL) != 0);
20152 /* -1 to auto-determine strlen */
20153 list_append_string(rettv->vval.v_list, str, -1);
20154 list_append_number(rettv->vval.v_list, matchid);
20155 }
20156#endif
20157}
20158
20159/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020160 * "synstack(lnum, col)" function
20161 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020163f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020164{
20165#ifdef FEAT_SYN_HL
20166 long lnum;
20167 long col;
20168 int i;
20169 int id;
20170#endif
20171
20172 rettv->v_type = VAR_LIST;
20173 rettv->vval.v_list = NULL;
20174
20175#ifdef FEAT_SYN_HL
20176 lnum = get_tv_lnum(argvars); /* -1 on type error */
20177 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20178
20179 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020180 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020181 && rettv_list_alloc(rettv) != FAIL)
20182 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020183 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020184 for (i = 0; ; ++i)
20185 {
20186 id = syn_get_stack_item(i);
20187 if (id < 0)
20188 break;
20189 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20190 break;
20191 }
20192 }
20193#endif
20194}
20195
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020197get_cmd_output_as_rettv(
20198 typval_T *argvars,
20199 typval_T *rettv,
20200 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020201{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020202 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020204 char_u *infile = NULL;
20205 char_u buf[NUMBUFLEN];
20206 int err = FALSE;
20207 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020208 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020209 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020210
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020211 rettv->v_type = VAR_STRING;
20212 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020213 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020214 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020215
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020216 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020217 {
20218 /*
20219 * Write the string to a temp file, to be used for input of the shell
20220 * command.
20221 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020222 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020223 {
20224 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020225 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020226 }
20227
20228 fd = mch_fopen((char *)infile, WRITEBIN);
20229 if (fd == NULL)
20230 {
20231 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020232 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020233 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020234 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020235 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020236 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20237 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020238 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020239 else
20240 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020241 size_t len;
20242
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020243 p = get_tv_string_buf_chk(&argvars[1], buf);
20244 if (p == NULL)
20245 {
20246 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020247 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020248 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020249 len = STRLEN(p);
20250 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020251 err = TRUE;
20252 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020253 if (fclose(fd) != 0)
20254 err = TRUE;
20255 if (err)
20256 {
20257 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020258 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020259 }
20260 }
20261
Bram Moolenaar52a72462014-08-29 15:53:52 +020020262 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20263 * echoes typeahead, that messes up the display. */
20264 if (!msg_silent)
20265 flags += SHELL_COOKED;
20266
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020267 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020269 int len;
20270 listitem_T *li;
20271 char_u *s = NULL;
20272 char_u *start;
20273 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275
Bram Moolenaar52a72462014-08-29 15:53:52 +020020276 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020277 if (res == NULL)
20278 goto errret;
20279
20280 list = list_alloc();
20281 if (list == NULL)
20282 goto errret;
20283
20284 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020286 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020287 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020288 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020289 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020290
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020291 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020292 if (s == NULL)
20293 goto errret;
20294
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020295 for (p = s; start < end; ++p, ++start)
20296 *p = *start == NUL ? NL : *start;
20297 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020298
20299 li = listitem_alloc();
20300 if (li == NULL)
20301 {
20302 vim_free(s);
20303 goto errret;
20304 }
20305 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020306 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020307 li->li_tv.vval.v_string = s;
20308 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020310
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020311 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020312 rettv->v_type = VAR_LIST;
20313 rettv->vval.v_list = list;
20314 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020315 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316 else
20317 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020318 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020319#ifdef USE_CR
20320 /* translate <CR> into <NL> */
20321 if (res != NULL)
20322 {
20323 char_u *s;
20324
20325 for (s = res; *s; ++s)
20326 {
20327 if (*s == CAR)
20328 *s = NL;
20329 }
20330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331#else
20332# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020333 /* translate <CR><NL> into <NL> */
20334 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020336 char_u *s, *d;
20337
20338 d = res;
20339 for (s = res; *s; ++s)
20340 {
20341 if (s[0] == CAR && s[1] == NL)
20342 ++s;
20343 *d++ = *s;
20344 }
20345 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347# endif
20348#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020349 rettv->vval.v_string = res;
20350 res = NULL;
20351 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020352
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020353errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020354 if (infile != NULL)
20355 {
20356 mch_remove(infile);
20357 vim_free(infile);
20358 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020359 if (res != NULL)
20360 vim_free(res);
20361 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020362 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020363}
20364
20365/*
20366 * "system()" function
20367 */
20368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020369f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020370{
20371 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20372}
20373
20374/*
20375 * "systemlist()" function
20376 */
20377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020378f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020379{
20380 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381}
20382
20383/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020384 * "tabpagebuflist()" function
20385 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020387f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020388{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020389#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020390 tabpage_T *tp;
20391 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020392
20393 if (argvars[0].v_type == VAR_UNKNOWN)
20394 wp = firstwin;
20395 else
20396 {
20397 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20398 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020399 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020400 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020401 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020402 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020403 for (; wp != NULL; wp = wp->w_next)
20404 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020405 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020406 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020407 }
20408#endif
20409}
20410
20411
20412/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020413 * "tabpagenr()" function
20414 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020416f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020417{
20418 int nr = 1;
20419#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020420 char_u *arg;
20421
20422 if (argvars[0].v_type != VAR_UNKNOWN)
20423 {
20424 arg = get_tv_string_chk(&argvars[0]);
20425 nr = 0;
20426 if (arg != NULL)
20427 {
20428 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020429 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020430 else
20431 EMSG2(_(e_invexpr2), arg);
20432 }
20433 }
20434 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020435 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020436#endif
20437 rettv->vval.v_number = nr;
20438}
20439
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020440
20441#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020442static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020443
20444/*
20445 * Common code for tabpagewinnr() and winnr().
20446 */
20447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020448get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020449{
20450 win_T *twin;
20451 int nr = 1;
20452 win_T *wp;
20453 char_u *arg;
20454
20455 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20456 if (argvar->v_type != VAR_UNKNOWN)
20457 {
20458 arg = get_tv_string_chk(argvar);
20459 if (arg == NULL)
20460 nr = 0; /* type error; errmsg already given */
20461 else if (STRCMP(arg, "$") == 0)
20462 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20463 else if (STRCMP(arg, "#") == 0)
20464 {
20465 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20466 if (twin == NULL)
20467 nr = 0;
20468 }
20469 else
20470 {
20471 EMSG2(_(e_invexpr2), arg);
20472 nr = 0;
20473 }
20474 }
20475
20476 if (nr > 0)
20477 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20478 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020479 {
20480 if (wp == NULL)
20481 {
20482 /* didn't find it in this tabpage */
20483 nr = 0;
20484 break;
20485 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020486 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020487 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020488 return nr;
20489}
20490#endif
20491
20492/*
20493 * "tabpagewinnr()" function
20494 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020496f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020497{
20498 int nr = 1;
20499#ifdef FEAT_WINDOWS
20500 tabpage_T *tp;
20501
20502 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20503 if (tp == NULL)
20504 nr = 0;
20505 else
20506 nr = get_winnr(tp, &argvars[1]);
20507#endif
20508 rettv->vval.v_number = nr;
20509}
20510
20511
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020512/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020513 * "tagfiles()" function
20514 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020516f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020517{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020518 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020519 tagname_T tn;
20520 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020521
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020522 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020523 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020524 fname = alloc(MAXPATHL);
20525 if (fname == NULL)
20526 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020527
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020528 for (first = TRUE; ; first = FALSE)
20529 if (get_tagfname(&tn, first, fname) == FAIL
20530 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020531 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020532 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020533 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020534}
20535
20536/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020537 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020538 */
20539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020540f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020541{
20542 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020543
20544 tag_pattern = get_tv_string(&argvars[0]);
20545
20546 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020547 if (*tag_pattern == NUL)
20548 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020549
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020550 if (rettv_list_alloc(rettv) == OK)
20551 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020552}
20553
20554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555 * "tempname()" function
20556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020558f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020559{
20560 static int x = 'A';
20561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020562 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020563 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564
20565 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20566 * names. Skip 'I' and 'O', they are used for shell redirection. */
20567 do
20568 {
20569 if (x == 'Z')
20570 x = '0';
20571 else if (x == '9')
20572 x = 'A';
20573 else
20574 {
20575#ifdef EBCDIC
20576 if (x == 'I')
20577 x = 'J';
20578 else if (x == 'R')
20579 x = 'S';
20580 else
20581#endif
20582 ++x;
20583 }
20584 } while (x == 'I' || x == 'O');
20585}
20586
20587/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020588 * "test(list)" function: Just checking the walls...
20589 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020591f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020592{
20593 /* Used for unit testing. Change the code below to your liking. */
20594#if 0
20595 listitem_T *li;
20596 list_T *l;
20597 char_u *bad, *good;
20598
20599 if (argvars[0].v_type != VAR_LIST)
20600 return;
20601 l = argvars[0].vval.v_list;
20602 if (l == NULL)
20603 return;
20604 li = l->lv_first;
20605 if (li == NULL)
20606 return;
20607 bad = get_tv_string(&li->li_tv);
20608 li = li->li_next;
20609 if (li == NULL)
20610 return;
20611 good = get_tv_string(&li->li_tv);
20612 rettv->vval.v_number = test_edit_score(bad, good);
20613#endif
20614}
20615
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020616#ifdef FEAT_FLOAT
20617/*
20618 * "tan()" function
20619 */
20620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020621f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020622{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020623 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020624
20625 rettv->v_type = VAR_FLOAT;
20626 if (get_float_arg(argvars, &f) == OK)
20627 rettv->vval.v_float = tan(f);
20628 else
20629 rettv->vval.v_float = 0.0;
20630}
20631
20632/*
20633 * "tanh()" function
20634 */
20635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020636f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020637{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020638 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020639
20640 rettv->v_type = VAR_FLOAT;
20641 if (get_float_arg(argvars, &f) == OK)
20642 rettv->vval.v_float = tanh(f);
20643 else
20644 rettv->vval.v_float = 0.0;
20645}
20646#endif
20647
Bram Moolenaar975b5272016-03-15 23:10:59 +010020648#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20649/*
20650 * Get a callback from "arg". It can be a Funcref or a function name.
20651 * When "arg" is zero return an empty string.
20652 * Return NULL for an invalid argument.
20653 */
20654 char_u *
20655get_callback(typval_T *arg, partial_T **pp)
20656{
20657 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20658 {
20659 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020660 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020661 return (*pp)->pt_name;
20662 }
20663 *pp = NULL;
20664 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20665 return arg->vval.v_string;
20666 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20667 return (char_u *)"";
20668 EMSG(_("E921: Invalid callback argument"));
20669 return NULL;
20670}
20671#endif
20672
20673#ifdef FEAT_TIMERS
20674/*
20675 * "timer_start(time, callback [, options])" function
20676 */
20677 static void
20678f_timer_start(typval_T *argvars, typval_T *rettv)
20679{
20680 long msec = get_tv_number(&argvars[0]);
20681 timer_T *timer;
20682 int repeat = 0;
20683 char_u *callback;
20684 dict_T *dict;
20685
20686 if (argvars[2].v_type != VAR_UNKNOWN)
20687 {
20688 if (argvars[2].v_type != VAR_DICT
20689 || (dict = argvars[2].vval.v_dict) == NULL)
20690 {
20691 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20692 return;
20693 }
20694 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20695 repeat = get_dict_number(dict, (char_u *)"repeat");
20696 }
20697
20698 timer = create_timer(msec, repeat);
20699 callback = get_callback(&argvars[1], &timer->tr_partial);
20700 if (callback == NULL)
20701 {
20702 stop_timer(timer);
20703 rettv->vval.v_number = -1;
20704 }
20705 else
20706 {
20707 timer->tr_callback = vim_strsave(callback);
20708 rettv->vval.v_number = timer->tr_id;
20709 }
20710}
20711
20712/*
20713 * "timer_stop(timer)" function
20714 */
20715 static void
20716f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20717{
20718 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20719
20720 if (timer != NULL)
20721 stop_timer(timer);
20722}
20723#endif
20724
Bram Moolenaard52d9742005-08-21 22:20:28 +000020725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020726 * "tolower(string)" function
20727 */
20728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020729f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730{
20731 char_u *p;
20732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020733 p = vim_strsave(get_tv_string(&argvars[0]));
20734 rettv->v_type = VAR_STRING;
20735 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736
20737 if (p != NULL)
20738 while (*p != NUL)
20739 {
20740#ifdef FEAT_MBYTE
20741 int l;
20742
20743 if (enc_utf8)
20744 {
20745 int c, lc;
20746
20747 c = utf_ptr2char(p);
20748 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020749 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 /* TODO: reallocate string when byte count changes. */
20751 if (utf_char2len(lc) == l)
20752 utf_char2bytes(lc, p);
20753 p += l;
20754 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020755 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756 p += l; /* skip multi-byte character */
20757 else
20758#endif
20759 {
20760 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20761 ++p;
20762 }
20763 }
20764}
20765
20766/*
20767 * "toupper(string)" function
20768 */
20769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020770f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020772 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020773 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774}
20775
20776/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020777 * "tr(string, fromstr, tostr)" function
20778 */
20779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020780f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020781{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020782 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020783 char_u *fromstr;
20784 char_u *tostr;
20785 char_u *p;
20786#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020787 int inlen;
20788 int fromlen;
20789 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020790 int idx;
20791 char_u *cpstr;
20792 int cplen;
20793 int first = TRUE;
20794#endif
20795 char_u buf[NUMBUFLEN];
20796 char_u buf2[NUMBUFLEN];
20797 garray_T ga;
20798
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020799 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020800 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20801 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020802
20803 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020804 rettv->v_type = VAR_STRING;
20805 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020806 if (fromstr == NULL || tostr == NULL)
20807 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020808 ga_init2(&ga, (int)sizeof(char), 80);
20809
20810#ifdef FEAT_MBYTE
20811 if (!has_mbyte)
20812#endif
20813 /* not multi-byte: fromstr and tostr must be the same length */
20814 if (STRLEN(fromstr) != STRLEN(tostr))
20815 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020816#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020817error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020818#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020819 EMSG2(_(e_invarg2), fromstr);
20820 ga_clear(&ga);
20821 return;
20822 }
20823
20824 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020825 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020826 {
20827#ifdef FEAT_MBYTE
20828 if (has_mbyte)
20829 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020830 inlen = (*mb_ptr2len)(in_str);
20831 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020832 cplen = inlen;
20833 idx = 0;
20834 for (p = fromstr; *p != NUL; p += fromlen)
20835 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020836 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020837 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020838 {
20839 for (p = tostr; *p != NUL; p += tolen)
20840 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020841 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020842 if (idx-- == 0)
20843 {
20844 cplen = tolen;
20845 cpstr = p;
20846 break;
20847 }
20848 }
20849 if (*p == NUL) /* tostr is shorter than fromstr */
20850 goto error;
20851 break;
20852 }
20853 ++idx;
20854 }
20855
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020856 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020857 {
20858 /* Check that fromstr and tostr have the same number of
20859 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020860 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020861 first = FALSE;
20862 for (p = tostr; *p != NUL; p += tolen)
20863 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020864 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020865 --idx;
20866 }
20867 if (idx != 0)
20868 goto error;
20869 }
20870
Bram Moolenaarcde88542015-08-11 19:14:00 +020020871 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020872 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020873 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020874
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020875 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020876 }
20877 else
20878#endif
20879 {
20880 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020881 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020882 if (p != NULL)
20883 ga_append(&ga, tostr[p - fromstr]);
20884 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020885 ga_append(&ga, *in_str);
20886 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020887 }
20888 }
20889
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020890 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020891 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020892 ga_append(&ga, NUL);
20893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020895}
20896
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020897#ifdef FEAT_FLOAT
20898/*
20899 * "trunc({float})" function
20900 */
20901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020902f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020903{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020904 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020905
20906 rettv->v_type = VAR_FLOAT;
20907 if (get_float_arg(argvars, &f) == OK)
20908 /* trunc() is not in C90, use floor() or ceil() instead. */
20909 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20910 else
20911 rettv->vval.v_float = 0.0;
20912}
20913#endif
20914
Bram Moolenaar8299df92004-07-10 09:47:34 +000020915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916 * "type(expr)" function
20917 */
20918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020919f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020921 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020922
20923 switch (argvars[0].v_type)
20924 {
20925 case VAR_NUMBER: n = 0; break;
20926 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020927 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020928 case VAR_FUNC: n = 2; break;
20929 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020930 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020931 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020932 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020933 if (argvars[0].vval.v_number == VVAL_FALSE
20934 || argvars[0].vval.v_number == VVAL_TRUE)
20935 n = 6;
20936 else
20937 n = 7;
20938 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020939 case VAR_JOB: n = 8; break;
20940 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020941 case VAR_UNKNOWN:
20942 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20943 n = -1;
20944 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020945 }
20946 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947}
20948
20949/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020950 * "undofile(name)" function
20951 */
20952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020953f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020954{
20955 rettv->v_type = VAR_STRING;
20956#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020957 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020958 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020959
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020960 if (*fname == NUL)
20961 {
20962 /* If there is no file name there will be no undo file. */
20963 rettv->vval.v_string = NULL;
20964 }
20965 else
20966 {
20967 char_u *ffname = FullName_save(fname, FALSE);
20968
20969 if (ffname != NULL)
20970 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20971 vim_free(ffname);
20972 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020973 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020974#else
20975 rettv->vval.v_string = NULL;
20976#endif
20977}
20978
20979/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020980 * "undotree()" function
20981 */
20982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020983f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020984{
20985 if (rettv_dict_alloc(rettv) == OK)
20986 {
20987 dict_T *dict = rettv->vval.v_dict;
20988 list_T *list;
20989
Bram Moolenaar730cde92010-06-27 05:18:54 +020020990 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020991 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020992 dict_add_nr_str(dict, "save_last",
20993 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020994 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20995 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020996 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020997
20998 list = list_alloc();
20999 if (list != NULL)
21000 {
21001 u_eval_tree(curbuf->b_u_oldhead, list);
21002 dict_add_list(dict, "entries", list);
21003 }
21004 }
21005}
21006
21007/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021008 * "values(dict)" function
21009 */
21010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021011f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021012{
21013 dict_list(argvars, rettv, 1);
21014}
21015
21016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 * "virtcol(string)" function
21018 */
21019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021020f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021{
21022 colnr_T vcol = 0;
21023 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021024 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021026 fp = var2fpos(&argvars[0], FALSE, &fnum);
21027 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21028 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 {
21030 getvvcol(curwin, fp, NULL, NULL, &vcol);
21031 ++vcol;
21032 }
21033
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021034 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021035}
21036
21037/*
21038 * "visualmode()" function
21039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021041f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 char_u str[2];
21044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021045 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 str[0] = curbuf->b_visual_mode_eval;
21047 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021048 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021049
21050 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021051 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053}
21054
21055/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021056 * "wildmenumode()" function
21057 */
21058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021059f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021060{
21061#ifdef FEAT_WILDMENU
21062 if (wild_menu_showing)
21063 rettv->vval.v_number = 1;
21064#endif
21065}
21066
21067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 * "winbufnr(nr)" function
21069 */
21070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021071f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072{
21073 win_T *wp;
21074
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021075 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021079 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080}
21081
21082/*
21083 * "wincol()" function
21084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021086f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087{
21088 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090}
21091
21092/*
21093 * "winheight(nr)" function
21094 */
21095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021096f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097{
21098 win_T *wp;
21099
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021100 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021102 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021104 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105}
21106
21107/*
21108 * "winline()" function
21109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021111f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021112{
21113 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021114 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115}
21116
21117/*
21118 * "winnr()" function
21119 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021121f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122{
21123 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021124
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021126 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021128 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129}
21130
21131/*
21132 * "winrestcmd()" function
21133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021135f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136{
21137#ifdef FEAT_WINDOWS
21138 win_T *wp;
21139 int winnr = 1;
21140 garray_T ga;
21141 char_u buf[50];
21142
21143 ga_init2(&ga, (int)sizeof(char), 70);
21144 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21145 {
21146 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21147 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021148 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21149 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150 ++winnr;
21151 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021152 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021153
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021154 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021156 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021158 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021159}
21160
21161/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021162 * "winrestview()" function
21163 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021165f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021166{
21167 dict_T *dict;
21168
21169 if (argvars[0].v_type != VAR_DICT
21170 || (dict = argvars[0].vval.v_dict) == NULL)
21171 EMSG(_(e_invarg));
21172 else
21173 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021174 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21175 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21176 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21177 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021178#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021179 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21180 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021181#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021182 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21183 {
21184 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21185 curwin->w_set_curswant = FALSE;
21186 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021187
Bram Moolenaar82c25852014-05-28 16:47:16 +020021188 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21189 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021190#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021191 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21192 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021193#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021194 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21195 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21196 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21197 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021198
21199 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021200 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021201# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021202 win_new_width(curwin, W_WIDTH(curwin));
21203# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021204 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021205
Bram Moolenaarb851a962014-10-31 15:45:52 +010021206 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021207 curwin->w_topline = 1;
21208 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21209 curwin->w_topline = curbuf->b_ml.ml_line_count;
21210#ifdef FEAT_DIFF
21211 check_topfill(curwin, TRUE);
21212#endif
21213 }
21214}
21215
21216/*
21217 * "winsaveview()" function
21218 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021220f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021221{
21222 dict_T *dict;
21223
Bram Moolenaara800b422010-06-27 01:15:55 +020021224 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021225 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021226 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021227
21228 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21229 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21230#ifdef FEAT_VIRTUALEDIT
21231 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21232#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021233 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021234 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21235
21236 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21237#ifdef FEAT_DIFF
21238 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21239#endif
21240 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21241 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21242}
21243
21244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 * "winwidth(nr)" function
21246 */
21247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021248f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249{
21250 win_T *wp;
21251
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021252 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021256#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021257 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021259 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260#endif
21261}
21262
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021264 * "wordcount()" function
21265 */
21266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021267f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021268{
21269 if (rettv_dict_alloc(rettv) == FAIL)
21270 return;
21271 cursor_pos_info(rettv->vval.v_dict);
21272}
21273
21274/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021275 * Write list of strings to file
21276 */
21277 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021278write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021279{
21280 listitem_T *li;
21281 int c;
21282 int ret = OK;
21283 char_u *s;
21284
21285 for (li = list->lv_first; li != NULL; li = li->li_next)
21286 {
21287 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21288 {
21289 if (*s == '\n')
21290 c = putc(NUL, fd);
21291 else
21292 c = putc(*s, fd);
21293 if (c == EOF)
21294 {
21295 ret = FAIL;
21296 break;
21297 }
21298 }
21299 if (!binary || li->li_next != NULL)
21300 if (putc('\n', fd) == EOF)
21301 {
21302 ret = FAIL;
21303 break;
21304 }
21305 if (ret == FAIL)
21306 {
21307 EMSG(_(e_write));
21308 break;
21309 }
21310 }
21311 return ret;
21312}
21313
21314/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021315 * "writefile()" function
21316 */
21317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021318f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021319{
21320 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021321 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021322 char_u *fname;
21323 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021324 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021325
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021326 if (check_restricted() || check_secure())
21327 return;
21328
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021329 if (argvars[0].v_type != VAR_LIST)
21330 {
21331 EMSG2(_(e_listarg), "writefile()");
21332 return;
21333 }
21334 if (argvars[0].vval.v_list == NULL)
21335 return;
21336
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021337 if (argvars[2].v_type != VAR_UNKNOWN)
21338 {
21339 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21340 binary = TRUE;
21341 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21342 append = TRUE;
21343 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021344
21345 /* Always open the file in binary mode, library functions have a mind of
21346 * their own about CR-LF conversion. */
21347 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021348 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21349 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021350 {
21351 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21352 ret = -1;
21353 }
21354 else
21355 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021356 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21357 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021358 fclose(fd);
21359 }
21360
21361 rettv->vval.v_number = ret;
21362}
21363
21364/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021365 * "xor(expr, expr)" function
21366 */
21367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021368f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021369{
21370 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21371 ^ get_tv_number_chk(&argvars[1], NULL);
21372}
21373
21374
21375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021377 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021378 */
21379 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021380var2fpos(
21381 typval_T *varp,
21382 int dollar_lnum, /* TRUE when $ is last line */
21383 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021385 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021387 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388
Bram Moolenaara5525202006-03-02 22:52:09 +000021389 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021390 if (varp->v_type == VAR_LIST)
21391 {
21392 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021393 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021394 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021395 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021396
21397 l = varp->vval.v_list;
21398 if (l == NULL)
21399 return NULL;
21400
21401 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021402 pos.lnum = list_find_nr(l, 0L, &error);
21403 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021404 return NULL; /* invalid line number */
21405
21406 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021407 pos.col = list_find_nr(l, 1L, &error);
21408 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021409 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021410 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021411
21412 /* We accept "$" for the column number: last column. */
21413 li = list_find(l, 1L);
21414 if (li != NULL && li->li_tv.v_type == VAR_STRING
21415 && li->li_tv.vval.v_string != NULL
21416 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21417 pos.col = len + 1;
21418
Bram Moolenaara5525202006-03-02 22:52:09 +000021419 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021420 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021421 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021422 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021423
Bram Moolenaara5525202006-03-02 22:52:09 +000021424#ifdef FEAT_VIRTUALEDIT
21425 /* Get the virtual offset. Defaults to zero. */
21426 pos.coladd = list_find_nr(l, 2L, &error);
21427 if (error)
21428 pos.coladd = 0;
21429#endif
21430
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021431 return &pos;
21432 }
21433
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021434 name = get_tv_string_chk(varp);
21435 if (name == NULL)
21436 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021437 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021438 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021439 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21440 {
21441 if (VIsual_active)
21442 return &VIsual;
21443 return &curwin->w_cursor;
21444 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021445 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021447 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21449 return NULL;
21450 return pp;
21451 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021452
21453#ifdef FEAT_VIRTUALEDIT
21454 pos.coladd = 0;
21455#endif
21456
Bram Moolenaar477933c2007-07-17 14:32:23 +000021457 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021458 {
21459 pos.col = 0;
21460 if (name[1] == '0') /* "w0": first visible line */
21461 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021462 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021463 pos.lnum = curwin->w_topline;
21464 return &pos;
21465 }
21466 else if (name[1] == '$') /* "w$": last visible line */
21467 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021468 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021469 pos.lnum = curwin->w_botline - 1;
21470 return &pos;
21471 }
21472 }
21473 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021475 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 {
21477 pos.lnum = curbuf->b_ml.ml_line_count;
21478 pos.col = 0;
21479 }
21480 else
21481 {
21482 pos.lnum = curwin->w_cursor.lnum;
21483 pos.col = (colnr_T)STRLEN(ml_get_curline());
21484 }
21485 return &pos;
21486 }
21487 return NULL;
21488}
21489
21490/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021491 * Convert list in "arg" into a position and optional file number.
21492 * When "fnump" is NULL there is no file number, only 3 items.
21493 * Note that the column is passed on as-is, the caller may want to decrement
21494 * it to use 1 for the first column.
21495 * Return FAIL when conversion is not possible, doesn't check the position for
21496 * validity.
21497 */
21498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021499list2fpos(
21500 typval_T *arg,
21501 pos_T *posp,
21502 int *fnump,
21503 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021504{
21505 list_T *l = arg->vval.v_list;
21506 long i = 0;
21507 long n;
21508
Bram Moolenaar493c1782014-05-28 14:34:46 +020021509 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21510 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021511 if (arg->v_type != VAR_LIST
21512 || l == NULL
21513 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021514 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021515 return FAIL;
21516
21517 if (fnump != NULL)
21518 {
21519 n = list_find_nr(l, i++, NULL); /* fnum */
21520 if (n < 0)
21521 return FAIL;
21522 if (n == 0)
21523 n = curbuf->b_fnum; /* current buffer */
21524 *fnump = n;
21525 }
21526
21527 n = list_find_nr(l, i++, NULL); /* lnum */
21528 if (n < 0)
21529 return FAIL;
21530 posp->lnum = n;
21531
21532 n = list_find_nr(l, i++, NULL); /* col */
21533 if (n < 0)
21534 return FAIL;
21535 posp->col = n;
21536
21537#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021538 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021539 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021540 posp->coladd = 0;
21541 else
21542 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021543#endif
21544
Bram Moolenaar493c1782014-05-28 14:34:46 +020021545 if (curswantp != NULL)
21546 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21547
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021548 return OK;
21549}
21550
21551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 * Get the length of an environment variable name.
21553 * Advance "arg" to the first character after the name.
21554 * Return 0 for error.
21555 */
21556 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021557get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021558{
21559 char_u *p;
21560 int len;
21561
21562 for (p = *arg; vim_isIDc(*p); ++p)
21563 ;
21564 if (p == *arg) /* no name found */
21565 return 0;
21566
21567 len = (int)(p - *arg);
21568 *arg = p;
21569 return len;
21570}
21571
21572/*
21573 * Get the length of the name of a function or internal variable.
21574 * "arg" is advanced to the first non-white character after the name.
21575 * Return 0 if something is wrong.
21576 */
21577 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021578get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579{
21580 char_u *p;
21581 int len;
21582
21583 /* Find the end of the name. */
21584 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021585 {
21586 if (*p == ':')
21587 {
21588 /* "s:" is start of "s:var", but "n:" is not and can be used in
21589 * slice "[n:]". Also "xx:" is not a namespace. */
21590 len = (int)(p - *arg);
21591 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21592 || len > 1)
21593 break;
21594 }
21595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021596 if (p == *arg) /* no name found */
21597 return 0;
21598
21599 len = (int)(p - *arg);
21600 *arg = skipwhite(p);
21601
21602 return len;
21603}
21604
21605/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021606 * Get the length of the name of a variable or function.
21607 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021609 * Return -1 if curly braces expansion failed.
21610 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611 * If the name contains 'magic' {}'s, expand them and return the
21612 * expanded name in an allocated string via 'alias' - caller must free.
21613 */
21614 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021615get_name_len(
21616 char_u **arg,
21617 char_u **alias,
21618 int evaluate,
21619 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620{
21621 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 char_u *p;
21623 char_u *expr_start;
21624 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625
21626 *alias = NULL; /* default to no alias */
21627
21628 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21629 && (*arg)[2] == (int)KE_SNR)
21630 {
21631 /* hard coded <SNR>, already translated */
21632 *arg += 3;
21633 return get_id_len(arg) + 3;
21634 }
21635 len = eval_fname_script(*arg);
21636 if (len > 0)
21637 {
21638 /* literal "<SID>", "s:" or "<SNR>" */
21639 *arg += len;
21640 }
21641
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021643 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021644 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021645 p = find_name_end(*arg, &expr_start, &expr_end,
21646 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021647 if (expr_start != NULL)
21648 {
21649 char_u *temp_string;
21650
21651 if (!evaluate)
21652 {
21653 len += (int)(p - *arg);
21654 *arg = skipwhite(p);
21655 return len;
21656 }
21657
21658 /*
21659 * Include any <SID> etc in the expanded string:
21660 * Thus the -len here.
21661 */
21662 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21663 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021664 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 *alias = temp_string;
21666 *arg = skipwhite(p);
21667 return (int)STRLEN(temp_string);
21668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669
21670 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021671 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672 EMSG2(_(e_invexpr2), *arg);
21673
21674 return len;
21675}
21676
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021677/*
21678 * Find the end of a variable or function name, taking care of magic braces.
21679 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21680 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021681 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021682 * Return a pointer to just after the name. Equal to "arg" if there is no
21683 * valid name.
21684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021686find_name_end(
21687 char_u *arg,
21688 char_u **expr_start,
21689 char_u **expr_end,
21690 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021692 int mb_nest = 0;
21693 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021695 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021697 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021699 *expr_start = NULL;
21700 *expr_end = NULL;
21701 }
21702
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021703 /* Quick check for valid starting character. */
21704 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21705 return arg;
21706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021707 for (p = arg; *p != NUL
21708 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021709 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021710 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021711 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021712 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021713 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021714 if (*p == '\'')
21715 {
21716 /* skip over 'string' to avoid counting [ and ] inside it. */
21717 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21718 ;
21719 if (*p == NUL)
21720 break;
21721 }
21722 else if (*p == '"')
21723 {
21724 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21725 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21726 if (*p == '\\' && p[1] != NUL)
21727 ++p;
21728 if (*p == NUL)
21729 break;
21730 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021731 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21732 {
21733 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021734 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021735 len = (int)(p - arg);
21736 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021737 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021738 break;
21739 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021741 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021743 if (*p == '[')
21744 ++br_nest;
21745 else if (*p == ']')
21746 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021749 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021751 if (*p == '{')
21752 {
21753 mb_nest++;
21754 if (expr_start != NULL && *expr_start == NULL)
21755 *expr_start = p;
21756 }
21757 else if (*p == '}')
21758 {
21759 mb_nest--;
21760 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21761 *expr_end = p;
21762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764 }
21765
21766 return p;
21767}
21768
21769/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021770 * Expands out the 'magic' {}'s in a variable/function name.
21771 * Note that this can call itself recursively, to deal with
21772 * constructs like foo{bar}{baz}{bam}
21773 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21774 * "in_start" ^
21775 * "expr_start" ^
21776 * "expr_end" ^
21777 * "in_end" ^
21778 *
21779 * Returns a new allocated string, which the caller must free.
21780 * Returns NULL for failure.
21781 */
21782 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021783make_expanded_name(
21784 char_u *in_start,
21785 char_u *expr_start,
21786 char_u *expr_end,
21787 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021788{
21789 char_u c1;
21790 char_u *retval = NULL;
21791 char_u *temp_result;
21792 char_u *nextcmd = NULL;
21793
21794 if (expr_end == NULL || in_end == NULL)
21795 return NULL;
21796 *expr_start = NUL;
21797 *expr_end = NUL;
21798 c1 = *in_end;
21799 *in_end = NUL;
21800
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021801 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021802 if (temp_result != NULL && nextcmd == NULL)
21803 {
21804 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21805 + (in_end - expr_end) + 1));
21806 if (retval != NULL)
21807 {
21808 STRCPY(retval, in_start);
21809 STRCAT(retval, temp_result);
21810 STRCAT(retval, expr_end + 1);
21811 }
21812 }
21813 vim_free(temp_result);
21814
21815 *in_end = c1; /* put char back for error messages */
21816 *expr_start = '{';
21817 *expr_end = '}';
21818
21819 if (retval != NULL)
21820 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021821 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021822 if (expr_start != NULL)
21823 {
21824 /* Further expansion! */
21825 temp_result = make_expanded_name(retval, expr_start,
21826 expr_end, temp_result);
21827 vim_free(retval);
21828 retval = temp_result;
21829 }
21830 }
21831
21832 return retval;
21833}
21834
21835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021837 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 */
21839 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021840eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021842 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21843}
21844
21845/*
21846 * Return TRUE if character "c" can be used as the first character in a
21847 * variable or function name (excluding '{' and '}').
21848 */
21849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021850eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021851{
21852 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853}
21854
21855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 * Set number v: variable to "val".
21857 */
21858 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021859set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021860{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021861 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021862}
21863
21864/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021865 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021866 */
21867 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021868get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021870 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871}
21872
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021873/*
21874 * Get string v: variable value. Uses a static buffer, can only be used once.
21875 */
21876 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021877get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021878{
21879 return get_tv_string(&vimvars[idx].vv_tv);
21880}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021881
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021883 * Get List v: variable value. Caller must take care of reference count when
21884 * needed.
21885 */
21886 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021887get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021888{
21889 return vimvars[idx].vv_list;
21890}
21891
21892/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021893 * Set v:char to character "c".
21894 */
21895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021896set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021897{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021898 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021899
21900#ifdef FEAT_MBYTE
21901 if (has_mbyte)
21902 buf[(*mb_char2bytes)(c, buf)] = NUL;
21903 else
21904#endif
21905 {
21906 buf[0] = c;
21907 buf[1] = NUL;
21908 }
21909 set_vim_var_string(VV_CHAR, buf, -1);
21910}
21911
21912/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021913 * Set v:count to "count" and v:count1 to "count1".
21914 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021915 */
21916 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021917set_vcount(
21918 long count,
21919 long count1,
21920 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021922 if (set_prevcount)
21923 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021924 vimvars[VV_COUNT].vv_nr = count;
21925 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926}
21927
21928/*
21929 * Set string v: variable to a copy of "val".
21930 */
21931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932set_vim_var_string(
21933 int idx,
21934 char_u *val,
21935 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936{
Bram Moolenaara542c682016-01-31 16:28:04 +010021937 clear_tv(&vimvars[idx].vv_di.di_tv);
21938 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021939 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021940 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021942 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021944 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945}
21946
21947/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021948 * Set List v: variable to "val".
21949 */
21950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021952{
Bram Moolenaara542c682016-01-31 16:28:04 +010021953 clear_tv(&vimvars[idx].vv_di.di_tv);
21954 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021955 vimvars[idx].vv_list = val;
21956 if (val != NULL)
21957 ++val->lv_refcount;
21958}
21959
21960/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021961 * Set Dictionary v: variable to "val".
21962 */
21963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021964set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021965{
21966 int todo;
21967 hashitem_T *hi;
21968
Bram Moolenaara542c682016-01-31 16:28:04 +010021969 clear_tv(&vimvars[idx].vv_di.di_tv);
21970 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021971 vimvars[idx].vv_dict = val;
21972 if (val != NULL)
21973 {
21974 ++val->dv_refcount;
21975
21976 /* Set readonly */
21977 todo = (int)val->dv_hashtab.ht_used;
21978 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21979 {
21980 if (HASHITEM_EMPTY(hi))
21981 continue;
21982 --todo;
21983 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21984 }
21985 }
21986}
21987
21988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 * Set v:register if needed.
21990 */
21991 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021992set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021993{
21994 char_u regname;
21995
21996 if (c == 0 || c == ' ')
21997 regname = '"';
21998 else
21999 regname = c;
22000 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022001 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002 set_vim_var_string(VV_REG, &regname, 1);
22003}
22004
22005/*
22006 * Get or set v:exception. If "oldval" == NULL, return the current value.
22007 * Otherwise, restore the value to "oldval" and return NULL.
22008 * Must always be called in pairs to save and restore v:exception! Does not
22009 * take care of memory allocations.
22010 */
22011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022012v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013{
22014 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022015 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016
Bram Moolenaare9a41262005-01-15 22:18:47 +000022017 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018 return NULL;
22019}
22020
22021/*
22022 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22023 * Otherwise, restore the value to "oldval" and return NULL.
22024 * Must always be called in pairs to save and restore v:throwpoint! Does not
22025 * take care of memory allocations.
22026 */
22027 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022028v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022029{
22030 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022031 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032
Bram Moolenaare9a41262005-01-15 22:18:47 +000022033 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022034 return NULL;
22035}
22036
22037#if defined(FEAT_AUTOCMD) || defined(PROTO)
22038/*
22039 * Set v:cmdarg.
22040 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22041 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22042 * Must always be called in pairs!
22043 */
22044 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022045set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046{
22047 char_u *oldval;
22048 char_u *newval;
22049 unsigned len;
22050
Bram Moolenaare9a41262005-01-15 22:18:47 +000022051 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022052 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022054 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022055 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022056 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 }
22058
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022059 if (eap->force_bin == FORCE_BIN)
22060 len = 6;
22061 else if (eap->force_bin == FORCE_NOBIN)
22062 len = 8;
22063 else
22064 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022065
22066 if (eap->read_edit)
22067 len += 7;
22068
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022069 if (eap->force_ff != 0)
22070 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22071# ifdef FEAT_MBYTE
22072 if (eap->force_enc != 0)
22073 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022074 if (eap->bad_char != 0)
22075 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022076# endif
22077
22078 newval = alloc(len + 1);
22079 if (newval == NULL)
22080 return NULL;
22081
22082 if (eap->force_bin == FORCE_BIN)
22083 sprintf((char *)newval, " ++bin");
22084 else if (eap->force_bin == FORCE_NOBIN)
22085 sprintf((char *)newval, " ++nobin");
22086 else
22087 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022088
22089 if (eap->read_edit)
22090 STRCAT(newval, " ++edit");
22091
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022092 if (eap->force_ff != 0)
22093 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22094 eap->cmd + eap->force_ff);
22095# ifdef FEAT_MBYTE
22096 if (eap->force_enc != 0)
22097 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22098 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022099 if (eap->bad_char == BAD_KEEP)
22100 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22101 else if (eap->bad_char == BAD_DROP)
22102 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22103 else if (eap->bad_char != 0)
22104 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022105# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022106 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022107 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108}
22109#endif
22110
22111/*
22112 * Get the value of internal variable "name".
22113 * Return OK or FAIL.
22114 */
22115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022116get_var_tv(
22117 char_u *name,
22118 int len, /* length of "name" */
22119 typval_T *rettv, /* NULL when only checking existence */
22120 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22121 int verbose, /* may give error message */
22122 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123{
22124 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022125 typval_T *tv = NULL;
22126 typval_T atv;
22127 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022128 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129
22130 /* truncate the name, so that we can use strcmp() */
22131 cc = name[len];
22132 name[len] = NUL;
22133
22134 /*
22135 * Check for "b:changedtick".
22136 */
22137 if (STRCMP(name, "b:changedtick") == 0)
22138 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022139 atv.v_type = VAR_NUMBER;
22140 atv.vval.v_number = curbuf->b_changedtick;
22141 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 }
22143
22144 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022145 * Check for user-defined variables.
22146 */
22147 else
22148 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022149 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022150 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022151 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022152 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022153 if (dip != NULL)
22154 *dip = v;
22155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 }
22157
Bram Moolenaare9a41262005-01-15 22:18:47 +000022158 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022160 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022161 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 ret = FAIL;
22163 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022164 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022165 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166
22167 name[len] = cc;
22168
22169 return ret;
22170}
22171
22172/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022173 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22174 * Also handle function call with Funcref variable: func(expr)
22175 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22176 */
22177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022178handle_subscript(
22179 char_u **arg,
22180 typval_T *rettv,
22181 int evaluate, /* do more than finding the end */
22182 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022183{
22184 int ret = OK;
22185 dict_T *selfdict = NULL;
22186 char_u *s;
22187 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022188 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022189
22190 while (ret == OK
22191 && (**arg == '['
22192 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022193 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22194 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022195 && !vim_iswhite(*(*arg - 1)))
22196 {
22197 if (**arg == '(')
22198 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022199 partial_T *pt = NULL;
22200
Bram Moolenaard9fba312005-06-26 22:34:35 +000022201 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022202 if (evaluate)
22203 {
22204 functv = *rettv;
22205 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022206
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022207 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022208 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022209 {
22210 pt = functv.vval.v_partial;
22211 s = pt->pt_name;
22212 }
22213 else
22214 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022215 }
22216 else
22217 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022218 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022219 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022220 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022221
22222 /* Clear the funcref afterwards, so that deleting it while
22223 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022224 if (evaluate)
22225 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022226
22227 /* Stop the expression evaluation when immediately aborting on
22228 * error, or when an interrupt occurred or an exception was thrown
22229 * but not caught. */
22230 if (aborting())
22231 {
22232 if (ret == OK)
22233 clear_tv(rettv);
22234 ret = FAIL;
22235 }
22236 dict_unref(selfdict);
22237 selfdict = NULL;
22238 }
22239 else /* **arg == '[' || **arg == '.' */
22240 {
22241 dict_unref(selfdict);
22242 if (rettv->v_type == VAR_DICT)
22243 {
22244 selfdict = rettv->vval.v_dict;
22245 if (selfdict != NULL)
22246 ++selfdict->dv_refcount;
22247 }
22248 else
22249 selfdict = NULL;
22250 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22251 {
22252 clear_tv(rettv);
22253 ret = FAIL;
22254 }
22255 }
22256 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022257
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022258 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
22259 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022260 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022261 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22262 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022263 char_u *tofree = NULL;
22264 ufunc_T *fp;
22265 char_u fname_buf[FLEN_FIXED + 1];
22266 int error;
22267
22268 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022269 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022270 fp = find_func(fname);
22271 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022272
22273 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010022274 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022275 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022276 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22277
22278 if (pt != NULL)
22279 {
22280 pt->pt_refcount = 1;
22281 pt->pt_dict = selfdict;
22282 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022283 if (rettv->v_type == VAR_FUNC)
22284 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022285 /* Just a function: Take over the function name and use
22286 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022287 pt->pt_name = rettv->vval.v_string;
22288 }
22289 else
22290 {
22291 partial_T *ret_pt = rettv->vval.v_partial;
22292 int i;
22293
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022294 /* Partial: copy the function name, use selfdict and copy
22295 * args. Can't take over name or args, the partial might
22296 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022297 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022298 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022299 if (ret_pt->pt_argc > 0)
22300 {
22301 pt->pt_argv = (typval_T *)alloc(
22302 sizeof(typval_T) * ret_pt->pt_argc);
22303 if (pt->pt_argv == NULL)
22304 /* out of memory: drop the arguments */
22305 pt->pt_argc = 0;
22306 else
22307 {
22308 pt->pt_argc = ret_pt->pt_argc;
22309 for (i = 0; i < pt->pt_argc; i++)
22310 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22311 }
22312 }
22313 partial_unref(ret_pt);
22314 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022315 rettv->v_type = VAR_PARTIAL;
22316 rettv->vval.v_partial = pt;
22317 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022318 }
22319 }
22320
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022321 dict_unref(selfdict);
22322 return ret;
22323}
22324
22325/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022326 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022327 * value).
22328 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022329 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022330alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022331{
Bram Moolenaar33570922005-01-25 22:26:29 +000022332 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022333}
22334
22335/*
22336 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 * The string "s" must have been allocated, it is consumed.
22338 * Return NULL for out of memory, the variable otherwise.
22339 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022340 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022341alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342{
Bram Moolenaar33570922005-01-25 22:26:29 +000022343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022345 rettv = alloc_tv();
22346 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022348 rettv->v_type = VAR_STRING;
22349 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350 }
22351 else
22352 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022353 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354}
22355
22356/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022357 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022360free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361{
22362 if (varp != NULL)
22363 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022364 switch (varp->v_type)
22365 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022367 func_unref(varp->vval.v_string);
22368 /*FALLTHROUGH*/
22369 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022370 vim_free(varp->vval.v_string);
22371 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022372 case VAR_PARTIAL:
22373 partial_unref(varp->vval.v_partial);
22374 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022375 case VAR_LIST:
22376 list_unref(varp->vval.v_list);
22377 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022378 case VAR_DICT:
22379 dict_unref(varp->vval.v_dict);
22380 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022381 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022382#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022383 job_unref(varp->vval.v_job);
22384 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022385#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022386 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022387#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022388 channel_unref(varp->vval.v_channel);
22389 break;
22390#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022391 case VAR_NUMBER:
22392 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022393 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022394 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022395 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397 vim_free(varp);
22398 }
22399}
22400
22401/*
22402 * Free the memory for a variable value and set the value to NULL or 0.
22403 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022404 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022405clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406{
22407 if (varp != NULL)
22408 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022409 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022411 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022412 func_unref(varp->vval.v_string);
22413 /*FALLTHROUGH*/
22414 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022415 vim_free(varp->vval.v_string);
22416 varp->vval.v_string = NULL;
22417 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022418 case VAR_PARTIAL:
22419 partial_unref(varp->vval.v_partial);
22420 varp->vval.v_partial = NULL;
22421 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022422 case VAR_LIST:
22423 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022424 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022425 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022426 case VAR_DICT:
22427 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022428 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022429 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022430 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022431 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022432 varp->vval.v_number = 0;
22433 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022434 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022435#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022436 varp->vval.v_float = 0.0;
22437 break;
22438#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022439 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022440#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022441 job_unref(varp->vval.v_job);
22442 varp->vval.v_job = NULL;
22443#endif
22444 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022445 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022446#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022447 channel_unref(varp->vval.v_channel);
22448 varp->vval.v_channel = NULL;
22449#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022450 case VAR_UNKNOWN:
22451 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022452 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022453 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 }
22455}
22456
22457/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022458 * Set the value of a variable to NULL without freeing items.
22459 */
22460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022461init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022462{
22463 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022464 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022465}
22466
22467/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 * Get the number value of a variable.
22469 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022470 * For incompatible types, return 0.
22471 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22472 * caller of incompatible types: it sets *denote to TRUE if "denote"
22473 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022475 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022476get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022478 int error = FALSE;
22479
22480 return get_tv_number_chk(varp, &error); /* return 0L on error */
22481}
22482
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022483 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022484get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022485{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022488 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022490 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022491 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022492 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022493#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022494 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022495 break;
22496#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022497 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022498 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022499 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022500 break;
22501 case VAR_STRING:
22502 if (varp->vval.v_string != NULL)
22503 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022504 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022505 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022506 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022507 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022508 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022509 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022510 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022511 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022512 case VAR_SPECIAL:
22513 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22514 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022515 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022516#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022517 EMSG(_("E910: Using a Job as a Number"));
22518 break;
22519#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022520 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022521#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022522 EMSG(_("E913: Using a Channel as a Number"));
22523 break;
22524#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022525 case VAR_UNKNOWN:
22526 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022528 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022529 if (denote == NULL) /* useful for values that must be unsigned */
22530 n = -1;
22531 else
22532 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022533 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534}
22535
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022536#ifdef FEAT_FLOAT
22537 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022538get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022539{
22540 switch (varp->v_type)
22541 {
22542 case VAR_NUMBER:
22543 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022544 case VAR_FLOAT:
22545 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022546 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022547 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022548 EMSG(_("E891: Using a Funcref as a Float"));
22549 break;
22550 case VAR_STRING:
22551 EMSG(_("E892: Using a String as a Float"));
22552 break;
22553 case VAR_LIST:
22554 EMSG(_("E893: Using a List as a Float"));
22555 break;
22556 case VAR_DICT:
22557 EMSG(_("E894: Using a Dictionary as a Float"));
22558 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022559 case VAR_SPECIAL:
22560 EMSG(_("E907: Using a special value as a Float"));
22561 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022562 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022563# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022564 EMSG(_("E911: Using a Job as a Float"));
22565 break;
22566# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022567 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022568# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022569 EMSG(_("E914: Using a Channel as a Float"));
22570 break;
22571# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022572 case VAR_UNKNOWN:
22573 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022574 break;
22575 }
22576 return 0;
22577}
22578#endif
22579
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022581 * Get the lnum from the first argument.
22582 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022583 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 */
22585 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022586get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587{
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589 linenr_T lnum;
22590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022591 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592 if (lnum == 0) /* no valid number, try using line() */
22593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022594 rettv.v_type = VAR_NUMBER;
22595 f_line(argvars, &rettv);
22596 lnum = rettv.vval.v_number;
22597 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022598 }
22599 return lnum;
22600}
22601
22602/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022603 * Get the lnum from the first argument.
22604 * Also accepts "$", then "buf" is used.
22605 * Returns 0 on error.
22606 */
22607 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022608get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022609{
22610 if (argvars[0].v_type == VAR_STRING
22611 && argvars[0].vval.v_string != NULL
22612 && argvars[0].vval.v_string[0] == '$'
22613 && buf != NULL)
22614 return buf->b_ml.ml_line_count;
22615 return get_tv_number_chk(&argvars[0], NULL);
22616}
22617
22618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022619 * Get the string value of a variable.
22620 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022621 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22622 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022623 * If the String variable has never been set, return an empty string.
22624 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022625 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22626 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022627 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022629get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630{
22631 static char_u mybuf[NUMBUFLEN];
22632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022633 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634}
22635
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022636 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022637get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022639 char_u *res = get_tv_string_buf_chk(varp, buf);
22640
22641 return res != NULL ? res : (char_u *)"";
22642}
22643
Bram Moolenaar7d647822014-04-05 21:28:56 +020022644/*
22645 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22646 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022647 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022648get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022649{
22650 static char_u mybuf[NUMBUFLEN];
22651
22652 return get_tv_string_buf_chk(varp, mybuf);
22653}
22654
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022656get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022657{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022658 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022660 case VAR_NUMBER:
22661 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22662 return buf;
22663 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022664 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022665 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666 break;
22667 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022668 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022669 break;
22670 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022671 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022672 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022673 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022674#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022675 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022676 break;
22677#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022678 case VAR_STRING:
22679 if (varp->vval.v_string != NULL)
22680 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022681 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022682 case VAR_SPECIAL:
22683 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22684 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022685 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022686#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022687 {
22688 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022689 char *status;
22690
22691 if (job == NULL)
22692 return (char_u *)"no process";
22693 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022694 : job->jv_status == JOB_ENDED ? "dead"
22695 : "run";
22696# ifdef UNIX
22697 vim_snprintf((char *)buf, NUMBUFLEN,
22698 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022699# elif defined(WIN32)
22700 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022701 "process %ld %s",
22702 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022703 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022704# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022705 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022706 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22707# endif
22708 return buf;
22709 }
22710#endif
22711 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022712 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022713#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022714 {
22715 channel_T *channel = varp->vval.v_channel;
22716 char *status = channel_status(channel);
22717
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022718 if (channel == NULL)
22719 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22720 else
22721 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022722 "channel %d %s", channel->ch_id, status);
22723 return buf;
22724 }
22725#endif
22726 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022727 case VAR_UNKNOWN:
22728 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022731 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732}
22733
22734/*
22735 * Find variable "name" in the list of variables.
22736 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022737 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022738 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022739 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022740 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022741 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022742find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746
Bram Moolenaara7043832005-01-21 11:56:39 +000022747 ht = find_var_ht(name, &varname);
22748 if (htp != NULL)
22749 *htp = ht;
22750 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022751 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022752 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753}
22754
22755/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022756 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022757 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022758 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022760find_var_in_ht(
22761 hashtab_T *ht,
22762 int htname,
22763 char_u *varname,
22764 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022765{
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 hashitem_T *hi;
22767
22768 if (*varname == NUL)
22769 {
22770 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022771 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022773 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022774 case 'g': return &globvars_var;
22775 case 'v': return &vimvars_var;
22776 case 'b': return &curbuf->b_bufvar;
22777 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022778#ifdef FEAT_WINDOWS
22779 case 't': return &curtab->tp_winvar;
22780#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022781 case 'l': return current_funccal == NULL
22782 ? NULL : &current_funccal->l_vars_var;
22783 case 'a': return current_funccal == NULL
22784 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022785 }
22786 return NULL;
22787 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022788
22789 hi = hash_find(ht, varname);
22790 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022791 {
22792 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022793 * worked find the variable again. Don't auto-load a script if it was
22794 * loaded already, otherwise it would be loaded every time when
22795 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022796 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022797 {
22798 /* Note: script_autoload() may make "hi" invalid. It must either
22799 * be obtained again or not used. */
22800 if (!script_autoload(varname, FALSE) || aborting())
22801 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022802 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022803 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022804 if (HASHITEM_EMPTY(hi))
22805 return NULL;
22806 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022808}
22809
22810/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022811 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022812 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022813 * Set "varname" to the start of name without ':'.
22814 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022815 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022816find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022818 hashitem_T *hi;
22819
Bram Moolenaar73627d02015-08-11 15:46:09 +020022820 if (name[0] == NUL)
22821 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 if (name[1] != ':')
22823 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022824 /* The name must not start with a colon or #. */
22825 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 return NULL;
22827 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022828
22829 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022830 hi = hash_find(&compat_hashtab, name);
22831 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022832 return &compat_hashtab;
22833
Bram Moolenaar071d4272004-06-13 20:20:40 +000022834 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022836 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 }
22838 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022839 if (*name == 'g') /* global variable */
22840 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022841 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22842 */
22843 if (vim_strchr(name + 2, ':') != NULL
22844 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022845 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022847 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022849 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022850#ifdef FEAT_WINDOWS
22851 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022852 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022853#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 if (*name == 'v') /* v: variable */
22855 return &vimvarht;
22856 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022857 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022858 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022859 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 if (*name == 's' /* script variable */
22861 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22862 return &SCRIPT_VARS(current_SID);
22863 return NULL;
22864}
22865
22866/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022867 * Get function call environment based on bactrace debug level
22868 */
22869 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022870get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022871{
22872 int i;
22873 funccall_T *funccal;
22874 funccall_T *temp_funccal;
22875
22876 funccal = current_funccal;
22877 if (debug_backtrace_level > 0)
22878 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022879 for (i = 0; i < debug_backtrace_level; i++)
22880 {
22881 temp_funccal = funccal->caller;
22882 if (temp_funccal)
22883 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022884 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022885 /* backtrace level overflow. reset to max */
22886 debug_backtrace_level = i;
22887 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022888 }
22889 return funccal;
22890}
22891
22892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022893 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022894 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022895 * Returns NULL when it doesn't exist.
22896 */
22897 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022898get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899{
Bram Moolenaar33570922005-01-25 22:26:29 +000022900 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022902 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 if (v == NULL)
22904 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022905 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906}
22907
22908/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910 * sourcing this script and when executing functions defined in the script.
22911 */
22912 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022913new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914{
Bram Moolenaara7043832005-01-21 11:56:39 +000022915 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022916 hashtab_T *ht;
22917 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022918
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22920 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022921 /* Re-allocating ga_data means that an ht_array pointing to
22922 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022923 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022924 for (i = 1; i <= ga_scripts.ga_len; ++i)
22925 {
22926 ht = &SCRIPT_VARS(i);
22927 if (ht->ht_mask == HT_INIT_SIZE - 1)
22928 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022929 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022930 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022931 }
22932
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933 while (ga_scripts.ga_len < id)
22934 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022935 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022936 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022937 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022938 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939 }
22940 }
22941}
22942
22943/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022944 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22945 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022946 */
22947 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022948init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949{
Bram Moolenaar33570922005-01-25 22:26:29 +000022950 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022951 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022952 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022953 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022954 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022955 dict_var->di_tv.vval.v_dict = dict;
22956 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022957 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22959 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022960}
22961
22962/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022963 * Unreference a dictionary initialized by init_var_dict().
22964 */
22965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022966unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022967{
22968 /* Now the dict needs to be freed if no one else is using it, go back to
22969 * normal reference counting. */
22970 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22971 dict_unref(dict);
22972}
22973
22974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022975 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022976 * Frees all allocated variables and the value they contain.
22977 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 */
22979 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022980vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022981{
22982 vars_clear_ext(ht, TRUE);
22983}
22984
22985/*
22986 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22987 */
22988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022989vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022990{
Bram Moolenaara7043832005-01-21 11:56:39 +000022991 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 hashitem_T *hi;
22993 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022994
Bram Moolenaar33570922005-01-25 22:26:29 +000022995 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022996 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022997 for (hi = ht->ht_array; todo > 0; ++hi)
22998 {
22999 if (!HASHITEM_EMPTY(hi))
23000 {
23001 --todo;
23002
Bram Moolenaar33570922005-01-25 22:26:29 +000023003 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023004 * ht_array might change then. hash_clear() takes care of it
23005 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023006 v = HI2DI(hi);
23007 if (free_val)
23008 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023009 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023010 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023011 }
23012 }
23013 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023014 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023015}
23016
Bram Moolenaara7043832005-01-21 11:56:39 +000023017/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 * Delete a variable from hashtab "ht" at item "hi".
23019 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023020 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023021 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023022delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023023{
Bram Moolenaar33570922005-01-25 22:26:29 +000023024 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023025
23026 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023027 clear_tv(&di->di_tv);
23028 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029}
23030
23031/*
23032 * List the value of one internal variable.
23033 */
23034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023036{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023037 char_u *tofree;
23038 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023039 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023040
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023041 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023043 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023044 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045}
23046
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023048list_one_var_a(
23049 char_u *prefix,
23050 char_u *name,
23051 int type,
23052 char_u *string,
23053 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023054{
Bram Moolenaar31859182007-08-14 20:41:13 +000023055 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23056 msg_start();
23057 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023058 if (name != NULL) /* "a:" vars don't have a name stored */
23059 msg_puts(name);
23060 msg_putchar(' ');
23061 msg_advance(22);
23062 if (type == VAR_NUMBER)
23063 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023064 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023065 msg_putchar('*');
23066 else if (type == VAR_LIST)
23067 {
23068 msg_putchar('[');
23069 if (*string == '[')
23070 ++string;
23071 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023072 else if (type == VAR_DICT)
23073 {
23074 msg_putchar('{');
23075 if (*string == '{')
23076 ++string;
23077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 else
23079 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023080
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023082
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023083 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023084 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023085 if (*first)
23086 {
23087 msg_clr_eos();
23088 *first = FALSE;
23089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090}
23091
23092/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023093 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 * If the variable already exists, the value is updated.
23095 * Otherwise the variable is created.
23096 */
23097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023098set_var(
23099 char_u *name,
23100 typval_T *tv,
23101 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023102{
Bram Moolenaar33570922005-01-25 22:26:29 +000023103 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023104 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023105 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023107 ht = find_var_ht(name, &varname);
23108 if (ht == NULL || *varname == NUL)
23109 {
23110 EMSG2(_(e_illvar), name);
23111 return;
23112 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023113 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023114
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023115 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23116 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023117 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023118
Bram Moolenaar33570922005-01-25 22:26:29 +000023119 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023121 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023122 if (var_check_ro(v->di_flags, name, FALSE)
23123 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023124 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023125
23126 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023127 * Handle setting internal v: variables separately where needed to
23128 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023129 */
23130 if (ht == &vimvarht)
23131 {
23132 if (v->di_tv.v_type == VAR_STRING)
23133 {
23134 vim_free(v->di_tv.vval.v_string);
23135 if (copy || tv->v_type != VAR_STRING)
23136 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23137 else
23138 {
23139 /* Take over the string to avoid an extra alloc/free. */
23140 v->di_tv.vval.v_string = tv->vval.v_string;
23141 tv->vval.v_string = NULL;
23142 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023143 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023144 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023145 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023146 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023147 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023148 if (STRCMP(varname, "searchforward") == 0)
23149 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023150#ifdef FEAT_SEARCH_EXTRA
23151 else if (STRCMP(varname, "hlsearch") == 0)
23152 {
23153 no_hlsearch = !v->di_tv.vval.v_number;
23154 redraw_all_later(SOME_VALID);
23155 }
23156#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023157 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023158 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023159 else if (v->di_tv.v_type != tv->v_type)
23160 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023161 }
23162
23163 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 }
23165 else /* add a new variable */
23166 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023167 /* Can't add "v:" variable. */
23168 if (ht == &vimvarht)
23169 {
23170 EMSG2(_(e_illvar), name);
23171 return;
23172 }
23173
Bram Moolenaar92124a32005-06-17 22:03:40 +000023174 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023175 if (!valid_varname(varname))
23176 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023177
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023178 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23179 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023180 if (v == NULL)
23181 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023182 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023183 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023185 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 return;
23187 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023188 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023190
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023191 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023192 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023193 else
23194 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023195 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023196 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023197 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199}
23200
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023201/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023202 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023203 * Also give an error message.
23204 */
23205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023206var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023207{
23208 if (flags & DI_FLAGS_RO)
23209 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023210 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023211 return TRUE;
23212 }
23213 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23214 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023215 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023216 return TRUE;
23217 }
23218 return FALSE;
23219}
23220
23221/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023222 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23223 * Also give an error message.
23224 */
23225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023226var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023227{
23228 if (flags & DI_FLAGS_FIX)
23229 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023230 EMSG2(_("E795: Cannot delete variable %s"),
23231 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023232 return TRUE;
23233 }
23234 return FALSE;
23235}
23236
23237/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023238 * Check if a funcref is assigned to a valid variable name.
23239 * Return TRUE and give an error if not.
23240 */
23241 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023242var_check_func_name(
23243 char_u *name, /* points to start of variable name */
23244 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023245{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023246 /* Allow for w: b: s: and t:. */
23247 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023248 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23249 ? name[2] : name[0]))
23250 {
23251 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23252 name);
23253 return TRUE;
23254 }
23255 /* Don't allow hiding a function. When "v" is not NULL we might be
23256 * assigning another function to the same var, the type is checked
23257 * below. */
23258 if (new_var && function_exists(name))
23259 {
23260 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23261 name);
23262 return TRUE;
23263 }
23264 return FALSE;
23265}
23266
23267/*
23268 * Check if a variable name is valid.
23269 * Return FALSE and give an error if not.
23270 */
23271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023272valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023273{
23274 char_u *p;
23275
23276 for (p = varname; *p != NUL; ++p)
23277 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23278 && *p != AUTOLOAD_CHAR)
23279 {
23280 EMSG2(_(e_illvar), varname);
23281 return FALSE;
23282 }
23283 return TRUE;
23284}
23285
23286/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023287 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023288 * Also give an error message, using "name" or _("name") when use_gettext is
23289 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023290 */
23291 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023292tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023293{
23294 if (lock & VAR_LOCKED)
23295 {
23296 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023297 name == NULL ? (char_u *)_("Unknown")
23298 : use_gettext ? (char_u *)_(name)
23299 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023300 return TRUE;
23301 }
23302 if (lock & VAR_FIXED)
23303 {
23304 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023305 name == NULL ? (char_u *)_("Unknown")
23306 : use_gettext ? (char_u *)_(name)
23307 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023308 return TRUE;
23309 }
23310 return FALSE;
23311}
23312
23313/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023314 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023315 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023316 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023317 * It is OK for "from" and "to" to point to the same item. This is used to
23318 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023319 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023320 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023321copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023323 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023324 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023325 switch (from->v_type)
23326 {
23327 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023328 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023329 to->vval.v_number = from->vval.v_number;
23330 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023331 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023332#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023333 to->vval.v_float = from->vval.v_float;
23334 break;
23335#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023336 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023337#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023338 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023339 if (to->vval.v_job != NULL)
23340 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023341 break;
23342#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023343 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023344#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023345 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023346 if (to->vval.v_channel != NULL)
23347 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023348 break;
23349#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023350 case VAR_STRING:
23351 case VAR_FUNC:
23352 if (from->vval.v_string == NULL)
23353 to->vval.v_string = NULL;
23354 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023355 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023356 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023357 if (from->v_type == VAR_FUNC)
23358 func_ref(to->vval.v_string);
23359 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023360 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023361 case VAR_PARTIAL:
23362 if (from->vval.v_partial == NULL)
23363 to->vval.v_partial = NULL;
23364 else
23365 {
23366 to->vval.v_partial = from->vval.v_partial;
23367 ++to->vval.v_partial->pt_refcount;
23368 }
23369 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023370 case VAR_LIST:
23371 if (from->vval.v_list == NULL)
23372 to->vval.v_list = NULL;
23373 else
23374 {
23375 to->vval.v_list = from->vval.v_list;
23376 ++to->vval.v_list->lv_refcount;
23377 }
23378 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023379 case VAR_DICT:
23380 if (from->vval.v_dict == NULL)
23381 to->vval.v_dict = NULL;
23382 else
23383 {
23384 to->vval.v_dict = from->vval.v_dict;
23385 ++to->vval.v_dict->dv_refcount;
23386 }
23387 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023388 case VAR_UNKNOWN:
23389 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023390 break;
23391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392}
23393
23394/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023395 * Make a copy of an item.
23396 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023397 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23398 * reference to an already copied list/dict can be used.
23399 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023400 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023401 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023402item_copy(
23403 typval_T *from,
23404 typval_T *to,
23405 int deep,
23406 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023407{
23408 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023409 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023410
Bram Moolenaar33570922005-01-25 22:26:29 +000023411 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023412 {
23413 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023414 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023415 }
23416 ++recurse;
23417
23418 switch (from->v_type)
23419 {
23420 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023421 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023422 case VAR_STRING:
23423 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023424 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023425 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023426 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023427 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023428 copy_tv(from, to);
23429 break;
23430 case VAR_LIST:
23431 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023432 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023433 if (from->vval.v_list == NULL)
23434 to->vval.v_list = NULL;
23435 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23436 {
23437 /* use the copy made earlier */
23438 to->vval.v_list = from->vval.v_list->lv_copylist;
23439 ++to->vval.v_list->lv_refcount;
23440 }
23441 else
23442 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23443 if (to->vval.v_list == NULL)
23444 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023445 break;
23446 case VAR_DICT:
23447 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023448 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023449 if (from->vval.v_dict == NULL)
23450 to->vval.v_dict = NULL;
23451 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23452 {
23453 /* use the copy made earlier */
23454 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23455 ++to->vval.v_dict->dv_refcount;
23456 }
23457 else
23458 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23459 if (to->vval.v_dict == NULL)
23460 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023461 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023462 case VAR_UNKNOWN:
23463 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023464 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023465 }
23466 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023467 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023468}
23469
23470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471 * ":echo expr1 ..." print each argument separated with a space, add a
23472 * newline at the end.
23473 * ":echon expr1 ..." print each argument plain.
23474 */
23475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023476ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477{
23478 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023479 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023480 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 char_u *p;
23482 int needclr = TRUE;
23483 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023484 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485
23486 if (eap->skip)
23487 ++emsg_skip;
23488 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23489 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023490 /* If eval1() causes an error message the text from the command may
23491 * still need to be cleared. E.g., "echo 22,44". */
23492 need_clr_eos = needclr;
23493
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023495 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 {
23497 /*
23498 * Report the invalid expression unless the expression evaluation
23499 * has been cancelled due to an aborting error, an interrupt, or an
23500 * exception.
23501 */
23502 if (!aborting())
23503 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023504 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023505 break;
23506 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023507 need_clr_eos = FALSE;
23508
Bram Moolenaar071d4272004-06-13 20:20:40 +000023509 if (!eap->skip)
23510 {
23511 if (atstart)
23512 {
23513 atstart = FALSE;
23514 /* Call msg_start() after eval1(), evaluating the expression
23515 * may cause a message to appear. */
23516 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023517 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023518 /* Mark the saved text as finishing the line, so that what
23519 * follows is displayed on a new line when scrolling back
23520 * at the more prompt. */
23521 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023524 }
23525 else if (eap->cmdidx == CMD_echo)
23526 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023527 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023528 if (p != NULL)
23529 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023531 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023533 if (*p != TAB && needclr)
23534 {
23535 /* remove any text still there from the command */
23536 msg_clr_eos();
23537 needclr = FALSE;
23538 }
23539 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023540 }
23541 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023542 {
23543#ifdef FEAT_MBYTE
23544 if (has_mbyte)
23545 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023546 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023547
23548 (void)msg_outtrans_len_attr(p, i, echo_attr);
23549 p += i - 1;
23550 }
23551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023553 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023556 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023557 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023558 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 arg = skipwhite(arg);
23560 }
23561 eap->nextcmd = check_nextcmd(arg);
23562
23563 if (eap->skip)
23564 --emsg_skip;
23565 else
23566 {
23567 /* remove text that may still be there from the command */
23568 if (needclr)
23569 msg_clr_eos();
23570 if (eap->cmdidx == CMD_echo)
23571 msg_end();
23572 }
23573}
23574
23575/*
23576 * ":echohl {name}".
23577 */
23578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023579ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023580{
23581 int id;
23582
23583 id = syn_name2id(eap->arg);
23584 if (id == 0)
23585 echo_attr = 0;
23586 else
23587 echo_attr = syn_id2attr(id);
23588}
23589
23590/*
23591 * ":execute expr1 ..." execute the result of an expression.
23592 * ":echomsg expr1 ..." Print a message
23593 * ":echoerr expr1 ..." Print an error
23594 * Each gets spaces around each argument and a newline at the end for
23595 * echo commands
23596 */
23597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023598ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599{
23600 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023601 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 int ret = OK;
23603 char_u *p;
23604 garray_T ga;
23605 int len;
23606 int save_did_emsg;
23607
23608 ga_init2(&ga, 1, 80);
23609
23610 if (eap->skip)
23611 ++emsg_skip;
23612 while (*arg != NUL && *arg != '|' && *arg != '\n')
23613 {
23614 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023615 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 {
23617 /*
23618 * Report the invalid expression unless the expression evaluation
23619 * has been cancelled due to an aborting error, an interrupt, or an
23620 * exception.
23621 */
23622 if (!aborting())
23623 EMSG2(_(e_invexpr2), p);
23624 ret = FAIL;
23625 break;
23626 }
23627
23628 if (!eap->skip)
23629 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023630 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631 len = (int)STRLEN(p);
23632 if (ga_grow(&ga, len + 2) == FAIL)
23633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023634 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635 ret = FAIL;
23636 break;
23637 }
23638 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023640 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 ga.ga_len += len;
23642 }
23643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023644 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 arg = skipwhite(arg);
23646 }
23647
23648 if (ret != FAIL && ga.ga_data != NULL)
23649 {
23650 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023651 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023653 out_flush();
23654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 else if (eap->cmdidx == CMD_echoerr)
23656 {
23657 /* We don't want to abort following commands, restore did_emsg. */
23658 save_did_emsg = did_emsg;
23659 EMSG((char_u *)ga.ga_data);
23660 if (!force_abort)
23661 did_emsg = save_did_emsg;
23662 }
23663 else if (eap->cmdidx == CMD_execute)
23664 do_cmdline((char_u *)ga.ga_data,
23665 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23666 }
23667
23668 ga_clear(&ga);
23669
23670 if (eap->skip)
23671 --emsg_skip;
23672
23673 eap->nextcmd = check_nextcmd(arg);
23674}
23675
23676/*
23677 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23678 * "arg" points to the "&" or '+' when called, to "option" when returning.
23679 * Returns NULL when no option name found. Otherwise pointer to the char
23680 * after the option name.
23681 */
23682 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023683find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684{
23685 char_u *p = *arg;
23686
23687 ++p;
23688 if (*p == 'g' && p[1] == ':')
23689 {
23690 *opt_flags = OPT_GLOBAL;
23691 p += 2;
23692 }
23693 else if (*p == 'l' && p[1] == ':')
23694 {
23695 *opt_flags = OPT_LOCAL;
23696 p += 2;
23697 }
23698 else
23699 *opt_flags = 0;
23700
23701 if (!ASCII_ISALPHA(*p))
23702 return NULL;
23703 *arg = p;
23704
23705 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23706 p += 4; /* termcap option */
23707 else
23708 while (ASCII_ISALPHA(*p))
23709 ++p;
23710 return p;
23711}
23712
23713/*
23714 * ":function"
23715 */
23716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023717ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718{
23719 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023720 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721 int j;
23722 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023724 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 char_u *name = NULL;
23726 char_u *p;
23727 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023728 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 garray_T newargs;
23730 garray_T newlines;
23731 int varargs = FALSE;
23732 int mustend = FALSE;
23733 int flags = 0;
23734 ufunc_T *fp;
23735 int indent;
23736 int nesting;
23737 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023738 dictitem_T *v;
23739 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023740 static int func_nr = 0; /* number for nameless function */
23741 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023742 hashtab_T *ht;
23743 int todo;
23744 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023745 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746
23747 /*
23748 * ":function" without argument: list functions.
23749 */
23750 if (ends_excmd(*eap->arg))
23751 {
23752 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023753 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023754 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023755 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023756 {
23757 if (!HASHITEM_EMPTY(hi))
23758 {
23759 --todo;
23760 fp = HI2UF(hi);
23761 if (!isdigit(*fp->uf_name))
23762 list_func_head(fp, FALSE);
23763 }
23764 }
23765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 eap->nextcmd = check_nextcmd(eap->arg);
23767 return;
23768 }
23769
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023770 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023771 * ":function /pat": list functions matching pattern.
23772 */
23773 if (*eap->arg == '/')
23774 {
23775 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23776 if (!eap->skip)
23777 {
23778 regmatch_T regmatch;
23779
23780 c = *p;
23781 *p = NUL;
23782 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23783 *p = c;
23784 if (regmatch.regprog != NULL)
23785 {
23786 regmatch.rm_ic = p_ic;
23787
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023788 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023789 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23790 {
23791 if (!HASHITEM_EMPTY(hi))
23792 {
23793 --todo;
23794 fp = HI2UF(hi);
23795 if (!isdigit(*fp->uf_name)
23796 && vim_regexec(&regmatch, fp->uf_name, 0))
23797 list_func_head(fp, FALSE);
23798 }
23799 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023800 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023801 }
23802 }
23803 if (*p == '/')
23804 ++p;
23805 eap->nextcmd = check_nextcmd(p);
23806 return;
23807 }
23808
23809 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023810 * Get the function name. There are these situations:
23811 * func normal function name
23812 * "name" == func, "fudi.fd_dict" == NULL
23813 * dict.func new dictionary entry
23814 * "name" == NULL, "fudi.fd_dict" set,
23815 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23816 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023817 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023818 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23819 * dict.func existing dict entry that's not a Funcref
23820 * "name" == NULL, "fudi.fd_dict" set,
23821 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023822 * s:func script-local function name
23823 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023826 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023827 paren = (vim_strchr(p, '(') != NULL);
23828 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023829 {
23830 /*
23831 * Return on an invalid expression in braces, unless the expression
23832 * evaluation has been cancelled due to an aborting error, an
23833 * interrupt, or an exception.
23834 */
23835 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023836 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023837 if (!eap->skip && fudi.fd_newkey != NULL)
23838 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023839 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 else
23843 eap->skip = TRUE;
23844 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023845
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 /* An error in a function call during evaluation of an expression in magic
23847 * braces should not cause the function not to be defined. */
23848 saved_did_emsg = did_emsg;
23849 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850
23851 /*
23852 * ":function func" with only function name: list function.
23853 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023854 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 {
23856 if (!ends_excmd(*skipwhite(p)))
23857 {
23858 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023859 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 }
23861 eap->nextcmd = check_nextcmd(p);
23862 if (eap->nextcmd != NULL)
23863 *p = NUL;
23864 if (!eap->skip && !got_int)
23865 {
23866 fp = find_func(name);
23867 if (fp != NULL)
23868 {
23869 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023872 if (FUNCLINE(fp, j) == NULL)
23873 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 msg_putchar('\n');
23875 msg_outnum((long)(j + 1));
23876 if (j < 9)
23877 msg_putchar(' ');
23878 if (j < 99)
23879 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023880 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023881 out_flush(); /* show a line at a time */
23882 ui_breakcheck();
23883 }
23884 if (!got_int)
23885 {
23886 msg_putchar('\n');
23887 msg_puts((char_u *)" endfunction");
23888 }
23889 }
23890 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023891 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023892 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023893 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 }
23895
23896 /*
23897 * ":function name(arg1, arg2)" Define function.
23898 */
23899 p = skipwhite(p);
23900 if (*p != '(')
23901 {
23902 if (!eap->skip)
23903 {
23904 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023905 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023906 }
23907 /* attempt to continue by skipping some text */
23908 if (vim_strchr(p, '(') != NULL)
23909 p = vim_strchr(p, '(');
23910 }
23911 p = skipwhite(p + 1);
23912
23913 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23914 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23915
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023916 if (!eap->skip)
23917 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023918 /* Check the name of the function. Unless it's a dictionary function
23919 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023920 if (name != NULL)
23921 arg = name;
23922 else
23923 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023924 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023925 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23926 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023927 {
23928 if (*arg == K_SPECIAL)
23929 j = 3;
23930 else
23931 j = 0;
23932 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23933 : eval_isnamec(arg[j])))
23934 ++j;
23935 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023936 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023937 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023938 /* Disallow using the g: dict. */
23939 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23940 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023941 }
23942
Bram Moolenaar071d4272004-06-13 20:20:40 +000023943 /*
23944 * Isolate the arguments: "arg1, arg2, ...)"
23945 */
23946 while (*p != ')')
23947 {
23948 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23949 {
23950 varargs = TRUE;
23951 p += 3;
23952 mustend = TRUE;
23953 }
23954 else
23955 {
23956 arg = p;
23957 while (ASCII_ISALNUM(*p) || *p == '_')
23958 ++p;
23959 if (arg == p || isdigit(*arg)
23960 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23961 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23962 {
23963 if (!eap->skip)
23964 EMSG2(_("E125: Illegal argument: %s"), arg);
23965 break;
23966 }
23967 if (ga_grow(&newargs, 1) == FAIL)
23968 goto erret;
23969 c = *p;
23970 *p = NUL;
23971 arg = vim_strsave(arg);
23972 if (arg == NULL)
23973 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023974
23975 /* Check for duplicate argument name. */
23976 for (i = 0; i < newargs.ga_len; ++i)
23977 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23978 {
23979 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023980 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023981 goto erret;
23982 }
23983
Bram Moolenaar071d4272004-06-13 20:20:40 +000023984 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23985 *p = c;
23986 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 if (*p == ',')
23988 ++p;
23989 else
23990 mustend = TRUE;
23991 }
23992 p = skipwhite(p);
23993 if (mustend && *p != ')')
23994 {
23995 if (!eap->skip)
23996 EMSG2(_(e_invarg2), eap->arg);
23997 break;
23998 }
23999 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024000 if (*p != ')')
24001 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 ++p; /* skip the ')' */
24003
Bram Moolenaare9a41262005-01-15 22:18:47 +000024004 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 for (;;)
24006 {
24007 p = skipwhite(p);
24008 if (STRNCMP(p, "range", 5) == 0)
24009 {
24010 flags |= FC_RANGE;
24011 p += 5;
24012 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024013 else if (STRNCMP(p, "dict", 4) == 0)
24014 {
24015 flags |= FC_DICT;
24016 p += 4;
24017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018 else if (STRNCMP(p, "abort", 5) == 0)
24019 {
24020 flags |= FC_ABORT;
24021 p += 5;
24022 }
24023 else
24024 break;
24025 }
24026
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024027 /* When there is a line break use what follows for the function body.
24028 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24029 if (*p == '\n')
24030 line_arg = p + 1;
24031 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024032 EMSG(_(e_trailing));
24033
24034 /*
24035 * Read the body of the function, until ":endfunction" is found.
24036 */
24037 if (KeyTyped)
24038 {
24039 /* Check if the function already exists, don't let the user type the
24040 * whole function before telling him it doesn't work! For a script we
24041 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024042 if (!eap->skip && !eap->forceit)
24043 {
24044 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24045 EMSG(_(e_funcdict));
24046 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024047 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024050 if (!eap->skip && did_emsg)
24051 goto erret;
24052
Bram Moolenaar071d4272004-06-13 20:20:40 +000024053 msg_putchar('\n'); /* don't overwrite the function name */
24054 cmdline_row = msg_row;
24055 }
24056
24057 indent = 2;
24058 nesting = 0;
24059 for (;;)
24060 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024061 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024062 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024063 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024064 saved_wait_return = FALSE;
24065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024066 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024067 sourcing_lnum_off = sourcing_lnum;
24068
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024069 if (line_arg != NULL)
24070 {
24071 /* Use eap->arg, split up in parts by line breaks. */
24072 theline = line_arg;
24073 p = vim_strchr(theline, '\n');
24074 if (p == NULL)
24075 line_arg += STRLEN(line_arg);
24076 else
24077 {
24078 *p = NUL;
24079 line_arg = p + 1;
24080 }
24081 }
24082 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 theline = getcmdline(':', 0L, indent);
24084 else
24085 theline = eap->getline(':', eap->cookie, indent);
24086 if (KeyTyped)
24087 lines_left = Rows - 1;
24088 if (theline == NULL)
24089 {
24090 EMSG(_("E126: Missing :endfunction"));
24091 goto erret;
24092 }
24093
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024094 /* Detect line continuation: sourcing_lnum increased more than one. */
24095 if (sourcing_lnum > sourcing_lnum_off + 1)
24096 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24097 else
24098 sourcing_lnum_off = 0;
24099
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 if (skip_until != NULL)
24101 {
24102 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24103 * don't check for ":endfunc". */
24104 if (STRCMP(theline, skip_until) == 0)
24105 {
24106 vim_free(skip_until);
24107 skip_until = NULL;
24108 }
24109 }
24110 else
24111 {
24112 /* skip ':' and blanks*/
24113 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24114 ;
24115
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024116 /* Check for "endfunction". */
24117 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024119 if (line_arg == NULL)
24120 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 break;
24122 }
24123
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024124 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 * at "end". */
24126 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24127 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024128 else if (STRNCMP(p, "if", 2) == 0
24129 || STRNCMP(p, "wh", 2) == 0
24130 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 || STRNCMP(p, "try", 3) == 0)
24132 indent += 2;
24133
24134 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024135 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024137 if (*p == '!')
24138 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024140 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024141 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024142 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024143 ++nesting;
24144 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 }
24146 }
24147
24148 /* Check for ":append" or ":insert". */
24149 p = skip_range(p, NULL);
24150 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24151 || (p[0] == 'i'
24152 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24153 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24154 skip_until = vim_strsave((char_u *)".");
24155
24156 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24157 arg = skipwhite(skiptowhite(p));
24158 if (arg[0] == '<' && arg[1] =='<'
24159 && ((p[0] == 'p' && p[1] == 'y'
24160 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24161 || (p[0] == 'p' && p[1] == 'e'
24162 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24163 || (p[0] == 't' && p[1] == 'c'
24164 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024165 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24166 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024167 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24168 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024169 || (p[0] == 'm' && p[1] == 'z'
24170 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024171 ))
24172 {
24173 /* ":python <<" continues until a dot, like ":append" */
24174 p = skipwhite(arg + 2);
24175 if (*p == NUL)
24176 skip_until = vim_strsave((char_u *)".");
24177 else
24178 skip_until = vim_strsave(p);
24179 }
24180 }
24181
24182 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024183 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024184 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024185 if (line_arg == NULL)
24186 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024188 }
24189
24190 /* Copy the line to newly allocated memory. get_one_sourceline()
24191 * allocates 250 bytes per line, this saves 80% on average. The cost
24192 * is an extra alloc/free. */
24193 p = vim_strsave(theline);
24194 if (p != NULL)
24195 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024196 if (line_arg == NULL)
24197 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024198 theline = p;
24199 }
24200
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024201 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24202
24203 /* Add NULL lines for continuation lines, so that the line count is
24204 * equal to the index in the growarray. */
24205 while (sourcing_lnum_off-- > 0)
24206 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024207
24208 /* Check for end of eap->arg. */
24209 if (line_arg != NULL && *line_arg == NUL)
24210 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024211 }
24212
24213 /* Don't define the function when skipping commands or when an error was
24214 * detected. */
24215 if (eap->skip || did_emsg)
24216 goto erret;
24217
24218 /*
24219 * If there are no errors, add the function
24220 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024221 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024222 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024223 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024224 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024225 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024226 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024227 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024228 goto erret;
24229 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024230
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024231 fp = find_func(name);
24232 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024234 if (!eap->forceit)
24235 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024236 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024237 goto erret;
24238 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024239 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024240 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024241 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024242 name);
24243 goto erret;
24244 }
24245 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024246 ga_clear_strings(&(fp->uf_args));
24247 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024248 vim_free(name);
24249 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 }
24252 else
24253 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024254 char numbuf[20];
24255
24256 fp = NULL;
24257 if (fudi.fd_newkey == NULL && !eap->forceit)
24258 {
24259 EMSG(_(e_funcdict));
24260 goto erret;
24261 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024262 if (fudi.fd_di == NULL)
24263 {
24264 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024265 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024266 goto erret;
24267 }
24268 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024269 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024270 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024271
24272 /* Give the function a sequential number. Can only be used with a
24273 * Funcref! */
24274 vim_free(name);
24275 sprintf(numbuf, "%d", ++func_nr);
24276 name = vim_strsave((char_u *)numbuf);
24277 if (name == NULL)
24278 goto erret;
24279 }
24280
24281 if (fp == NULL)
24282 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024283 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024284 {
24285 int slen, plen;
24286 char_u *scriptname;
24287
24288 /* Check that the autoload name matches the script name. */
24289 j = FAIL;
24290 if (sourcing_name != NULL)
24291 {
24292 scriptname = autoload_name(name);
24293 if (scriptname != NULL)
24294 {
24295 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024296 plen = (int)STRLEN(p);
24297 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024298 if (slen > plen && fnamecmp(p,
24299 sourcing_name + slen - plen) == 0)
24300 j = OK;
24301 vim_free(scriptname);
24302 }
24303 }
24304 if (j == FAIL)
24305 {
24306 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24307 goto erret;
24308 }
24309 }
24310
24311 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024312 if (fp == NULL)
24313 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024314
24315 if (fudi.fd_dict != NULL)
24316 {
24317 if (fudi.fd_di == NULL)
24318 {
24319 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024320 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024321 if (fudi.fd_di == NULL)
24322 {
24323 vim_free(fp);
24324 goto erret;
24325 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024326 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24327 {
24328 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024329 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024330 goto erret;
24331 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024332 }
24333 else
24334 /* overwrite existing dict entry */
24335 clear_tv(&fudi.fd_di->di_tv);
24336 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024337 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024338 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024339 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024340
24341 /* behave like "dict" was used */
24342 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024343 }
24344
Bram Moolenaar071d4272004-06-13 20:20:40 +000024345 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024346 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024347 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24348 {
24349 vim_free(fp);
24350 goto erret;
24351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024352 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024353 fp->uf_args = newargs;
24354 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024355#ifdef FEAT_PROFILE
24356 fp->uf_tml_count = NULL;
24357 fp->uf_tml_total = NULL;
24358 fp->uf_tml_self = NULL;
24359 fp->uf_profiling = FALSE;
24360 if (prof_def_func())
24361 func_do_profile(fp);
24362#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024363 fp->uf_varargs = varargs;
24364 fp->uf_flags = flags;
24365 fp->uf_calls = 0;
24366 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024367 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024368
24369erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024370 ga_clear_strings(&newargs);
24371 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024372ret_free:
24373 vim_free(skip_until);
24374 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024376 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024377 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378}
24379
24380/*
24381 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024382 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024383 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024384 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024385 * TFN_INT: internal function name OK
24386 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024387 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 * Advances "pp" to just after the function name (if no error).
24389 */
24390 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024391trans_function_name(
24392 char_u **pp,
24393 int skip, /* only find the end, don't evaluate */
24394 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024395 funcdict_T *fdp, /* return: info about dictionary used */
24396 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024398 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024399 char_u *start;
24400 char_u *end;
24401 int lead;
24402 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024404 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024405
24406 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024407 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024408 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024409
24410 /* Check for hard coded <SNR>: already translated function ID (from a user
24411 * command). */
24412 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24413 && (*pp)[2] == (int)KE_SNR)
24414 {
24415 *pp += 3;
24416 len = get_id_len(pp) + 3;
24417 return vim_strnsave(start, len);
24418 }
24419
24420 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24421 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024422 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024423 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024424 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024425
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024426 /* Note that TFN_ flags use the same values as GLV_ flags. */
24427 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024428 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024429 if (end == start)
24430 {
24431 if (!skip)
24432 EMSG(_("E129: Function name required"));
24433 goto theend;
24434 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024435 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024436 {
24437 /*
24438 * Report an invalid expression in braces, unless the expression
24439 * evaluation has been cancelled due to an aborting error, an
24440 * interrupt, or an exception.
24441 */
24442 if (!aborting())
24443 {
24444 if (end != NULL)
24445 EMSG2(_(e_invarg2), start);
24446 }
24447 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024448 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024449 goto theend;
24450 }
24451
24452 if (lv.ll_tv != NULL)
24453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024454 if (fdp != NULL)
24455 {
24456 fdp->fd_dict = lv.ll_dict;
24457 fdp->fd_newkey = lv.ll_newkey;
24458 lv.ll_newkey = NULL;
24459 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024460 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024461 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24462 {
24463 name = vim_strsave(lv.ll_tv->vval.v_string);
24464 *pp = end;
24465 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024466 else if (lv.ll_tv->v_type == VAR_PARTIAL
24467 && lv.ll_tv->vval.v_partial != NULL)
24468 {
24469 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24470 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024471 if (partial != NULL)
24472 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024473 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024474 else
24475 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024476 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24477 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024478 EMSG(_(e_funcref));
24479 else
24480 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024481 name = NULL;
24482 }
24483 goto theend;
24484 }
24485
24486 if (lv.ll_name == NULL)
24487 {
24488 /* Error found, but continue after the function name. */
24489 *pp = end;
24490 goto theend;
24491 }
24492
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024493 /* Check if the name is a Funcref. If so, use the value. */
24494 if (lv.ll_exp_name != NULL)
24495 {
24496 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024497 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024498 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024499 if (name == lv.ll_exp_name)
24500 name = NULL;
24501 }
24502 else
24503 {
24504 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024505 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024506 if (name == *pp)
24507 name = NULL;
24508 }
24509 if (name != NULL)
24510 {
24511 name = vim_strsave(name);
24512 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024513 if (STRNCMP(name, "<SNR>", 5) == 0)
24514 {
24515 /* Change "<SNR>" to the byte sequence. */
24516 name[0] = K_SPECIAL;
24517 name[1] = KS_EXTRA;
24518 name[2] = (int)KE_SNR;
24519 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24520 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024521 goto theend;
24522 }
24523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024524 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024525 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024526 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024527 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24528 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24529 {
24530 /* When there was "s:" already or the name expanded to get a
24531 * leading "s:" then remove it. */
24532 lv.ll_name += 2;
24533 len -= 2;
24534 lead = 2;
24535 }
24536 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024537 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024538 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024539 /* skip over "s:" and "g:" */
24540 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024541 lv.ll_name += 2;
24542 len = (int)(end - lv.ll_name);
24543 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024544
24545 /*
24546 * Copy the function name to allocated memory.
24547 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24548 * Accept <SNR>123_name() outside a script.
24549 */
24550 if (skip)
24551 lead = 0; /* do nothing */
24552 else if (lead > 0)
24553 {
24554 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024555 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24556 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024557 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024558 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024559 if (current_SID <= 0)
24560 {
24561 EMSG(_(e_usingsid));
24562 goto theend;
24563 }
24564 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24565 lead += (int)STRLEN(sid_buf);
24566 }
24567 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024568 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024569 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024570 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024571 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024572 goto theend;
24573 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024574 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024575 {
24576 char_u *cp = vim_strchr(lv.ll_name, ':');
24577
24578 if (cp != NULL && cp < end)
24579 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024580 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024581 goto theend;
24582 }
24583 }
24584
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024585 name = alloc((unsigned)(len + lead + 1));
24586 if (name != NULL)
24587 {
24588 if (lead > 0)
24589 {
24590 name[0] = K_SPECIAL;
24591 name[1] = KS_EXTRA;
24592 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024593 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024594 STRCPY(name + 3, sid_buf);
24595 }
24596 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024597 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024598 }
24599 *pp = end;
24600
24601theend:
24602 clear_lval(&lv);
24603 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024604}
24605
24606/*
24607 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24608 * Return 2 if "p" starts with "s:".
24609 * Return 0 otherwise.
24610 */
24611 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024612eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024613{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024614 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24615 * the standard library function. */
24616 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24617 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024618 return 5;
24619 if (p[0] == 's' && p[1] == ':')
24620 return 2;
24621 return 0;
24622}
24623
24624/*
24625 * Return TRUE if "p" starts with "<SID>" or "s:".
24626 * Only works if eval_fname_script() returned non-zero for "p"!
24627 */
24628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024629eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630{
24631 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24632}
24633
24634/*
24635 * List the head of the function: "name(arg1, arg2)".
24636 */
24637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024638list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639{
24640 int j;
24641
24642 msg_start();
24643 if (indent)
24644 MSG_PUTS(" ");
24645 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024646 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024647 {
24648 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024649 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024650 }
24651 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024652 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024654 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 {
24656 if (j)
24657 MSG_PUTS(", ");
24658 msg_puts(FUNCARG(fp, j));
24659 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024660 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024661 {
24662 if (j)
24663 MSG_PUTS(", ");
24664 MSG_PUTS("...");
24665 }
24666 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024667 if (fp->uf_flags & FC_ABORT)
24668 MSG_PUTS(" abort");
24669 if (fp->uf_flags & FC_RANGE)
24670 MSG_PUTS(" range");
24671 if (fp->uf_flags & FC_DICT)
24672 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024673 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024674 if (p_verbose > 0)
24675 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676}
24677
24678/*
24679 * Find a function by name, return pointer to it in ufuncs.
24680 * Return NULL for unknown function.
24681 */
24682 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024683find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024685 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024686
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024687 hi = hash_find(&func_hashtab, name);
24688 if (!HASHITEM_EMPTY(hi))
24689 return HI2UF(hi);
24690 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691}
24692
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024693#if defined(EXITFREE) || defined(PROTO)
24694 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024695free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024696{
24697 hashitem_T *hi;
24698
24699 /* Need to start all over every time, because func_free() may change the
24700 * hash table. */
24701 while (func_hashtab.ht_used > 0)
24702 for (hi = func_hashtab.ht_array; ; ++hi)
24703 if (!HASHITEM_EMPTY(hi))
24704 {
24705 func_free(HI2UF(hi));
24706 break;
24707 }
24708}
24709#endif
24710
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024712translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024713{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024714 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024715 return find_internal_func(name) >= 0;
24716 return find_func(name) != NULL;
24717}
24718
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024719/*
24720 * Return TRUE if a function "name" exists.
24721 */
24722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024723function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024724{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024725 char_u *nm = name;
24726 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024727 int n = FALSE;
24728
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024729 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024730 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024731 nm = skipwhite(nm);
24732
24733 /* Only accept "funcname", "funcname ", "funcname (..." and
24734 * "funcname(...", not "funcname!...". */
24735 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024736 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024737 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024738 return n;
24739}
24740
Bram Moolenaara1544c02013-05-30 12:35:52 +020024741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024742get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024743{
24744 char_u *nm = name;
24745 char_u *p;
24746
Bram Moolenaar65639032016-03-16 21:40:30 +010024747 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024748
24749 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024750 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024751 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024752
Bram Moolenaara1544c02013-05-30 12:35:52 +020024753 vim_free(p);
24754 return NULL;
24755}
24756
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024757/*
24758 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024759 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24760 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024761 */
24762 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024763builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024764{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024765 char_u *p;
24766
24767 if (!ASCII_ISLOWER(name[0]))
24768 return FALSE;
24769 p = vim_strchr(name, AUTOLOAD_CHAR);
24770 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024771}
24772
Bram Moolenaar05159a02005-02-26 23:04:13 +000024773#if defined(FEAT_PROFILE) || defined(PROTO)
24774/*
24775 * Start profiling function "fp".
24776 */
24777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024778func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024779{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024780 int len = fp->uf_lines.ga_len;
24781
24782 if (len == 0)
24783 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024784 fp->uf_tm_count = 0;
24785 profile_zero(&fp->uf_tm_self);
24786 profile_zero(&fp->uf_tm_total);
24787 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024788 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024789 if (fp->uf_tml_total == NULL)
24790 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024791 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024792 if (fp->uf_tml_self == NULL)
24793 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024794 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024795 fp->uf_tml_idx = -1;
24796 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24797 || fp->uf_tml_self == NULL)
24798 return; /* out of memory */
24799
24800 fp->uf_profiling = TRUE;
24801}
24802
24803/*
24804 * Dump the profiling results for all functions in file "fd".
24805 */
24806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024807func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024808{
24809 hashitem_T *hi;
24810 int todo;
24811 ufunc_T *fp;
24812 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024813 ufunc_T **sorttab;
24814 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024815
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024816 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024817 if (todo == 0)
24818 return; /* nothing to dump */
24819
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024820 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024821
Bram Moolenaar05159a02005-02-26 23:04:13 +000024822 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24823 {
24824 if (!HASHITEM_EMPTY(hi))
24825 {
24826 --todo;
24827 fp = HI2UF(hi);
24828 if (fp->uf_profiling)
24829 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024830 if (sorttab != NULL)
24831 sorttab[st_len++] = fp;
24832
Bram Moolenaar05159a02005-02-26 23:04:13 +000024833 if (fp->uf_name[0] == K_SPECIAL)
24834 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24835 else
24836 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24837 if (fp->uf_tm_count == 1)
24838 fprintf(fd, "Called 1 time\n");
24839 else
24840 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24841 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24842 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24843 fprintf(fd, "\n");
24844 fprintf(fd, "count total (s) self (s)\n");
24845
24846 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24847 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024848 if (FUNCLINE(fp, i) == NULL)
24849 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024850 prof_func_line(fd, fp->uf_tml_count[i],
24851 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024852 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24853 }
24854 fprintf(fd, "\n");
24855 }
24856 }
24857 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024858
24859 if (sorttab != NULL && st_len > 0)
24860 {
24861 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24862 prof_total_cmp);
24863 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24864 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24865 prof_self_cmp);
24866 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24867 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024868
24869 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024870}
Bram Moolenaar73830342005-02-28 22:48:19 +000024871
24872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024873prof_sort_list(
24874 FILE *fd,
24875 ufunc_T **sorttab,
24876 int st_len,
24877 char *title,
24878 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024879{
24880 int i;
24881 ufunc_T *fp;
24882
24883 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24884 fprintf(fd, "count total (s) self (s) function\n");
24885 for (i = 0; i < 20 && i < st_len; ++i)
24886 {
24887 fp = sorttab[i];
24888 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24889 prefer_self);
24890 if (fp->uf_name[0] == K_SPECIAL)
24891 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24892 else
24893 fprintf(fd, " %s()\n", fp->uf_name);
24894 }
24895 fprintf(fd, "\n");
24896}
24897
24898/*
24899 * Print the count and times for one function or function line.
24900 */
24901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024902prof_func_line(
24903 FILE *fd,
24904 int count,
24905 proftime_T *total,
24906 proftime_T *self,
24907 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024908{
24909 if (count > 0)
24910 {
24911 fprintf(fd, "%5d ", count);
24912 if (prefer_self && profile_equal(total, self))
24913 fprintf(fd, " ");
24914 else
24915 fprintf(fd, "%s ", profile_msg(total));
24916 if (!prefer_self && profile_equal(total, self))
24917 fprintf(fd, " ");
24918 else
24919 fprintf(fd, "%s ", profile_msg(self));
24920 }
24921 else
24922 fprintf(fd, " ");
24923}
24924
24925/*
24926 * Compare function for total time sorting.
24927 */
24928 static int
24929#ifdef __BORLANDC__
24930_RTLENTRYF
24931#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024932prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024933{
24934 ufunc_T *p1, *p2;
24935
24936 p1 = *(ufunc_T **)s1;
24937 p2 = *(ufunc_T **)s2;
24938 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24939}
24940
24941/*
24942 * Compare function for self time sorting.
24943 */
24944 static int
24945#ifdef __BORLANDC__
24946_RTLENTRYF
24947#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024948prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024949{
24950 ufunc_T *p1, *p2;
24951
24952 p1 = *(ufunc_T **)s1;
24953 p2 = *(ufunc_T **)s2;
24954 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24955}
24956
Bram Moolenaar05159a02005-02-26 23:04:13 +000024957#endif
24958
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024959/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024960 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024961 * Return TRUE if a package was loaded.
24962 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024963 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024964script_autoload(
24965 char_u *name,
24966 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024967{
24968 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024969 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024970 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024971 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024972
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024973 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024974 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024975 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024976 return FALSE;
24977
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024978 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024979
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024980 /* Find the name in the list of previously loaded package names. Skip
24981 * "autoload/", it's always the same. */
24982 for (i = 0; i < ga_loaded.ga_len; ++i)
24983 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24984 break;
24985 if (!reload && i < ga_loaded.ga_len)
24986 ret = FALSE; /* was loaded already */
24987 else
24988 {
24989 /* Remember the name if it wasn't loaded already. */
24990 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24991 {
24992 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24993 tofree = NULL;
24994 }
24995
24996 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024997 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024998 ret = TRUE;
24999 }
25000
25001 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025002 return ret;
25003}
25004
25005/*
25006 * Return the autoload script name for a function or variable name.
25007 * Returns NULL when out of memory.
25008 */
25009 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025010autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025011{
25012 char_u *p;
25013 char_u *scriptname;
25014
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025015 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025016 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25017 if (scriptname == NULL)
25018 return FALSE;
25019 STRCPY(scriptname, "autoload/");
25020 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025021 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025022 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025023 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025024 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025025 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025026}
25027
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25029
25030/*
25031 * Function given to ExpandGeneric() to obtain the list of user defined
25032 * function names.
25033 */
25034 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025035get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025036{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025037 static long_u done;
25038 static hashitem_T *hi;
25039 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025040
25041 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025043 done = 0;
25044 hi = func_hashtab.ht_array;
25045 }
25046 if (done < func_hashtab.ht_used)
25047 {
25048 if (done++ > 0)
25049 ++hi;
25050 while (HASHITEM_EMPTY(hi))
25051 ++hi;
25052 fp = HI2UF(hi);
25053
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025054 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025055 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025056
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025057 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25058 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025059
25060 cat_func_name(IObuff, fp);
25061 if (xp->xp_context != EXPAND_USER_FUNC)
25062 {
25063 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025064 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 STRCAT(IObuff, ")");
25066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025067 return IObuff;
25068 }
25069 return NULL;
25070}
25071
25072#endif /* FEAT_CMDL_COMPL */
25073
25074/*
25075 * Copy the function name of "fp" to buffer "buf".
25076 * "buf" must be able to hold the function name plus three bytes.
25077 * Takes care of script-local function names.
25078 */
25079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025080cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025081{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025082 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025083 {
25084 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025085 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025086 }
25087 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025088 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025089}
25090
25091/*
25092 * ":delfunction {name}"
25093 */
25094 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025095ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025096{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025097 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 char_u *p;
25099 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025100 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025101
25102 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025103 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025104 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025105 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025106 {
25107 if (fudi.fd_dict != NULL && !eap->skip)
25108 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025109 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025111 if (!ends_excmd(*skipwhite(p)))
25112 {
25113 vim_free(name);
25114 EMSG(_(e_trailing));
25115 return;
25116 }
25117 eap->nextcmd = check_nextcmd(p);
25118 if (eap->nextcmd != NULL)
25119 *p = NUL;
25120
25121 if (!eap->skip)
25122 fp = find_func(name);
25123 vim_free(name);
25124
25125 if (!eap->skip)
25126 {
25127 if (fp == NULL)
25128 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025129 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 return;
25131 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025132 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025133 {
25134 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25135 return;
25136 }
25137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025138 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025140 /* Delete the dict item that refers to the function, it will
25141 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025142 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025144 else
25145 func_free(fp);
25146 }
25147}
25148
25149/*
25150 * Free a function and remove it from the list of functions.
25151 */
25152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025153func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025154{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025155 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025156
25157 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025158 ga_clear_strings(&(fp->uf_args));
25159 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025160#ifdef FEAT_PROFILE
25161 vim_free(fp->uf_tml_count);
25162 vim_free(fp->uf_tml_total);
25163 vim_free(fp->uf_tml_self);
25164#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025165
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025166 /* remove the function from the function hashtable */
25167 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25168 if (HASHITEM_EMPTY(hi))
25169 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025170 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025171 hash_remove(&func_hashtab, hi);
25172
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025173 vim_free(fp);
25174}
25175
25176/*
25177 * Unreference a Function: decrement the reference count and free it when it
25178 * becomes zero. Only for numbered functions.
25179 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025181func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025182{
25183 ufunc_T *fp;
25184
25185 if (name != NULL && isdigit(*name))
25186 {
25187 fp = find_func(name);
25188 if (fp == NULL)
25189 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025190 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025191 {
25192 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025193 * when "uf_calls" becomes zero. */
25194 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025195 func_free(fp);
25196 }
25197 }
25198}
25199
25200/*
25201 * Count a reference to a Function.
25202 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025204func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025205{
25206 ufunc_T *fp;
25207
25208 if (name != NULL && isdigit(*name))
25209 {
25210 fp = find_func(name);
25211 if (fp == NULL)
25212 EMSG2(_(e_intern2), "func_ref()");
25213 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025214 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025215 }
25216}
25217
25218/*
25219 * Call a user function.
25220 */
25221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025222call_user_func(
25223 ufunc_T *fp, /* pointer to function */
25224 int argcount, /* nr of args */
25225 typval_T *argvars, /* arguments */
25226 typval_T *rettv, /* return value */
25227 linenr_T firstline, /* first line of range */
25228 linenr_T lastline, /* last line of range */
25229 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230{
Bram Moolenaar33570922005-01-25 22:26:29 +000025231 char_u *save_sourcing_name;
25232 linenr_T save_sourcing_lnum;
25233 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025234 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025235 int save_did_emsg;
25236 static int depth = 0;
25237 dictitem_T *v;
25238 int fixvar_idx = 0; /* index in fixvar[] */
25239 int i;
25240 int ai;
25241 char_u numbuf[NUMBUFLEN];
25242 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025243 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025244#ifdef FEAT_PROFILE
25245 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025246 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025248
25249 /* If depth of calling is getting too high, don't execute the function */
25250 if (depth >= p_mfd)
25251 {
25252 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025253 rettv->v_type = VAR_NUMBER;
25254 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025255 return;
25256 }
25257 ++depth;
25258
25259 line_breakcheck(); /* check for CTRL-C hit */
25260
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025261 fc = (funccall_T *)alloc(sizeof(funccall_T));
25262 fc->caller = current_funccal;
25263 current_funccal = fc;
25264 fc->func = fp;
25265 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025266 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025267 fc->linenr = 0;
25268 fc->returned = FALSE;
25269 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025271 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25272 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273
Bram Moolenaar33570922005-01-25 22:26:29 +000025274 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025275 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025276 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25277 * each argument variable and saves a lot of time.
25278 */
25279 /*
25280 * Init l: variables.
25281 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025282 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025283 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025284 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025285 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25286 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025287 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025288 name = v->di_key;
25289 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025290 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025291 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025293 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025294 v->di_tv.vval.v_dict = selfdict;
25295 ++selfdict->dv_refcount;
25296 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025297
Bram Moolenaar33570922005-01-25 22:26:29 +000025298 /*
25299 * Init a: variables.
25300 * Set a:0 to "argcount".
25301 * Set a:000 to a list with room for the "..." arguments.
25302 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025303 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025304 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025305 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025306 /* Use "name" to avoid a warning from some compiler that checks the
25307 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025308 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025309 name = v->di_key;
25310 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025311 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025312 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025313 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025314 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025315 v->di_tv.vval.v_list = &fc->l_varlist;
25316 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25317 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25318 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025319
25320 /*
25321 * Set a:firstline to "firstline" and a:lastline to "lastline".
25322 * Set a:name to named arguments.
25323 * Set a:N to the "..." arguments.
25324 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025325 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025326 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025327 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025328 (varnumber_T)lastline);
25329 for (i = 0; i < argcount; ++i)
25330 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025331 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025332 if (ai < 0)
25333 /* named argument a:name */
25334 name = FUNCARG(fp, i);
25335 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025336 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025337 /* "..." argument a:1, a:2, etc. */
25338 sprintf((char *)numbuf, "%d", ai + 1);
25339 name = numbuf;
25340 }
25341 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25342 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025343 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025344 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25345 }
25346 else
25347 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025348 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25349 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025350 if (v == NULL)
25351 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025352 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025353 }
25354 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025355 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025356
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025357 /* Note: the values are copied directly to avoid alloc/free.
25358 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025359 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025360 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025361
25362 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25363 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025364 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25365 fc->l_listitems[ai].li_tv = argvars[i];
25366 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025367 }
25368 }
25369
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370 /* Don't redraw while executing the function. */
25371 ++RedrawingDisabled;
25372 save_sourcing_name = sourcing_name;
25373 save_sourcing_lnum = sourcing_lnum;
25374 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025375 /* need space for function name + ("function " + 3) or "[number]" */
25376 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25377 + STRLEN(fp->uf_name) + 20;
25378 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 if (sourcing_name != NULL)
25380 {
25381 if (save_sourcing_name != NULL
25382 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025383 sprintf((char *)sourcing_name, "%s[%d]..",
25384 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385 else
25386 STRCPY(sourcing_name, "function ");
25387 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25388
25389 if (p_verbose >= 12)
25390 {
25391 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025392 verbose_enter_scroll();
25393
Bram Moolenaar555b2802005-05-19 21:08:39 +000025394 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 if (p_verbose >= 14)
25396 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025398 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025399 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025400 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401
25402 msg_puts((char_u *)"(");
25403 for (i = 0; i < argcount; ++i)
25404 {
25405 if (i > 0)
25406 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025407 if (argvars[i].v_type == VAR_NUMBER)
25408 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 else
25410 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025411 /* Do not want errors such as E724 here. */
25412 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025413 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025414 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025415 if (s != NULL)
25416 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025417 if (vim_strsize(s) > MSG_BUF_CLEN)
25418 {
25419 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25420 s = buf;
25421 }
25422 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025423 vim_free(tofree);
25424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025425 }
25426 }
25427 msg_puts((char_u *)")");
25428 }
25429 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025430
25431 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432 --no_wait_return;
25433 }
25434 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025435#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025436 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025437 {
25438 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25439 func_do_profile(fp);
25440 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025441 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025442 {
25443 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025444 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445 profile_zero(&fp->uf_tm_children);
25446 }
25447 script_prof_save(&wait_start);
25448 }
25449#endif
25450
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025452 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 save_did_emsg = did_emsg;
25454 did_emsg = FALSE;
25455
25456 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025457 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25459
25460 --RedrawingDisabled;
25461
25462 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025463 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025464 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025465 clear_tv(rettv);
25466 rettv->v_type = VAR_NUMBER;
25467 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 }
25469
Bram Moolenaar05159a02005-02-26 23:04:13 +000025470#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025471 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025472 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025473 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025474 profile_end(&call_start);
25475 profile_sub_wait(&wait_start, &call_start);
25476 profile_add(&fp->uf_tm_total, &call_start);
25477 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025478 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025479 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025480 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25481 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025482 }
25483 }
25484#endif
25485
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 /* when being verbose, mention the return value */
25487 if (p_verbose >= 12)
25488 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025489 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025490 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491
Bram Moolenaar071d4272004-06-13 20:20:40 +000025492 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025493 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025494 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025495 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025496 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025498 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025499 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025500 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025501 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025502 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025503
Bram Moolenaar555b2802005-05-19 21:08:39 +000025504 /* The value may be very long. Skip the middle part, so that we
25505 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025506 * truncate it at the end. Don't want errors such as E724 here. */
25507 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025508 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025509 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025510 if (s != NULL)
25511 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025512 if (vim_strsize(s) > MSG_BUF_CLEN)
25513 {
25514 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25515 s = buf;
25516 }
25517 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025518 vim_free(tofree);
25519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025520 }
25521 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025522
25523 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025524 --no_wait_return;
25525 }
25526
25527 vim_free(sourcing_name);
25528 sourcing_name = save_sourcing_name;
25529 sourcing_lnum = save_sourcing_lnum;
25530 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025531#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025532 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025533 script_prof_restore(&wait_start);
25534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535
25536 if (p_verbose >= 12 && sourcing_name != NULL)
25537 {
25538 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025539 verbose_enter_scroll();
25540
Bram Moolenaar555b2802005-05-19 21:08:39 +000025541 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025543
25544 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025545 --no_wait_return;
25546 }
25547
25548 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025549 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025550 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025551
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025552 /* If the a:000 list and the l: and a: dicts are not referenced we can
25553 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025554 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25555 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25556 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25557 {
25558 free_funccal(fc, FALSE);
25559 }
25560 else
25561 {
25562 hashitem_T *hi;
25563 listitem_T *li;
25564 int todo;
25565
25566 /* "fc" is still in use. This can happen when returning "a:000" or
25567 * assigning "l:" to a global variable.
25568 * Link "fc" in the list for garbage collection later. */
25569 fc->caller = previous_funccal;
25570 previous_funccal = fc;
25571
25572 /* Make a copy of the a: variables, since we didn't do that above. */
25573 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25574 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25575 {
25576 if (!HASHITEM_EMPTY(hi))
25577 {
25578 --todo;
25579 v = HI2DI(hi);
25580 copy_tv(&v->di_tv, &v->di_tv);
25581 }
25582 }
25583
25584 /* Make a copy of the a:000 items, since we didn't do that above. */
25585 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25586 copy_tv(&li->li_tv, &li->li_tv);
25587 }
25588}
25589
25590/*
25591 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025592 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025593 */
25594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025595can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025596{
25597 return (fc->l_varlist.lv_copyID != copyID
25598 && fc->l_vars.dv_copyID != copyID
25599 && fc->l_avars.dv_copyID != copyID);
25600}
25601
25602/*
25603 * Free "fc" and what it contains.
25604 */
25605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025606free_funccal(
25607 funccall_T *fc,
25608 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025609{
25610 listitem_T *li;
25611
25612 /* The a: variables typevals may not have been allocated, only free the
25613 * allocated variables. */
25614 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25615
25616 /* free all l: variables */
25617 vars_clear(&fc->l_vars.dv_hashtab);
25618
25619 /* Free the a:000 variables if they were allocated. */
25620 if (free_val)
25621 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25622 clear_tv(&li->li_tv);
25623
25624 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025625}
25626
25627/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025628 * Add a number variable "name" to dict "dp" with value "nr".
25629 */
25630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025631add_nr_var(
25632 dict_T *dp,
25633 dictitem_T *v,
25634 char *name,
25635 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025636{
25637 STRCPY(v->di_key, name);
25638 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25639 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25640 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025641 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025642 v->di_tv.vval.v_number = nr;
25643}
25644
25645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025646 * ":return [expr]"
25647 */
25648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025649ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650{
25651 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025652 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025653 int returning = FALSE;
25654
25655 if (current_funccal == NULL)
25656 {
25657 EMSG(_("E133: :return not inside a function"));
25658 return;
25659 }
25660
25661 if (eap->skip)
25662 ++emsg_skip;
25663
25664 eap->nextcmd = NULL;
25665 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025666 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 {
25668 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025669 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025671 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025672 }
25673 /* It's safer to return also on error. */
25674 else if (!eap->skip)
25675 {
25676 /*
25677 * Return unless the expression evaluation has been cancelled due to an
25678 * aborting error, an interrupt, or an exception.
25679 */
25680 if (!aborting())
25681 returning = do_return(eap, FALSE, TRUE, NULL);
25682 }
25683
25684 /* When skipping or the return gets pending, advance to the next command
25685 * in this line (!returning). Otherwise, ignore the rest of the line.
25686 * Following lines will be ignored by get_func_line(). */
25687 if (returning)
25688 eap->nextcmd = NULL;
25689 else if (eap->nextcmd == NULL) /* no argument */
25690 eap->nextcmd = check_nextcmd(arg);
25691
25692 if (eap->skip)
25693 --emsg_skip;
25694}
25695
25696/*
25697 * Return from a function. Possibly makes the return pending. Also called
25698 * for a pending return at the ":endtry" or after returning from an extra
25699 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025700 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025701 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025702 * FALSE when the return gets pending.
25703 */
25704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025705do_return(
25706 exarg_T *eap,
25707 int reanimate,
25708 int is_cmd,
25709 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710{
25711 int idx;
25712 struct condstack *cstack = eap->cstack;
25713
25714 if (reanimate)
25715 /* Undo the return. */
25716 current_funccal->returned = FALSE;
25717
25718 /*
25719 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25720 * not in its finally clause (which then is to be executed next) is found.
25721 * In this case, make the ":return" pending for execution at the ":endtry".
25722 * Otherwise, return normally.
25723 */
25724 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25725 if (idx >= 0)
25726 {
25727 cstack->cs_pending[idx] = CSTP_RETURN;
25728
25729 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025730 /* A pending return again gets pending. "rettv" points to an
25731 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025733 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 else
25735 {
25736 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025737 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025738 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025739 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025741 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742 {
25743 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025744 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025745 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025746 else
25747 EMSG(_(e_outofmem));
25748 }
25749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025750 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025751
25752 if (reanimate)
25753 {
25754 /* The pending return value could be overwritten by a ":return"
25755 * without argument in a finally clause; reset the default
25756 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025757 current_funccal->rettv->v_type = VAR_NUMBER;
25758 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025759 }
25760 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025761 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 }
25763 else
25764 {
25765 current_funccal->returned = TRUE;
25766
25767 /* If the return is carried out now, store the return value. For
25768 * a return immediately after reanimation, the value is already
25769 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025770 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025772 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025773 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025775 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 }
25777 }
25778
25779 return idx < 0;
25780}
25781
25782/*
25783 * Free the variable with a pending return value.
25784 */
25785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025786discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025787{
Bram Moolenaar33570922005-01-25 22:26:29 +000025788 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789}
25790
25791/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025792 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025793 * is an allocated string. Used by report_pending() for verbose messages.
25794 */
25795 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025796get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025797{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025798 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025799 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025800 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025801
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025802 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025803 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025804 if (s == NULL)
25805 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025806
25807 STRCPY(IObuff, ":return ");
25808 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25809 if (STRLEN(s) + 8 >= IOSIZE)
25810 STRCPY(IObuff + IOSIZE - 4, "...");
25811 vim_free(tofree);
25812 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025813}
25814
25815/*
25816 * Get next function line.
25817 * Called by do_cmdline() to get the next line.
25818 * Returns allocated string, or NULL for end of function.
25819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025821get_func_line(
25822 int c UNUSED,
25823 void *cookie,
25824 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025825{
Bram Moolenaar33570922005-01-25 22:26:29 +000025826 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025827 ufunc_T *fp = fcp->func;
25828 char_u *retval;
25829 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025830
25831 /* If breakpoints have been added/deleted need to check for it. */
25832 if (fcp->dbg_tick != debug_tick)
25833 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025834 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025835 sourcing_lnum);
25836 fcp->dbg_tick = debug_tick;
25837 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025838#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025839 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025840 func_line_end(cookie);
25841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025842
Bram Moolenaar05159a02005-02-26 23:04:13 +000025843 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025844 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25845 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025846 retval = NULL;
25847 else
25848 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025849 /* Skip NULL lines (continuation lines). */
25850 while (fcp->linenr < gap->ga_len
25851 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25852 ++fcp->linenr;
25853 if (fcp->linenr >= gap->ga_len)
25854 retval = NULL;
25855 else
25856 {
25857 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25858 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025859#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025860 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025861 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025862#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025864 }
25865
25866 /* Did we encounter a breakpoint? */
25867 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25868 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025869 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025871 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872 sourcing_lnum);
25873 fcp->dbg_tick = debug_tick;
25874 }
25875
25876 return retval;
25877}
25878
Bram Moolenaar05159a02005-02-26 23:04:13 +000025879#if defined(FEAT_PROFILE) || defined(PROTO)
25880/*
25881 * Called when starting to read a function line.
25882 * "sourcing_lnum" must be correct!
25883 * When skipping lines it may not actually be executed, but we won't find out
25884 * until later and we need to store the time now.
25885 */
25886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025887func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025888{
25889 funccall_T *fcp = (funccall_T *)cookie;
25890 ufunc_T *fp = fcp->func;
25891
25892 if (fp->uf_profiling && sourcing_lnum >= 1
25893 && sourcing_lnum <= fp->uf_lines.ga_len)
25894 {
25895 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025896 /* Skip continuation lines. */
25897 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25898 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025899 fp->uf_tml_execed = FALSE;
25900 profile_start(&fp->uf_tml_start);
25901 profile_zero(&fp->uf_tml_children);
25902 profile_get_wait(&fp->uf_tml_wait);
25903 }
25904}
25905
25906/*
25907 * Called when actually executing a function line.
25908 */
25909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025910func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025911{
25912 funccall_T *fcp = (funccall_T *)cookie;
25913 ufunc_T *fp = fcp->func;
25914
25915 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25916 fp->uf_tml_execed = TRUE;
25917}
25918
25919/*
25920 * Called when done with a function line.
25921 */
25922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025923func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025924{
25925 funccall_T *fcp = (funccall_T *)cookie;
25926 ufunc_T *fp = fcp->func;
25927
25928 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25929 {
25930 if (fp->uf_tml_execed)
25931 {
25932 ++fp->uf_tml_count[fp->uf_tml_idx];
25933 profile_end(&fp->uf_tml_start);
25934 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025935 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025936 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25937 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025938 }
25939 fp->uf_tml_idx = -1;
25940 }
25941}
25942#endif
25943
Bram Moolenaar071d4272004-06-13 20:20:40 +000025944/*
25945 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025946 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 */
25948 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025949func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950{
Bram Moolenaar33570922005-01-25 22:26:29 +000025951 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952
25953 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25954 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025955 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025956 || fcp->returned);
25957}
25958
25959/*
25960 * return TRUE if cookie indicates a function which "abort"s on errors.
25961 */
25962 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025963func_has_abort(
25964 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025966 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025967}
25968
25969#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25970typedef enum
25971{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025972 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25973 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25974 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025975} var_flavour_T;
25976
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025977static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978
25979 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025980var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025981{
25982 char_u *p = varname;
25983
25984 if (ASCII_ISUPPER(*p))
25985 {
25986 while (*(++p))
25987 if (ASCII_ISLOWER(*p))
25988 return VAR_FLAVOUR_SESSION;
25989 return VAR_FLAVOUR_VIMINFO;
25990 }
25991 else
25992 return VAR_FLAVOUR_DEFAULT;
25993}
25994#endif
25995
25996#if defined(FEAT_VIMINFO) || defined(PROTO)
25997/*
25998 * Restore global vars that start with a capital from the viminfo file
25999 */
26000 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026001read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002{
26003 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026004 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026005 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026006 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026007
26008 if (!writing && (find_viminfo_parameter('!') != NULL))
26009 {
26010 tab = vim_strchr(virp->vir_line + 1, '\t');
26011 if (tab != NULL)
26012 {
26013 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026014 switch (*tab)
26015 {
26016 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026017#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026018 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026019#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026020 case 'D': type = VAR_DICT; break;
26021 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026022 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026024
26025 tab = vim_strchr(tab, '\t');
26026 if (tab != NULL)
26027 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026028 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026029 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026030 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026032#ifdef FEAT_FLOAT
26033 else if (type == VAR_FLOAT)
26034 (void)string2float(tab + 1, &tv.vval.v_float);
26035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026036 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026037 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026038 if (type == VAR_DICT || type == VAR_LIST)
26039 {
26040 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26041
26042 if (etv == NULL)
26043 /* Failed to parse back the dict or list, use it as a
26044 * string. */
26045 tv.v_type = VAR_STRING;
26046 else
26047 {
26048 vim_free(tv.vval.v_string);
26049 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026050 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026051 }
26052 }
26053
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026054 /* when in a function use global variables */
26055 save_funccal = current_funccal;
26056 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026057 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026058 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026059
26060 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026061 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026062 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26063 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064 }
26065 }
26066 }
26067
26068 return viminfo_readline(virp);
26069}
26070
26071/*
26072 * Write global vars that start with a capital to the viminfo file
26073 */
26074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026075write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026076{
Bram Moolenaar33570922005-01-25 22:26:29 +000026077 hashitem_T *hi;
26078 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026079 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026080 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026081 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026082 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026083 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084
26085 if (find_viminfo_parameter('!') == NULL)
26086 return;
26087
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026088 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026090 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026091 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026093 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026095 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026096 this_var = HI2DI(hi);
26097 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026098 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026099 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026100 {
26101 case VAR_STRING: s = "STR"; break;
26102 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026103 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026104 case VAR_DICT: s = "DIC"; break;
26105 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026106 case VAR_SPECIAL: s = "XPL"; break;
26107
26108 case VAR_UNKNOWN:
26109 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026110 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026111 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026112 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026113 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026114 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026115 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026116 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026117 if (p != NULL)
26118 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026119 vim_free(tofree);
26120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 }
26122 }
26123}
26124#endif
26125
26126#if defined(FEAT_SESSION) || defined(PROTO)
26127 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026128store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026129{
Bram Moolenaar33570922005-01-25 22:26:29 +000026130 hashitem_T *hi;
26131 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026132 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133 char_u *p, *t;
26134
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026135 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026136 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026138 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026139 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026140 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026141 this_var = HI2DI(hi);
26142 if ((this_var->di_tv.v_type == VAR_NUMBER
26143 || this_var->di_tv.v_type == VAR_STRING)
26144 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026145 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026146 /* Escape special characters with a backslash. Turn a LF and
26147 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026148 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026149 (char_u *)"\\\"\n\r");
26150 if (p == NULL) /* out of memory */
26151 break;
26152 for (t = p; *t != NUL; ++t)
26153 if (*t == '\n')
26154 *t = 'n';
26155 else if (*t == '\r')
26156 *t = 'r';
26157 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026158 this_var->di_key,
26159 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26160 : ' ',
26161 p,
26162 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26163 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026164 || put_eol(fd) == FAIL)
26165 {
26166 vim_free(p);
26167 return FAIL;
26168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 vim_free(p);
26170 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026171#ifdef FEAT_FLOAT
26172 else if (this_var->di_tv.v_type == VAR_FLOAT
26173 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26174 {
26175 float_T f = this_var->di_tv.vval.v_float;
26176 int sign = ' ';
26177
26178 if (f < 0)
26179 {
26180 f = -f;
26181 sign = '-';
26182 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026183 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026184 this_var->di_key, sign, f) < 0)
26185 || put_eol(fd) == FAIL)
26186 return FAIL;
26187 }
26188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189 }
26190 }
26191 return OK;
26192}
26193#endif
26194
Bram Moolenaar661b1822005-07-28 22:36:45 +000026195/*
26196 * Display script name where an item was last set.
26197 * Should only be invoked when 'verbose' is non-zero.
26198 */
26199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026200last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026201{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026202 char_u *p;
26203
Bram Moolenaar661b1822005-07-28 22:36:45 +000026204 if (scriptID != 0)
26205 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026206 p = home_replace_save(NULL, get_scriptname(scriptID));
26207 if (p != NULL)
26208 {
26209 verbose_enter();
26210 MSG_PUTS(_("\n\tLast set from "));
26211 MSG_PUTS(p);
26212 vim_free(p);
26213 verbose_leave();
26214 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026215 }
26216}
26217
Bram Moolenaard812df62008-11-09 12:46:09 +000026218/*
26219 * List v:oldfiles in a nice way.
26220 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026222ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026223{
26224 list_T *l = vimvars[VV_OLDFILES].vv_list;
26225 listitem_T *li;
26226 int nr = 0;
26227
26228 if (l == NULL)
26229 msg((char_u *)_("No old files"));
26230 else
26231 {
26232 msg_start();
26233 msg_scroll = TRUE;
26234 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26235 {
26236 msg_outnum((long)++nr);
26237 MSG_PUTS(": ");
26238 msg_outtrans(get_tv_string(&li->li_tv));
26239 msg_putchar('\n');
26240 out_flush(); /* output one line at a time */
26241 ui_breakcheck();
26242 }
26243 /* Assume "got_int" was set to truncate the listing. */
26244 got_int = FALSE;
26245
26246#ifdef FEAT_BROWSE_CMD
26247 if (cmdmod.browse)
26248 {
26249 quit_more = FALSE;
26250 nr = prompt_for_number(FALSE);
26251 msg_starthere();
26252 if (nr > 0)
26253 {
26254 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26255 (long)nr);
26256
26257 if (p != NULL)
26258 {
26259 p = expand_env_save(p);
26260 eap->arg = p;
26261 eap->cmdidx = CMD_edit;
26262 cmdmod.browse = FALSE;
26263 do_exedit(eap, NULL);
26264 vim_free(p);
26265 }
26266 }
26267 }
26268#endif
26269 }
26270}
26271
Bram Moolenaar53744302015-07-17 17:38:22 +020026272/* reset v:option_new, v:option_old and v:option_type */
26273 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026274reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026275{
26276 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26277 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26278 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26279}
26280
26281
Bram Moolenaar071d4272004-06-13 20:20:40 +000026282#endif /* FEAT_EVAL */
26283
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026285#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026286
26287#ifdef WIN3264
26288/*
26289 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26290 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026291static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26292static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26293static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026294
26295/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026296 * Get the short path (8.3) for the filename in "fnamep".
26297 * Only works for a valid file name.
26298 * When the path gets longer "fnamep" is changed and the allocated buffer
26299 * is put in "bufp".
26300 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26301 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026302 */
26303 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026304get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026306 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307 char_u *newbuf;
26308
26309 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026310 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026311 if (l > len - 1)
26312 {
26313 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026314 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026315 newbuf = vim_strnsave(*fnamep, l);
26316 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026317 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026318
26319 vim_free(*bufp);
26320 *fnamep = *bufp = newbuf;
26321
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026322 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026323 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026324 }
26325
26326 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026327 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026328}
26329
26330/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026331 * Get the short path (8.3) for the filename in "fname". The converted
26332 * path is returned in "bufp".
26333 *
26334 * Some of the directories specified in "fname" may not exist. This function
26335 * will shorten the existing directories at the beginning of the path and then
26336 * append the remaining non-existing path.
26337 *
26338 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026339 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026340 * bufp - Pointer to an allocated buffer for the filename.
26341 * fnamelen - Length of the filename pointed to by fname
26342 *
26343 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026344 */
26345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026346shortpath_for_invalid_fname(
26347 char_u **fname,
26348 char_u **bufp,
26349 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026350{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026351 char_u *short_fname, *save_fname, *pbuf_unused;
26352 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026354 int old_len, len;
26355 int new_len, sfx_len;
26356 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026357
26358 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026359 old_len = *fnamelen;
26360 save_fname = vim_strnsave(*fname, old_len);
26361 pbuf_unused = NULL;
26362 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026363
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026364 endp = save_fname + old_len - 1; /* Find the end of the copy */
26365 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026366
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026367 /*
26368 * Try shortening the supplied path till it succeeds by removing one
26369 * directory at a time from the tail of the path.
26370 */
26371 len = 0;
26372 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026373 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026374 /* go back one path-separator */
26375 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26376 --endp;
26377 if (endp <= save_fname)
26378 break; /* processed the complete path */
26379
26380 /*
26381 * Replace the path separator with a NUL and try to shorten the
26382 * resulting path.
26383 */
26384 ch = *endp;
26385 *endp = 0;
26386 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026387 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026388 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26389 {
26390 retval = FAIL;
26391 goto theend;
26392 }
26393 *endp = ch; /* preserve the string */
26394
26395 if (len > 0)
26396 break; /* successfully shortened the path */
26397
26398 /* failed to shorten the path. Skip the path separator */
26399 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026400 }
26401
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026402 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026403 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026404 /*
26405 * Succeeded in shortening the path. Now concatenate the shortened
26406 * path with the remaining path at the tail.
26407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026408
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026409 /* Compute the length of the new path. */
26410 sfx_len = (int)(save_endp - endp) + 1;
26411 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026412
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026413 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026415 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026416 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026417 /* There is not enough space in the currently allocated string,
26418 * copy it to a buffer big enough. */
26419 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026421 {
26422 retval = FAIL;
26423 goto theend;
26424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026425 }
26426 else
26427 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026428 /* Transfer short_fname to the main buffer (it's big enough),
26429 * unless get_short_pathname() did its work in-place. */
26430 *fname = *bufp = save_fname;
26431 if (short_fname != save_fname)
26432 vim_strncpy(save_fname, short_fname, len);
26433 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026434 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026435
26436 /* concat the not-shortened part of the path */
26437 vim_strncpy(*fname + len, endp, sfx_len);
26438 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026439 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026440
26441theend:
26442 vim_free(pbuf_unused);
26443 vim_free(save_fname);
26444
26445 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026446}
26447
26448/*
26449 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026450 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026451 */
26452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026453shortpath_for_partial(
26454 char_u **fnamep,
26455 char_u **bufp,
26456 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026457{
26458 int sepcount, len, tflen;
26459 char_u *p;
26460 char_u *pbuf, *tfname;
26461 int hasTilde;
26462
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026463 /* Count up the path separators from the RHS.. so we know which part
26464 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026465 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026466 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026467 if (vim_ispathsep(*p))
26468 ++sepcount;
26469
26470 /* Need full path first (use expand_env() to remove a "~/") */
26471 hasTilde = (**fnamep == '~');
26472 if (hasTilde)
26473 pbuf = tfname = expand_env_save(*fnamep);
26474 else
26475 pbuf = tfname = FullName_save(*fnamep, FALSE);
26476
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026477 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026478
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026479 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26480 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026481
26482 if (len == 0)
26483 {
26484 /* Don't have a valid filename, so shorten the rest of the
26485 * path if we can. This CAN give us invalid 8.3 filenames, but
26486 * there's not a lot of point in guessing what it might be.
26487 */
26488 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026489 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26490 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026491 }
26492
26493 /* Count the paths backward to find the beginning of the desired string. */
26494 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026495 {
26496#ifdef FEAT_MBYTE
26497 if (has_mbyte)
26498 p -= mb_head_off(tfname, p);
26499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026500 if (vim_ispathsep(*p))
26501 {
26502 if (sepcount == 0 || (hasTilde && sepcount == 1))
26503 break;
26504 else
26505 sepcount --;
26506 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026508 if (hasTilde)
26509 {
26510 --p;
26511 if (p >= tfname)
26512 *p = '~';
26513 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026514 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026515 }
26516 else
26517 ++p;
26518
26519 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26520 vim_free(*bufp);
26521 *fnamelen = (int)STRLEN(p);
26522 *bufp = pbuf;
26523 *fnamep = p;
26524
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026525 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526}
26527#endif /* WIN3264 */
26528
26529/*
26530 * Adjust a filename, according to a string of modifiers.
26531 * *fnamep must be NUL terminated when called. When returning, the length is
26532 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026533 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534 * When there is an error, *fnamep is set to NULL.
26535 */
26536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026537modify_fname(
26538 char_u *src, /* string with modifiers */
26539 int *usedlen, /* characters after src that are used */
26540 char_u **fnamep, /* file name so far */
26541 char_u **bufp, /* buffer for allocated file name or NULL */
26542 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026543{
26544 int valid = 0;
26545 char_u *tail;
26546 char_u *s, *p, *pbuf;
26547 char_u dirname[MAXPATHL];
26548 int c;
26549 int has_fullname = 0;
26550#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026551 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026552 int has_shortname = 0;
26553#endif
26554
26555repeat:
26556 /* ":p" - full path/file_name */
26557 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26558 {
26559 has_fullname = 1;
26560
26561 valid |= VALID_PATH;
26562 *usedlen += 2;
26563
26564 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26565 if ((*fnamep)[0] == '~'
26566#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26567 && ((*fnamep)[1] == '/'
26568# ifdef BACKSLASH_IN_FILENAME
26569 || (*fnamep)[1] == '\\'
26570# endif
26571 || (*fnamep)[1] == NUL)
26572
26573#endif
26574 )
26575 {
26576 *fnamep = expand_env_save(*fnamep);
26577 vim_free(*bufp); /* free any allocated file name */
26578 *bufp = *fnamep;
26579 if (*fnamep == NULL)
26580 return -1;
26581 }
26582
26583 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026584 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026585 {
26586 if (vim_ispathsep(*p)
26587 && p[1] == '.'
26588 && (p[2] == NUL
26589 || vim_ispathsep(p[2])
26590 || (p[2] == '.'
26591 && (p[3] == NUL || vim_ispathsep(p[3])))))
26592 break;
26593 }
26594
26595 /* FullName_save() is slow, don't use it when not needed. */
26596 if (*p != NUL || !vim_isAbsName(*fnamep))
26597 {
26598 *fnamep = FullName_save(*fnamep, *p != NUL);
26599 vim_free(*bufp); /* free any allocated file name */
26600 *bufp = *fnamep;
26601 if (*fnamep == NULL)
26602 return -1;
26603 }
26604
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026605#ifdef WIN3264
26606# if _WIN32_WINNT >= 0x0500
26607 if (vim_strchr(*fnamep, '~') != NULL)
26608 {
26609 /* Expand 8.3 filename to full path. Needed to make sure the same
26610 * file does not have two different names.
26611 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26612 p = alloc(_MAX_PATH + 1);
26613 if (p != NULL)
26614 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026615 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026616 {
26617 vim_free(*bufp);
26618 *bufp = *fnamep = p;
26619 }
26620 else
26621 vim_free(p);
26622 }
26623 }
26624# endif
26625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026626 /* Append a path separator to a directory. */
26627 if (mch_isdir(*fnamep))
26628 {
26629 /* Make room for one or two extra characters. */
26630 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26631 vim_free(*bufp); /* free any allocated file name */
26632 *bufp = *fnamep;
26633 if (*fnamep == NULL)
26634 return -1;
26635 add_pathsep(*fnamep);
26636 }
26637 }
26638
26639 /* ":." - path relative to the current directory */
26640 /* ":~" - path relative to the home directory */
26641 /* ":8" - shortname path - postponed till after */
26642 while (src[*usedlen] == ':'
26643 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26644 {
26645 *usedlen += 2;
26646 if (c == '8')
26647 {
26648#ifdef WIN3264
26649 has_shortname = 1; /* Postpone this. */
26650#endif
26651 continue;
26652 }
26653 pbuf = NULL;
26654 /* Need full path first (use expand_env() to remove a "~/") */
26655 if (!has_fullname)
26656 {
26657 if (c == '.' && **fnamep == '~')
26658 p = pbuf = expand_env_save(*fnamep);
26659 else
26660 p = pbuf = FullName_save(*fnamep, FALSE);
26661 }
26662 else
26663 p = *fnamep;
26664
26665 has_fullname = 0;
26666
26667 if (p != NULL)
26668 {
26669 if (c == '.')
26670 {
26671 mch_dirname(dirname, MAXPATHL);
26672 s = shorten_fname(p, dirname);
26673 if (s != NULL)
26674 {
26675 *fnamep = s;
26676 if (pbuf != NULL)
26677 {
26678 vim_free(*bufp); /* free any allocated file name */
26679 *bufp = pbuf;
26680 pbuf = NULL;
26681 }
26682 }
26683 }
26684 else
26685 {
26686 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26687 /* Only replace it when it starts with '~' */
26688 if (*dirname == '~')
26689 {
26690 s = vim_strsave(dirname);
26691 if (s != NULL)
26692 {
26693 *fnamep = s;
26694 vim_free(*bufp);
26695 *bufp = s;
26696 }
26697 }
26698 }
26699 vim_free(pbuf);
26700 }
26701 }
26702
26703 tail = gettail(*fnamep);
26704 *fnamelen = (int)STRLEN(*fnamep);
26705
26706 /* ":h" - head, remove "/file_name", can be repeated */
26707 /* Don't remove the first "/" or "c:\" */
26708 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26709 {
26710 valid |= VALID_HEAD;
26711 *usedlen += 2;
26712 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026713 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026714 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026715 *fnamelen = (int)(tail - *fnamep);
26716#ifdef VMS
26717 if (*fnamelen > 0)
26718 *fnamelen += 1; /* the path separator is part of the path */
26719#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026720 if (*fnamelen == 0)
26721 {
26722 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26723 p = vim_strsave((char_u *)".");
26724 if (p == NULL)
26725 return -1;
26726 vim_free(*bufp);
26727 *bufp = *fnamep = tail = p;
26728 *fnamelen = 1;
26729 }
26730 else
26731 {
26732 while (tail > s && !after_pathsep(s, tail))
26733 mb_ptr_back(*fnamep, tail);
26734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026735 }
26736
26737 /* ":8" - shortname */
26738 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26739 {
26740 *usedlen += 2;
26741#ifdef WIN3264
26742 has_shortname = 1;
26743#endif
26744 }
26745
26746#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026747 /*
26748 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026749 */
26750 if (has_shortname)
26751 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026752 /* Copy the string if it is shortened by :h and when it wasn't copied
26753 * yet, because we are going to change it in place. Avoids changing
26754 * the buffer name for "%:8". */
26755 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026756 {
26757 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026758 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026759 return -1;
26760 vim_free(*bufp);
26761 *bufp = *fnamep = p;
26762 }
26763
26764 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026765 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026766 if (!has_fullname && !vim_isAbsName(*fnamep))
26767 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026768 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026769 return -1;
26770 }
26771 else
26772 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026773 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026774
Bram Moolenaardc935552011-08-17 15:23:23 +020026775 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026776 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026777 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026778 return -1;
26779
26780 if (l == 0)
26781 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026782 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026783 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026784 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026785 return -1;
26786 }
26787 *fnamelen = l;
26788 }
26789 }
26790#endif /* WIN3264 */
26791
26792 /* ":t" - tail, just the basename */
26793 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26794 {
26795 *usedlen += 2;
26796 *fnamelen -= (int)(tail - *fnamep);
26797 *fnamep = tail;
26798 }
26799
26800 /* ":e" - extension, can be repeated */
26801 /* ":r" - root, without extension, can be repeated */
26802 while (src[*usedlen] == ':'
26803 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26804 {
26805 /* find a '.' in the tail:
26806 * - for second :e: before the current fname
26807 * - otherwise: The last '.'
26808 */
26809 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26810 s = *fnamep - 2;
26811 else
26812 s = *fnamep + *fnamelen - 1;
26813 for ( ; s > tail; --s)
26814 if (s[0] == '.')
26815 break;
26816 if (src[*usedlen + 1] == 'e') /* :e */
26817 {
26818 if (s > tail)
26819 {
26820 *fnamelen += (int)(*fnamep - (s + 1));
26821 *fnamep = s + 1;
26822#ifdef VMS
26823 /* cut version from the extension */
26824 s = *fnamep + *fnamelen - 1;
26825 for ( ; s > *fnamep; --s)
26826 if (s[0] == ';')
26827 break;
26828 if (s > *fnamep)
26829 *fnamelen = s - *fnamep;
26830#endif
26831 }
26832 else if (*fnamep <= tail)
26833 *fnamelen = 0;
26834 }
26835 else /* :r */
26836 {
26837 if (s > tail) /* remove one extension */
26838 *fnamelen = (int)(s - *fnamep);
26839 }
26840 *usedlen += 2;
26841 }
26842
26843 /* ":s?pat?foo?" - substitute */
26844 /* ":gs?pat?foo?" - global substitute */
26845 if (src[*usedlen] == ':'
26846 && (src[*usedlen + 1] == 's'
26847 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26848 {
26849 char_u *str;
26850 char_u *pat;
26851 char_u *sub;
26852 int sep;
26853 char_u *flags;
26854 int didit = FALSE;
26855
26856 flags = (char_u *)"";
26857 s = src + *usedlen + 2;
26858 if (src[*usedlen + 1] == 'g')
26859 {
26860 flags = (char_u *)"g";
26861 ++s;
26862 }
26863
26864 sep = *s++;
26865 if (sep)
26866 {
26867 /* find end of pattern */
26868 p = vim_strchr(s, sep);
26869 if (p != NULL)
26870 {
26871 pat = vim_strnsave(s, (int)(p - s));
26872 if (pat != NULL)
26873 {
26874 s = p + 1;
26875 /* find end of substitution */
26876 p = vim_strchr(s, sep);
26877 if (p != NULL)
26878 {
26879 sub = vim_strnsave(s, (int)(p - s));
26880 str = vim_strnsave(*fnamep, *fnamelen);
26881 if (sub != NULL && str != NULL)
26882 {
26883 *usedlen = (int)(p + 1 - src);
26884 s = do_string_sub(str, pat, sub, flags);
26885 if (s != NULL)
26886 {
26887 *fnamep = s;
26888 *fnamelen = (int)STRLEN(s);
26889 vim_free(*bufp);
26890 *bufp = s;
26891 didit = TRUE;
26892 }
26893 }
26894 vim_free(sub);
26895 vim_free(str);
26896 }
26897 vim_free(pat);
26898 }
26899 }
26900 /* after using ":s", repeat all the modifiers */
26901 if (didit)
26902 goto repeat;
26903 }
26904 }
26905
Bram Moolenaar26df0922014-02-23 23:39:13 +010026906 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26907 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026908 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026909 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026910 if (c != NUL)
26911 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026912 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026913 if (c != NUL)
26914 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026915 if (p == NULL)
26916 return -1;
26917 vim_free(*bufp);
26918 *bufp = *fnamep = p;
26919 *fnamelen = (int)STRLEN(p);
26920 *usedlen += 2;
26921 }
26922
Bram Moolenaar071d4272004-06-13 20:20:40 +000026923 return valid;
26924}
26925
26926/*
26927 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26928 * "flags" can be "g" to do a global substitute.
26929 * Returns an allocated string, NULL for error.
26930 */
26931 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026932do_string_sub(
26933 char_u *str,
26934 char_u *pat,
26935 char_u *sub,
26936 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026937{
26938 int sublen;
26939 regmatch_T regmatch;
26940 int i;
26941 int do_all;
26942 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026943 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026944 garray_T ga;
26945 char_u *ret;
26946 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026947 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026948
26949 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26950 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026951 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026952
26953 ga_init2(&ga, 1, 200);
26954
26955 do_all = (flags[0] == 'g');
26956
26957 regmatch.rm_ic = p_ic;
26958 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26959 if (regmatch.regprog != NULL)
26960 {
26961 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026962 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026963 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26964 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026965 /* Skip empty match except for first match. */
26966 if (regmatch.startp[0] == regmatch.endp[0])
26967 {
26968 if (zero_width == regmatch.startp[0])
26969 {
26970 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026971 i = MB_PTR2LEN(tail);
26972 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26973 (size_t)i);
26974 ga.ga_len += i;
26975 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026976 continue;
26977 }
26978 zero_width = regmatch.startp[0];
26979 }
26980
Bram Moolenaar071d4272004-06-13 20:20:40 +000026981 /*
26982 * Get some space for a temporary buffer to do the substitution
26983 * into. It will contain:
26984 * - The text up to where the match is.
26985 * - The substituted text.
26986 * - The text after the match.
26987 */
26988 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026989 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026990 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26991 {
26992 ga_clear(&ga);
26993 break;
26994 }
26995
26996 /* copy the text up to where the match is */
26997 i = (int)(regmatch.startp[0] - tail);
26998 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26999 /* add the substituted text */
27000 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27001 + ga.ga_len + i, TRUE, TRUE, FALSE);
27002 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027003 tail = regmatch.endp[0];
27004 if (*tail == NUL)
27005 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027006 if (!do_all)
27007 break;
27008 }
27009
27010 if (ga.ga_data != NULL)
27011 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27012
Bram Moolenaar473de612013-06-08 18:19:48 +020027013 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027014 }
27015
27016 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27017 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027018 if (p_cpo == empty_option)
27019 p_cpo = save_cpo;
27020 else
27021 /* Darn, evaluating {sub} expression changed the value. */
27022 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023
27024 return ret;
27025}
27026
27027#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */