blob: 9adaa419f33108561ce28ff30fc190ac8d5a307a [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);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200448static char_u *list2string(typval_T *tv, int copyID, int restore_copyID);
449static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID, garray_T *join_gap);
450static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo_style, int restore_copyID, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100451static 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);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200456static char_u *dict2string(typval_T *tv, int copyID, int restore_copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100457static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200458static char_u *echo_string_core(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID, int echo_style, int restore_copyID, int dict_val);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100460static char_u *string_quote(char_u *str, int function);
461static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
462static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100463static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
464static 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 +0100465static void emsg_funcname(char *ermsg, char_u *name);
466static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000467
Bram Moolenaar107e1ee2016-04-08 17:07:19 +0200468static void dict_free_contents(dict_T *d);
469static void dict_free_dict(dict_T *d);
470
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000471#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100472static void f_abs(typval_T *argvars, typval_T *rettv);
473static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000474#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100475static void f_add(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100476static 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);
552static void f_empty(typval_T *argvars, typval_T *rettv);
553static void f_escape(typval_T *argvars, typval_T *rettv);
554static void f_eval(typval_T *argvars, typval_T *rettv);
555static void f_eventhandler(typval_T *argvars, typval_T *rettv);
556static void f_executable(typval_T *argvars, typval_T *rettv);
557static void f_exepath(typval_T *argvars, typval_T *rettv);
558static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200559#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_expand(typval_T *argvars, typval_T *rettv);
563static void f_extend(typval_T *argvars, typval_T *rettv);
564static void f_feedkeys(typval_T *argvars, typval_T *rettv);
565static void f_filereadable(typval_T *argvars, typval_T *rettv);
566static void f_filewritable(typval_T *argvars, typval_T *rettv);
567static void f_filter(typval_T *argvars, typval_T *rettv);
568static void f_finddir(typval_T *argvars, typval_T *rettv);
569static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000570#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100571static void f_float2nr(typval_T *argvars, typval_T *rettv);
572static void f_floor(typval_T *argvars, typval_T *rettv);
573static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000574#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100575static void f_fnameescape(typval_T *argvars, typval_T *rettv);
576static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
577static void f_foldclosed(typval_T *argvars, typval_T *rettv);
578static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
579static void f_foldlevel(typval_T *argvars, typval_T *rettv);
580static void f_foldtext(typval_T *argvars, typval_T *rettv);
581static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
582static void f_foreground(typval_T *argvars, typval_T *rettv);
583static void f_function(typval_T *argvars, typval_T *rettv);
584static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
585static void f_get(typval_T *argvars, typval_T *rettv);
586static void f_getbufline(typval_T *argvars, typval_T *rettv);
587static void f_getbufvar(typval_T *argvars, typval_T *rettv);
588static void f_getchar(typval_T *argvars, typval_T *rettv);
589static void f_getcharmod(typval_T *argvars, typval_T *rettv);
590static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
591static void f_getcmdline(typval_T *argvars, typval_T *rettv);
592static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
593static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
594static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
595static void f_getcwd(typval_T *argvars, typval_T *rettv);
596static void f_getfontname(typval_T *argvars, typval_T *rettv);
597static void f_getfperm(typval_T *argvars, typval_T *rettv);
598static void f_getfsize(typval_T *argvars, typval_T *rettv);
599static void f_getftime(typval_T *argvars, typval_T *rettv);
600static void f_getftype(typval_T *argvars, typval_T *rettv);
601static void f_getline(typval_T *argvars, typval_T *rettv);
602static void f_getmatches(typval_T *argvars, typval_T *rettv);
603static void f_getpid(typval_T *argvars, typval_T *rettv);
604static void f_getcurpos(typval_T *argvars, typval_T *rettv);
605static void f_getpos(typval_T *argvars, typval_T *rettv);
606static void f_getqflist(typval_T *argvars, typval_T *rettv);
607static void f_getreg(typval_T *argvars, typval_T *rettv);
608static void f_getregtype(typval_T *argvars, typval_T *rettv);
609static void f_gettabvar(typval_T *argvars, typval_T *rettv);
610static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
611static void f_getwinposx(typval_T *argvars, typval_T *rettv);
612static void f_getwinposy(typval_T *argvars, typval_T *rettv);
613static void f_getwinvar(typval_T *argvars, typval_T *rettv);
614static void f_glob(typval_T *argvars, typval_T *rettv);
615static void f_globpath(typval_T *argvars, typval_T *rettv);
616static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
617static void f_has(typval_T *argvars, typval_T *rettv);
618static void f_has_key(typval_T *argvars, typval_T *rettv);
619static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
620static void f_hasmapto(typval_T *argvars, typval_T *rettv);
621static void f_histadd(typval_T *argvars, typval_T *rettv);
622static void f_histdel(typval_T *argvars, typval_T *rettv);
623static void f_histget(typval_T *argvars, typval_T *rettv);
624static void f_histnr(typval_T *argvars, typval_T *rettv);
625static void f_hlID(typval_T *argvars, typval_T *rettv);
626static void f_hlexists(typval_T *argvars, typval_T *rettv);
627static void f_hostname(typval_T *argvars, typval_T *rettv);
628static void f_iconv(typval_T *argvars, typval_T *rettv);
629static void f_indent(typval_T *argvars, typval_T *rettv);
630static void f_index(typval_T *argvars, typval_T *rettv);
631static void f_input(typval_T *argvars, typval_T *rettv);
632static void f_inputdialog(typval_T *argvars, typval_T *rettv);
633static void f_inputlist(typval_T *argvars, typval_T *rettv);
634static void f_inputrestore(typval_T *argvars, typval_T *rettv);
635static void f_inputsave(typval_T *argvars, typval_T *rettv);
636static void f_inputsecret(typval_T *argvars, typval_T *rettv);
637static void f_insert(typval_T *argvars, typval_T *rettv);
638static void f_invert(typval_T *argvars, typval_T *rettv);
639static void f_isdirectory(typval_T *argvars, typval_T *rettv);
640static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100641#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
642static void f_isnan(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100645#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100646static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100647static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100648static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100649static void f_job_start(typval_T *argvars, typval_T *rettv);
650static void f_job_stop(typval_T *argvars, typval_T *rettv);
651static void f_job_status(typval_T *argvars, typval_T *rettv);
652#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100653static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100654static void f_js_decode(typval_T *argvars, typval_T *rettv);
655static void f_js_encode(typval_T *argvars, typval_T *rettv);
656static void f_json_decode(typval_T *argvars, typval_T *rettv);
657static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_keys(typval_T *argvars, typval_T *rettv);
659static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
660static void f_len(typval_T *argvars, typval_T *rettv);
661static void f_libcall(typval_T *argvars, typval_T *rettv);
662static void f_libcallnr(typval_T *argvars, typval_T *rettv);
663static void f_line(typval_T *argvars, typval_T *rettv);
664static void f_line2byte(typval_T *argvars, typval_T *rettv);
665static void f_lispindent(typval_T *argvars, typval_T *rettv);
666static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100668static void f_log(typval_T *argvars, typval_T *rettv);
669static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000670#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200671#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100672static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200673#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100674static void f_map(typval_T *argvars, typval_T *rettv);
675static void f_maparg(typval_T *argvars, typval_T *rettv);
676static void f_mapcheck(typval_T *argvars, typval_T *rettv);
677static void f_match(typval_T *argvars, typval_T *rettv);
678static void f_matchadd(typval_T *argvars, typval_T *rettv);
679static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
680static void f_matcharg(typval_T *argvars, typval_T *rettv);
681static void f_matchdelete(typval_T *argvars, typval_T *rettv);
682static void f_matchend(typval_T *argvars, typval_T *rettv);
683static void f_matchlist(typval_T *argvars, typval_T *rettv);
684static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200685static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_max(typval_T *argvars, typval_T *rettv);
687static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
696static void f_nr2char(typval_T *argvars, typval_T *rettv);
697static void f_or(typval_T *argvars, typval_T *rettv);
698static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100701#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
706static void f_printf(typval_T *argvars, typval_T *rettv);
707static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200710#endif
711#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200713#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100714static void f_range(typval_T *argvars, typval_T *rettv);
715static void f_readfile(typval_T *argvars, typval_T *rettv);
716static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100717#ifdef FEAT_FLOAT
718static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_reltimestr(typval_T *argvars, typval_T *rettv);
721static void f_remote_expr(typval_T *argvars, typval_T *rettv);
722static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
723static void f_remote_peek(typval_T *argvars, typval_T *rettv);
724static void f_remote_read(typval_T *argvars, typval_T *rettv);
725static void f_remote_send(typval_T *argvars, typval_T *rettv);
726static void f_remove(typval_T *argvars, typval_T *rettv);
727static void f_rename(typval_T *argvars, typval_T *rettv);
728static void f_repeat(typval_T *argvars, typval_T *rettv);
729static void f_resolve(typval_T *argvars, typval_T *rettv);
730static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000733#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100734static void f_screenattr(typval_T *argvars, typval_T *rettv);
735static void f_screenchar(typval_T *argvars, typval_T *rettv);
736static void f_screencol(typval_T *argvars, typval_T *rettv);
737static void f_screenrow(typval_T *argvars, typval_T *rettv);
738static void f_search(typval_T *argvars, typval_T *rettv);
739static void f_searchdecl(typval_T *argvars, typval_T *rettv);
740static void f_searchpair(typval_T *argvars, typval_T *rettv);
741static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
742static void f_searchpos(typval_T *argvars, typval_T *rettv);
743static void f_server2client(typval_T *argvars, typval_T *rettv);
744static void f_serverlist(typval_T *argvars, typval_T *rettv);
745static void f_setbufvar(typval_T *argvars, typval_T *rettv);
746static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
747static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100748static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_setline(typval_T *argvars, typval_T *rettv);
750static void f_setloclist(typval_T *argvars, typval_T *rettv);
751static void f_setmatches(typval_T *argvars, typval_T *rettv);
752static void f_setpos(typval_T *argvars, typval_T *rettv);
753static void f_setqflist(typval_T *argvars, typval_T *rettv);
754static void f_setreg(typval_T *argvars, typval_T *rettv);
755static void f_settabvar(typval_T *argvars, typval_T *rettv);
756static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
757static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100758#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100760#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100761static void f_shellescape(typval_T *argvars, typval_T *rettv);
762static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
763static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sin(typval_T *argvars, typval_T *rettv);
766static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_sort(typval_T *argvars, typval_T *rettv);
769static void f_soundfold(typval_T *argvars, typval_T *rettv);
770static void f_spellbadword(typval_T *argvars, typval_T *rettv);
771static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
772static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000773#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100774static void f_sqrt(typval_T *argvars, typval_T *rettv);
775static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_str2nr(typval_T *argvars, typval_T *rettv);
778static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000779#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100780static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000781#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200782static void f_strgetchar(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100783static void f_stridx(typval_T *argvars, typval_T *rettv);
784static void f_string(typval_T *argvars, typval_T *rettv);
785static void f_strlen(typval_T *argvars, typval_T *rettv);
Bram Moolenaar58de0e22016-04-14 15:13:46 +0200786static void f_strcharpart(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100787static void f_strpart(typval_T *argvars, typval_T *rettv);
788static void f_strridx(typval_T *argvars, typval_T *rettv);
789static void f_strtrans(typval_T *argvars, typval_T *rettv);
790static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
791static void f_strwidth(typval_T *argvars, typval_T *rettv);
792static void f_submatch(typval_T *argvars, typval_T *rettv);
793static void f_substitute(typval_T *argvars, typval_T *rettv);
794static void f_synID(typval_T *argvars, typval_T *rettv);
795static void f_synIDattr(typval_T *argvars, typval_T *rettv);
796static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
797static void f_synstack(typval_T *argvars, typval_T *rettv);
798static void f_synconcealed(typval_T *argvars, typval_T *rettv);
799static void f_system(typval_T *argvars, typval_T *rettv);
800static void f_systemlist(typval_T *argvars, typval_T *rettv);
801static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
802static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
803static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
804static void f_taglist(typval_T *argvars, typval_T *rettv);
805static void f_tagfiles(typval_T *argvars, typval_T *rettv);
806static void f_tempname(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200807static void f_test_alloc_fail(typval_T *argvars, typval_T *rettv);
808static void f_test_disable_char_avail(typval_T *argvars, typval_T *rettv);
Bram Moolenaar574860b2016-05-24 17:33:34 +0200809static void f_test_garbagecollect_now(typval_T *argvars, typval_T *rettv);
810#ifdef FEAT_JOB_CHANNEL
811static void f_test_null_channel(typval_T *argvars, typval_T *rettv);
812#endif
813static void f_test_null_dict(typval_T *argvars, typval_T *rettv);
814#ifdef FEAT_JOB_CHANNEL
815static void f_test_null_job(typval_T *argvars, typval_T *rettv);
816#endif
817static void f_test_null_list(typval_T *argvars, typval_T *rettv);
818static void f_test_null_partial(typval_T *argvars, typval_T *rettv);
819static void f_test_null_string(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200820#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100821static void f_tan(typval_T *argvars, typval_T *rettv);
822static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200823#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100824#ifdef FEAT_TIMERS
825static void f_timer_start(typval_T *argvars, typval_T *rettv);
826static void f_timer_stop(typval_T *argvars, typval_T *rettv);
827#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100828static void f_tolower(typval_T *argvars, typval_T *rettv);
829static void f_toupper(typval_T *argvars, typval_T *rettv);
830static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000831#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000833#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static void f_type(typval_T *argvars, typval_T *rettv);
835static void f_undofile(typval_T *argvars, typval_T *rettv);
836static void f_undotree(typval_T *argvars, typval_T *rettv);
837static void f_uniq(typval_T *argvars, typval_T *rettv);
838static void f_values(typval_T *argvars, typval_T *rettv);
839static void f_virtcol(typval_T *argvars, typval_T *rettv);
840static void f_visualmode(typval_T *argvars, typval_T *rettv);
841static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100842static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100843static void f_win_getid(typval_T *argvars, typval_T *rettv);
844static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
845static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
846static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100847static void f_winbufnr(typval_T *argvars, typval_T *rettv);
848static void f_wincol(typval_T *argvars, typval_T *rettv);
849static void f_winheight(typval_T *argvars, typval_T *rettv);
850static void f_winline(typval_T *argvars, typval_T *rettv);
851static void f_winnr(typval_T *argvars, typval_T *rettv);
852static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
853static void f_winrestview(typval_T *argvars, typval_T *rettv);
854static void f_winsaveview(typval_T *argvars, typval_T *rettv);
855static void f_winwidth(typval_T *argvars, typval_T *rettv);
856static void f_writefile(typval_T *argvars, typval_T *rettv);
857static void f_wordcount(typval_T *argvars, typval_T *rettv);
858static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000859
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100860static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
861static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
862static int get_env_len(char_u **arg);
863static int get_id_len(char_u **arg);
864static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
865static 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 +0000866#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
867#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
868 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100869static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
870static int eval_isnamec(int c);
871static int eval_isnamec1(int c);
872static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
873static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static typval_T *alloc_string_tv(char_u *string);
875static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100876#ifdef FEAT_FLOAT
877static float_T get_tv_float(typval_T *varp);
878#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100879static linenr_T get_tv_lnum(typval_T *argvars);
880static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
882static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
883static hashtab_T *find_var_ht(char_u *name, char_u **varname);
884static funccall_T *get_funccal(void);
885static void vars_clear_ext(hashtab_T *ht, int free_val);
886static void delete_var(hashtab_T *ht, hashitem_T *hi);
887static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
888static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
889static void set_var(char_u *name, typval_T *varp, int copy);
890static int var_check_ro(int flags, char_u *name, int use_gettext);
891static int var_check_fixed(int flags, char_u *name, int use_gettext);
892static int var_check_func_name(char_u *name, int new_var);
893static int valid_varname(char_u *varname);
894static int tv_check_lock(int lock, char_u *name, int use_gettext);
895static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
896static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100897static 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 +0100898static int eval_fname_script(char_u *p);
899static int eval_fname_sid(char_u *p);
900static void list_func_head(ufunc_T *fp, int indent);
901static ufunc_T *find_func(char_u *name);
902static int function_exists(char_u *name);
903static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000904#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100905static void func_do_profile(ufunc_T *fp);
906static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
907static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000908static int
909# ifdef __BORLANDC__
910 _RTLENTRYF
911# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100912 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000913static int
914# ifdef __BORLANDC__
915 _RTLENTRYF
916# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100917 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000918#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100919static int script_autoload(char_u *name, int reload);
920static char_u *autoload_name(char_u *name);
921static void cat_func_name(char_u *buf, ufunc_T *fp);
922static void func_free(ufunc_T *fp);
923static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
924static int can_free_funccal(funccall_T *fc, int copyID) ;
925static void free_funccal(funccall_T *fc, int free_val);
926static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
927static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
928static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
929static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
930static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
931static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
932static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
933static int write_list(FILE *fd, list_T *list, int binary);
934static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000935
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200936
937#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100938static int compare_func_name(const void *s1, const void *s2);
939static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200940#endif
941
Bram Moolenaar33570922005-01-25 22:26:29 +0000942/*
943 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000944 */
945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100946eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000947{
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 int i;
949 struct vimvar *p;
950
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200951 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
952 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200953 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000954 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000955 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000956
957 for (i = 0; i < VV_LEN; ++i)
958 {
959 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200960 if (STRLEN(p->vv_name) > 16)
961 {
962 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
963 getout(1);
964 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 STRCPY(p->vv_di.di_key, p->vv_name);
966 if (p->vv_flags & VV_RO)
967 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
968 else if (p->vv_flags & VV_RO_SBX)
969 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
970 else
971 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000972
973 /* add to v: scope dict, unless the value is not always available */
974 if (p->vv_type != VAR_UNKNOWN)
975 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000976 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000977 /* add to compat scope dict */
978 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100980 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
981
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000982 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100983 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200984 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100985 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100986
987 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
988 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
989 set_vim_var_nr(VV_NONE, VVAL_NONE);
990 set_vim_var_nr(VV_NULL, VVAL_NULL);
991
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200992 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200993
994#ifdef EBCDIC
995 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100996 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200997 */
998 sortFunctions();
999#endif
Bram Moolenaara7043832005-01-21 11:56:39 +00001000}
1001
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001002#if defined(EXITFREE) || defined(PROTO)
1003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001005{
1006 int i;
1007 struct vimvar *p;
1008
1009 for (i = 0; i < VV_LEN; ++i)
1010 {
1011 p = &vimvars[i];
1012 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +00001013 {
Bram Moolenaar12193212008-11-09 16:22:01 +00001014 vim_free(p->vv_str);
1015 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00001016 }
1017 else if (p->vv_di.di_tv.v_type == VAR_LIST)
1018 {
1019 list_unref(p->vv_list);
1020 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +00001021 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001022 }
1023 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001024 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001025 hash_clear(&compat_hashtab);
1026
Bram Moolenaard9fba312005-06-26 22:34:35 +00001027 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001028# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001029 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001030# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001031
1032 /* global variables */
1033 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001034
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001035 /* autoloaded script names */
1036 ga_clear_strings(&ga_loaded);
1037
Bram Moolenaarcca74132013-09-25 21:00:28 +02001038 /* Script-local variables. First clear all the variables and in a second
1039 * loop free the scriptvar_T, because a variable in one script might hold
1040 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001041 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001042 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001043 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001044 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001045 ga_clear(&ga_scripts);
1046
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001047 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02001048 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001049
1050 /* functions */
1051 free_all_functions();
1052 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001053}
1054#endif
1055
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 * Return the name of the executed function.
1058 */
1059 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001060func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063}
1064
1065/*
1066 * Return the address holding the next breakpoint line for a funccall cookie.
1067 */
1068 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001069func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
Bram Moolenaar33570922005-01-25 22:26:29 +00001071 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072}
1073
1074/*
1075 * Return the address holding the debug tick for a funccall cookie.
1076 */
1077 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar33570922005-01-25 22:26:29 +00001080 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081}
1082
1083/*
1084 * Return the nesting level for a funccall cookie.
1085 */
1086 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001087func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088{
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090}
1091
1092/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001093funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001095/* pointer to list of previously used funccal, still around because some
1096 * item in it is still being used. */
1097funccall_T *previous_funccal = NULL;
1098
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099/*
1100 * Return TRUE when a function was ended by a ":return" command.
1101 */
1102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001103current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104{
1105 return current_funccal->returned;
1106}
1107
1108
1109/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 * Set an internal variable to a string value. Creates the variable if it does
1111 * not already exist.
1112 */
1113 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001114set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001116 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001117 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 val = vim_strsave(value);
1120 if (val != NULL)
1121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001123 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001125 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129}
1130
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001132static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133static char_u *redir_endp = NULL;
1134static char_u *redir_varname = NULL;
1135
1136/*
1137 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001138 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001139 * Returns OK if successfully completed the setup. FAIL otherwise.
1140 */
1141 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001142var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143{
1144 int save_emsg;
1145 int err;
1146 typval_T tv;
1147
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001149 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 {
1151 EMSG(_(e_invarg));
1152 return FAIL;
1153 }
1154
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001156 redir_varname = vim_strsave(name);
1157 if (redir_varname == NULL)
1158 return FAIL;
1159
1160 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1161 if (redir_lval == NULL)
1162 {
1163 var_redir_stop();
1164 return FAIL;
1165 }
1166
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001167 /* The output is stored in growarray "redir_ga" until redirection ends. */
1168 ga_init2(&redir_ga, (int)sizeof(char), 500);
1169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001171 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001172 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1174 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001175 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (redir_endp != NULL && *redir_endp != NUL)
1177 /* Trailing characters are present after the variable name */
1178 EMSG(_(e_trailing));
1179 else
1180 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001181 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182 var_redir_stop();
1183 return FAIL;
1184 }
1185
1186 /* check if we can write to the variable: set it to or append an empty
1187 * string */
1188 save_emsg = did_emsg;
1189 did_emsg = FALSE;
1190 tv.v_type = VAR_STRING;
1191 tv.vval.v_string = (char_u *)"";
1192 if (append)
1193 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1194 else
1195 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001196 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001198 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 if (err)
1200 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001201 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202 var_redir_stop();
1203 return FAIL;
1204 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205
1206 return OK;
1207}
1208
1209/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001210 * Append "value[value_len]" to the variable set by var_redir_start().
1211 * The actual appending is postponed until redirection ends, because the value
1212 * appended may in fact be the string we write to, changing it may cause freed
1213 * memory to be used:
1214 * :redir => foo
1215 * :let foo
1216 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217 */
1218 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001219var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001221 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001222
1223 if (redir_lval == NULL)
1224 return;
1225
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001226 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001227 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001228 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001229 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001230
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001231 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232 {
1233 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001234 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235 }
1236 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001238}
1239
1240/*
1241 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001242 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001243 */
1244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001245var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001246{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001247 typval_T tv;
1248
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001249 if (redir_lval != NULL)
1250 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001251 /* If there was no error: assign the text to the variable. */
1252 if (redir_endp != NULL)
1253 {
1254 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1255 tv.v_type = VAR_STRING;
1256 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001257 /* Call get_lval() again, if it's inside a Dict or List it may
1258 * have changed. */
1259 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001260 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001261 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1262 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1263 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001264 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001265
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001266 /* free the collected output */
1267 vim_free(redir_ga.ga_data);
1268 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001269
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001270 vim_free(redir_lval);
1271 redir_lval = NULL;
1272 }
1273 vim_free(redir_varname);
1274 redir_varname = NULL;
1275}
1276
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277# if defined(FEAT_MBYTE) || defined(PROTO)
1278 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001279eval_charconvert(
1280 char_u *enc_from,
1281 char_u *enc_to,
1282 char_u *fname_from,
1283 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 int err = FALSE;
1286
1287 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1288 set_vim_var_string(VV_CC_TO, enc_to, -1);
1289 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1290 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1291 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1292 err = TRUE;
1293 set_vim_var_string(VV_CC_FROM, NULL, -1);
1294 set_vim_var_string(VV_CC_TO, NULL, -1);
1295 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1296 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1297
1298 if (err)
1299 return FAIL;
1300 return OK;
1301}
1302# endif
1303
1304# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001306eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307{
1308 int err = FALSE;
1309
1310 set_vim_var_string(VV_FNAME_IN, fname, -1);
1311 set_vim_var_string(VV_CMDARG, args, -1);
1312 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1313 err = TRUE;
1314 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1315 set_vim_var_string(VV_CMDARG, NULL, -1);
1316
1317 if (err)
1318 {
1319 mch_remove(fname);
1320 return FAIL;
1321 }
1322 return OK;
1323}
1324# endif
1325
1326# if defined(FEAT_DIFF) || defined(PROTO)
1327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001328eval_diff(
1329 char_u *origfile,
1330 char_u *newfile,
1331 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332{
1333 int err = FALSE;
1334
1335 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1336 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1337 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1338 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1339 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1340 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1341 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1342}
1343
1344 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001345eval_patch(
1346 char_u *origfile,
1347 char_u *difffile,
1348 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349{
1350 int err;
1351
1352 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1353 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1354 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1355 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1356 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1357 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1358 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1359}
1360# endif
1361
1362/*
1363 * Top level evaluation function, returning a boolean.
1364 * Sets "error" to TRUE if there was an error.
1365 * Return TRUE or FALSE.
1366 */
1367 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001368eval_to_bool(
1369 char_u *arg,
1370 int *error,
1371 char_u **nextcmd,
1372 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 int retval = FALSE;
1376
1377 if (skip)
1378 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 else
1382 {
1383 *error = FALSE;
1384 if (!skip)
1385 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001386 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 }
1390 if (skip)
1391 --emsg_skip;
1392
1393 return retval;
1394}
1395
1396/*
1397 * Top level evaluation function, returning a string. If "skip" is TRUE,
1398 * only parsing to "nextcmd" is done, without reporting errors. Return
1399 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1400 */
1401 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402eval_to_string_skip(
1403 char_u *arg,
1404 char_u **nextcmd,
1405 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406{
Bram Moolenaar33570922005-01-25 22:26:29 +00001407 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 char_u *retval;
1409
1410 if (skip)
1411 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 retval = NULL;
1414 else
1415 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001416 retval = vim_strsave(get_tv_string(&tv));
1417 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 if (skip)
1420 --emsg_skip;
1421
1422 return retval;
1423}
1424
1425/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001426 * Skip over an expression at "*pp".
1427 * Return FAIL for an error, OK otherwise.
1428 */
1429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001430skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001431{
Bram Moolenaar33570922005-01-25 22:26:29 +00001432 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001433
1434 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001435 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001436}
1437
1438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440 * When "convert" is TRUE convert a List into a sequence of lines and convert
1441 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 * Return pointer to allocated memory, or NULL for failure.
1443 */
1444 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001445eval_to_string(
1446 char_u *arg,
1447 char_u **nextcmd,
1448 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
Bram Moolenaar33570922005-01-25 22:26:29 +00001450 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001453#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001454 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001457 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 retval = NULL;
1459 else
1460 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001461 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001462 {
1463 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001464 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001465 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02001466 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001467 if (tv.vval.v_list->lv_len > 0)
1468 ga_append(&ga, NL);
1469 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001470 ga_append(&ga, NUL);
1471 retval = (char_u *)ga.ga_data;
1472 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001473#ifdef FEAT_FLOAT
1474 else if (convert && tv.v_type == VAR_FLOAT)
1475 {
1476 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1477 retval = vim_strsave(numbuf);
1478 }
1479#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001480 else
1481 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 }
1484
1485 return retval;
1486}
1487
1488/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001489 * Call eval_to_string() without using current local variables and using
1490 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 */
1492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493eval_to_string_safe(
1494 char_u *arg,
1495 char_u **nextcmd,
1496 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
1498 char_u *retval;
1499 void *save_funccalp;
1500
1501 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001502 if (use_sandbox)
1503 ++sandbox;
1504 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001505 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001506 if (use_sandbox)
1507 --sandbox;
1508 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 restore_funccal(save_funccalp);
1510 return retval;
1511}
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513/*
1514 * Top level evaluation function, returning a number.
1515 * Evaluates "expr" silently.
1516 * Returns -1 for an error.
1517 */
1518 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001519eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520{
Bram Moolenaar33570922005-01-25 22:26:29 +00001521 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001523 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524
1525 ++emsg_off;
1526
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 retval = -1;
1529 else
1530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001531 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 }
1534 --emsg_off;
1535
1536 return retval;
1537}
1538
Bram Moolenaara40058a2005-07-11 22:42:07 +00001539/*
1540 * Prepare v: variable "idx" to be used.
1541 * Save the current typeval in "save_tv".
1542 * When not used yet add the variable to the v: hashtable.
1543 */
1544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001546{
1547 *save_tv = vimvars[idx].vv_tv;
1548 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1549 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1550}
1551
1552/*
1553 * Restore v: variable "idx" to typeval "save_tv".
1554 * When no longer defined, remove the variable from the v: hashtable.
1555 */
1556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001557restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001558{
1559 hashitem_T *hi;
1560
Bram Moolenaara40058a2005-07-11 22:42:07 +00001561 vimvars[idx].vv_tv = *save_tv;
1562 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1563 {
1564 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1565 if (HASHITEM_EMPTY(hi))
1566 EMSG2(_(e_intern2), "restore_vimvar()");
1567 else
1568 hash_remove(&vimvarht, hi);
1569 }
1570}
1571
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001572#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573/*
1574 * Evaluate an expression to a list with suggestions.
1575 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001576 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001577 */
1578 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 typval_T save_val;
1582 typval_T rettv;
1583 list_T *list = NULL;
1584 char_u *p = skipwhite(expr);
1585
1586 /* Set "v:val" to the bad word. */
1587 prepare_vimvar(VV_VAL, &save_val);
1588 vimvars[VV_VAL].vv_type = VAR_STRING;
1589 vimvars[VV_VAL].vv_str = badword;
1590 if (p_verbose == 0)
1591 ++emsg_off;
1592
1593 if (eval1(&p, &rettv, TRUE) == OK)
1594 {
1595 if (rettv.v_type != VAR_LIST)
1596 clear_tv(&rettv);
1597 else
1598 list = rettv.vval.v_list;
1599 }
1600
1601 if (p_verbose == 0)
1602 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001603 restore_vimvar(VV_VAL, &save_val);
1604
1605 return list;
1606}
1607
1608/*
1609 * "list" is supposed to contain two items: a word and a number. Return the
1610 * word in "pp" and the number as the return value.
1611 * Return -1 if anything isn't right.
1612 * Used to get the good word and score from the eval_spell_expr() result.
1613 */
1614 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001615get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001616{
1617 listitem_T *li;
1618
1619 li = list->lv_first;
1620 if (li == NULL)
1621 return -1;
1622 *pp = get_tv_string(&li->li_tv);
1623
1624 li = li->li_next;
1625 if (li == NULL)
1626 return -1;
1627 return get_tv_number(&li->li_tv);
1628}
1629#endif
1630
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001631/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 * Top level evaluation function.
1633 * Returns an allocated typval_T with the result.
1634 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001635 */
1636 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001637eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001638{
1639 typval_T *tv;
1640
1641 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001642 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001643 {
1644 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001645 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001646 }
1647
1648 return tv;
1649}
1650
1651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001653 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001654 * Uses argv[argc] for the function arguments. Only Number and String
1655 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001656 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001658 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001659call_vim_function(
1660 char_u *func,
1661 int argc,
1662 char_u **argv,
1663 int safe, /* use the sandbox */
1664 int str_arg_only, /* all arguments are strings */
1665 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
Bram Moolenaar33570922005-01-25 22:26:29 +00001667 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 long n;
1669 int len;
1670 int i;
1671 int doesrange;
1672 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001675 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
1679 for (i = 0; i < argc; i++)
1680 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001681 /* Pass a NULL or empty argument as an empty string */
1682 if (argv[i] == NULL || *argv[i] == NUL)
1683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001684 argvars[i].v_type = VAR_STRING;
1685 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001686 continue;
1687 }
1688
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001689 if (str_arg_only)
1690 len = 0;
1691 else
1692 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001693 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 if (len != 0 && len == (int)STRLEN(argv[i]))
1695 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001696 argvars[i].v_type = VAR_NUMBER;
1697 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 }
1699 else
1700 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001701 argvars[i].v_type = VAR_STRING;
1702 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 }
1704 }
1705
1706 if (safe)
1707 {
1708 save_funccalp = save_funccal();
1709 ++sandbox;
1710 }
1711
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001712 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1713 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001715 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (safe)
1717 {
1718 --sandbox;
1719 restore_funccal(save_funccalp);
1720 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721 vim_free(argvars);
1722
1723 if (ret == FAIL)
1724 clear_tv(rettv);
1725
1726 return ret;
1727}
1728
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001729/*
1730 * Call vimL function "func" and return the result as a number.
1731 * Returns -1 when calling the function fails.
1732 * Uses argv[argc] for the function arguments.
1733 */
1734 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735call_func_retnr(
1736 char_u *func,
1737 int argc,
1738 char_u **argv,
1739 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001740{
1741 typval_T rettv;
1742 long retval;
1743
1744 /* All arguments are passed as strings, no conversion to number. */
1745 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1746 return -1;
1747
1748 retval = get_tv_number_chk(&rettv, NULL);
1749 clear_tv(&rettv);
1750 return retval;
1751}
1752
1753#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1754 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1755
Bram Moolenaar4f688582007-07-24 12:34:30 +00001756# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001758 * Call vimL function "func" and return the result as a string.
1759 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760 * Uses argv[argc] for the function arguments.
1761 */
1762 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001763call_func_retstr(
1764 char_u *func,
1765 int argc,
1766 char_u **argv,
1767 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768{
1769 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001770 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001771
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001772 /* All arguments are passed as strings, no conversion to number. */
1773 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001774 return NULL;
1775
1776 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001777 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 return retval;
1779}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001780# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001781
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001782/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001783 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001784 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001785 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001786 */
1787 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788call_func_retlist(
1789 char_u *func,
1790 int argc,
1791 char_u **argv,
1792 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001793{
1794 typval_T rettv;
1795
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001796 /* All arguments are passed as strings, no conversion to number. */
1797 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001798 return NULL;
1799
1800 if (rettv.v_type != VAR_LIST)
1801 {
1802 clear_tv(&rettv);
1803 return NULL;
1804 }
1805
1806 return rettv.vval.v_list;
1807}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#endif
1809
1810/*
1811 * Save the current function call pointer, and set it to NULL.
1812 * Used when executing autocommands and for ":source".
1813 */
1814 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001815save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 current_funccal = NULL;
1820 return (void *)fc;
1821}
1822
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001826 funccall_T *fc = (funccall_T *)vfc;
1827
1828 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829}
1830
Bram Moolenaar05159a02005-02-26 23:04:13 +00001831#if defined(FEAT_PROFILE) || defined(PROTO)
1832/*
1833 * Prepare profiling for entering a child or something else that is not
1834 * counted for the script/function itself.
1835 * Should always be called in pair with prof_child_exit().
1836 */
1837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001838prof_child_enter(
1839 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001840{
1841 funccall_T *fc = current_funccal;
1842
1843 if (fc != NULL && fc->func->uf_profiling)
1844 profile_start(&fc->prof_child);
1845 script_prof_save(tm);
1846}
1847
1848/*
1849 * Take care of time spent in a child.
1850 * Should always be called after prof_child_enter().
1851 */
1852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853prof_child_exit(
1854 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001855{
1856 funccall_T *fc = current_funccal;
1857
1858 if (fc != NULL && fc->func->uf_profiling)
1859 {
1860 profile_end(&fc->prof_child);
1861 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1862 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1863 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1864 }
1865 script_prof_restore(tm);
1866}
1867#endif
1868
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#ifdef FEAT_FOLDING
1871/*
1872 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1873 * it in "*cp". Doesn't give error messages.
1874 */
1875 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
Bram Moolenaar33570922005-01-25 22:26:29 +00001878 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 int retval;
1880 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001881 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1882 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001885 if (use_sandbox)
1886 ++sandbox;
1887 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 retval = 0;
1891 else
1892 {
1893 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 if (tv.v_type == VAR_NUMBER)
1895 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001896 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 retval = 0;
1898 else
1899 {
1900 /* If the result is a string, check if there is a non-digit before
1901 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 if (!VIM_ISDIGIT(*s) && *s != '-')
1904 *cp = *s++;
1905 retval = atol((char *)s);
1906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 }
1909 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001910 if (use_sandbox)
1911 --sandbox;
1912 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913
1914 return retval;
1915}
1916#endif
1917
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 * ":let" list all variable values
1920 * ":let var1 var2" list variable values
1921 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 * ":let var += expr" assignment command.
1923 * ":let var -= expr" assignment command.
1924 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 */
1927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001928ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929{
1930 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001932 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 int var_count = 0;
1935 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001936 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001937 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaardb552d602006-03-23 22:59:57 +00001940 argend = skip_var_list(arg, &var_count, &semicolon);
1941 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001943 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1944 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 expr = skipwhite(argend);
1946 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1947 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001949 /*
1950 * ":let" without "=": list variables
1951 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 if (*arg == '[')
1953 EMSG(_(e_invarg));
1954 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001956 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001960 list_glob_vars(&first);
1961 list_buf_vars(&first);
1962 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001963#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001964 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001965#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001966 list_script_vars(&first);
1967 list_func_vars(&first);
1968 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 eap->nextcmd = check_nextcmd(arg);
1971 }
1972 else
1973 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 op[0] = '=';
1975 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001976 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001978 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1979 op[0] = *expr; /* +=, -= or .= */
1980 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001981 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001982 else
1983 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 if (eap->skip)
1986 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (eap->skip)
1989 {
1990 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 --emsg_skip;
1993 }
1994 else if (i != FAIL)
1995 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001997 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000 }
2001}
2002
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003/*
2004 * Assign the typevalue "tv" to the variable or variables at "arg_start".
2005 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002006 * When "nextchars" is not NULL it points to a string with characters that
2007 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
2008 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 * Returns OK or FAIL;
2010 */
2011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002012ex_let_vars(
2013 char_u *arg_start,
2014 typval_T *tv,
2015 int copy, /* copy values from "tv", don't move */
2016 int semicolon, /* from skip_var_list() */
2017 int var_count, /* from skip_var_list() */
2018 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019{
2020 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00002021 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002023 listitem_T *item;
2024 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025
2026 if (*arg != '[')
2027 {
2028 /*
2029 * ":let var = expr" or ":for var in list"
2030 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 return FAIL;
2033 return OK;
2034 }
2035
2036 /*
2037 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2038 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002039 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 {
2041 EMSG(_(e_listreq));
2042 return FAIL;
2043 }
2044
2045 i = list_len(l);
2046 if (semicolon == 0 && var_count < i)
2047 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002048 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 return FAIL;
2050 }
2051 if (var_count - semicolon > i)
2052 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002053 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 return FAIL;
2055 }
2056
2057 item = l->lv_first;
2058 while (*arg != ']')
2059 {
2060 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002062 item = item->li_next;
2063 if (arg == NULL)
2064 return FAIL;
2065
2066 arg = skipwhite(arg);
2067 if (*arg == ';')
2068 {
2069 /* Put the rest of the list (may be empty) in the var after ';'.
2070 * Create a new list for this. */
2071 l = list_alloc();
2072 if (l == NULL)
2073 return FAIL;
2074 while (item != NULL)
2075 {
2076 list_append_tv(l, &item->li_tv);
2077 item = item->li_next;
2078 }
2079
2080 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002081 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002082 ltv.vval.v_list = l;
2083 l->lv_refcount = 1;
2084
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002085 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2086 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 clear_tv(&ltv);
2088 if (arg == NULL)
2089 return FAIL;
2090 break;
2091 }
2092 else if (*arg != ',' && *arg != ']')
2093 {
2094 EMSG2(_(e_intern2), "ex_let_vars()");
2095 return FAIL;
2096 }
2097 }
2098
2099 return OK;
2100}
2101
2102/*
2103 * Skip over assignable variable "var" or list of variables "[var, var]".
2104 * Used for ":let varvar = expr" and ":for varvar in expr".
2105 * For "[var, var]" increment "*var_count" for each variable.
2106 * for "[var, var; var]" set "semicolon".
2107 * Return NULL for an error.
2108 */
2109 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002110skip_var_list(
2111 char_u *arg,
2112 int *var_count,
2113 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002114{
2115 char_u *p, *s;
2116
2117 if (*arg == '[')
2118 {
2119 /* "[var, var]": find the matching ']'. */
2120 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002121 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122 {
2123 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2124 s = skip_var_one(p);
2125 if (s == p)
2126 {
2127 EMSG2(_(e_invarg2), p);
2128 return NULL;
2129 }
2130 ++*var_count;
2131
2132 p = skipwhite(s);
2133 if (*p == ']')
2134 break;
2135 else if (*p == ';')
2136 {
2137 if (*semicolon == 1)
2138 {
2139 EMSG(_("Double ; in list of variables"));
2140 return NULL;
2141 }
2142 *semicolon = 1;
2143 }
2144 else if (*p != ',')
2145 {
2146 EMSG2(_(e_invarg2), p);
2147 return NULL;
2148 }
2149 }
2150 return p + 1;
2151 }
2152 else
2153 return skip_var_one(arg);
2154}
2155
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002156/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002157 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002158 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002159 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002160 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002161skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002162{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002163 if (*arg == '@' && arg[1] != NUL)
2164 return arg + 2;
2165 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2166 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002167}
2168
Bram Moolenaara7043832005-01-21 11:56:39 +00002169/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002170 * List variables for hashtab "ht" with prefix "prefix".
2171 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_hashtable_vars(
2175 hashtab_T *ht,
2176 char_u *prefix,
2177 int empty,
2178 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 hashitem_T *hi;
2181 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002182 int todo;
2183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002184 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002185 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2186 {
2187 if (!HASHITEM_EMPTY(hi))
2188 {
2189 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002190 di = HI2DI(hi);
2191 if (empty || di->di_tv.v_type != VAR_STRING
2192 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002194 }
2195 }
2196}
2197
2198/*
2199 * List global variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002203{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002204 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002205}
2206
2207/*
2208 * List buffer variables.
2209 */
2210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002211list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002212{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002213 char_u numbuf[NUMBUFLEN];
2214
Bram Moolenaar429fa852013-04-15 12:27:36 +02002215 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002216 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217
2218 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002219 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2220 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002221}
2222
2223/*
2224 * List window variables.
2225 */
2226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002228{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002229 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002230 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002231}
2232
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002233#ifdef FEAT_WINDOWS
2234/*
2235 * List tab page variables.
2236 */
2237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002238list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002240 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002241 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002242}
2243#endif
2244
Bram Moolenaara7043832005-01-21 11:56:39 +00002245/*
2246 * List Vim variables.
2247 */
2248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002250{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252}
2253
2254/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002255 * List script-local variables, if there is a script.
2256 */
2257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002258list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002259{
2260 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002261 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2262 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002263}
2264
2265/*
2266 * List function variables, if there is a function.
2267 */
2268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002269list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002270{
2271 if (current_funccal != NULL)
2272 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002273 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002274}
2275
2276/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002277 * List variables in "arg".
2278 */
2279 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002280list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281{
2282 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 char_u *name_start;
2286 char_u *arg_subsc;
2287 char_u *tofree;
2288 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289
2290 while (!ends_excmd(*arg) && !got_int)
2291 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002293 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002294 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002295 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2296 {
2297 emsg_severe = TRUE;
2298 EMSG(_(e_trailing));
2299 break;
2300 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 /* get_name_len() takes care of expanding curly braces */
2305 name_start = name = arg;
2306 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2307 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002309 /* This is mainly to keep test 49 working: when expanding
2310 * curly braces fails overrule the exception error message. */
2311 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 emsg_severe = TRUE;
2314 EMSG2(_(e_invarg2), arg);
2315 break;
2316 }
2317 error = TRUE;
2318 }
2319 else
2320 {
2321 if (tofree != NULL)
2322 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002323 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002324 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 else
2326 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002327 /* handle d.key, l[idx], f(expr) */
2328 arg_subsc = arg;
2329 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002330 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002332 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002333 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002334 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002337 case 'g': list_glob_vars(first); break;
2338 case 'b': list_buf_vars(first); break;
2339 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002340#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002341 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002342#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002343 case 'v': list_vim_vars(first); break;
2344 case 's': list_script_vars(first); break;
2345 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002346 default:
2347 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002348 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002349 }
2350 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002351 {
2352 char_u numbuf[NUMBUFLEN];
2353 char_u *tf;
2354 int c;
2355 char_u *s;
2356
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002357 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002358 c = *arg;
2359 *arg = NUL;
2360 list_one_var_a((char_u *)"",
2361 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002362 tv.v_type,
2363 s == NULL ? (char_u *)"" : s,
2364 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002365 *arg = c;
2366 vim_free(tf);
2367 }
2368 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002369 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 }
2371 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002372
2373 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002375
2376 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002377 }
2378
2379 return arg;
2380}
2381
2382/*
2383 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2384 * Returns a pointer to the char just after the var name.
2385 * Returns NULL if there is an error.
2386 */
2387 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002388ex_let_one(
2389 char_u *arg, /* points to variable name */
2390 typval_T *tv, /* value to assign to variable */
2391 int copy, /* copy value from "tv" */
2392 char_u *endchars, /* valid chars after variable name or NULL */
2393 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394{
2395 int c1;
2396 char_u *name;
2397 char_u *p;
2398 char_u *arg_end = NULL;
2399 int len;
2400 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402
2403 /*
2404 * ":let $VAR = expr": Set environment variable.
2405 */
2406 if (*arg == '$')
2407 {
2408 /* Find the end of the name. */
2409 ++arg;
2410 name = arg;
2411 len = get_env_len(&arg);
2412 if (len == 0)
2413 EMSG2(_(e_invarg2), name - 1);
2414 else
2415 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002416 if (op != NULL && (*op == '+' || *op == '-'))
2417 EMSG2(_(e_letwrong), op);
2418 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002419 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002421 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 {
2423 c1 = name[len];
2424 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002425 p = get_tv_string_chk(tv);
2426 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002427 {
2428 int mustfree = FALSE;
2429 char_u *s = vim_getenv(name, &mustfree);
2430
2431 if (s != NULL)
2432 {
2433 p = tofree = concat_str(s, p);
2434 if (mustfree)
2435 vim_free(s);
2436 }
2437 }
2438 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002441 if (STRICMP(name, "HOME") == 0)
2442 init_homedir();
2443 else if (didset_vim && STRICMP(name, "VIM") == 0)
2444 didset_vim = FALSE;
2445 else if (didset_vimruntime
2446 && STRICMP(name, "VIMRUNTIME") == 0)
2447 didset_vimruntime = FALSE;
2448 arg_end = arg;
2449 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002451 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002452 }
2453 }
2454 }
2455
2456 /*
2457 * ":let &option = expr": Set option value.
2458 * ":let &l:option = expr": Set local option value.
2459 * ":let &g:option = expr": Set global option value.
2460 */
2461 else if (*arg == '&')
2462 {
2463 /* Find the end of the name. */
2464 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002465 if (p == NULL || (endchars != NULL
2466 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 EMSG(_(e_letunexp));
2468 else
2469 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002470 long n;
2471 int opt_type;
2472 long numval;
2473 char_u *stringval = NULL;
2474 char_u *s;
2475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 c1 = *p;
2477 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478
2479 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002480 s = get_tv_string_chk(tv); /* != NULL if number or string */
2481 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 {
2483 opt_type = get_option_value(arg, &numval,
2484 &stringval, opt_flags);
2485 if ((opt_type == 1 && *op == '.')
2486 || (opt_type == 0 && *op != '.'))
2487 EMSG2(_(e_letwrong), op);
2488 else
2489 {
2490 if (opt_type == 1) /* number */
2491 {
2492 if (*op == '+')
2493 n = numval + n;
2494 else
2495 n = numval - n;
2496 }
2497 else if (opt_type == 0 && stringval != NULL) /* string */
2498 {
2499 s = concat_str(stringval, s);
2500 vim_free(stringval);
2501 stringval = s;
2502 }
2503 }
2504 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002505 if (s != NULL)
2506 {
2507 set_option_value(arg, n, s, opt_flags);
2508 arg_end = p;
2509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002510 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002511 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002512 }
2513 }
2514
2515 /*
2516 * ":let @r = expr": Set register contents.
2517 */
2518 else if (*arg == '@')
2519 {
2520 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002521 if (op != NULL && (*op == '+' || *op == '-'))
2522 EMSG2(_(e_letwrong), op);
2523 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002524 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 EMSG(_(e_letunexp));
2526 else
2527 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002528 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 char_u *s;
2530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002531 p = get_tv_string_chk(tv);
2532 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002533 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002534 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 if (s != NULL)
2536 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002537 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 vim_free(s);
2539 }
2540 }
2541 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002543 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002544 arg_end = arg + 1;
2545 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002546 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
2548 }
2549
2550 /*
2551 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002553 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002555 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002556 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002558 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2562 EMSG(_(e_letunexp));
2563 else
2564 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 arg_end = p;
2567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002568 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002569 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002570 }
2571
2572 else
2573 EMSG2(_(e_invarg2), arg);
2574
2575 return arg_end;
2576}
2577
2578/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002579 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2580 */
2581 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002582check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002583{
2584 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2585 {
2586 EMSG2(_(e_readonlyvar), arg);
2587 return TRUE;
2588 }
2589 return FALSE;
2590}
2591
2592/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 * Get an lval: variable, Dict item or List item that can be assigned a value
2594 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2595 * "name.key", "name.key[expr]" etc.
2596 * Indexing only works if "name" is an existing List or Dictionary.
2597 * "name" points to the start of the name.
2598 * If "rettv" is not NULL it points to the value to be assigned.
2599 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2600 * wrong; must end in space or cmd separator.
2601 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002602 * flags:
2603 * GLV_QUIET: do not give error messages
2604 * GLV_NO_AUTOLOAD: do not use script autoloading
2605 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002607 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 */
2610 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002611get_lval(
2612 char_u *name,
2613 typval_T *rettv,
2614 lval_T *lp,
2615 int unlet,
2616 int skip,
2617 int flags, /* GLV_ values */
2618 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002621 char_u *expr_start, *expr_end;
2622 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002623 dictitem_T *v;
2624 typval_T var1;
2625 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002627 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002630 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002631 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002633 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002634 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635
2636 if (skip)
2637 {
2638 /* When skipping just find the end of the name. */
2639 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002640 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 }
2642
2643 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002644 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002645 if (expr_start != NULL)
2646 {
2647 /* Don't expand the name when we already know there is an error. */
2648 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2649 && *p != '[' && *p != '.')
2650 {
2651 EMSG(_(e_trailing));
2652 return NULL;
2653 }
2654
2655 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2656 if (lp->ll_exp_name == NULL)
2657 {
2658 /* Report an invalid expression in braces, unless the
2659 * expression evaluation has been cancelled due to an
2660 * aborting error, an interrupt, or an exception. */
2661 if (!aborting() && !quiet)
2662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002663 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 EMSG2(_(e_invarg2), name);
2665 return NULL;
2666 }
2667 }
2668 lp->ll_name = lp->ll_exp_name;
2669 }
2670 else
2671 lp->ll_name = name;
2672
2673 /* Without [idx] or .key we are done. */
2674 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2675 return p;
2676
2677 cc = *p;
2678 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002679 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 if (v == NULL && !quiet)
2681 EMSG2(_(e_undefvar), lp->ll_name);
2682 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002683 if (v == NULL)
2684 return NULL;
2685
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 /*
2687 * Loop until no more [idx] or .key is following.
2688 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002689 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2693 && !(lp->ll_tv->v_type == VAR_DICT
2694 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 if (!quiet)
2697 EMSG(_("E689: Can only index a List or Dictionary"));
2698 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002700 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002701 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (!quiet)
2703 EMSG(_("E708: [:] must come last"));
2704 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002705 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002706
Bram Moolenaar8c711452005-01-14 21:53:12 +00002707 len = -1;
2708 if (*p == '.')
2709 {
2710 key = p + 1;
2711 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2712 ;
2713 if (len == 0)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
2716 EMSG(_(e_emptykey));
2717 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 }
2719 p = key + len;
2720 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721 else
2722 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 if (*p == ':')
2726 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 else
2728 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 empty1 = FALSE;
2730 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002732 if (get_tv_string_chk(&var1) == NULL)
2733 {
2734 /* not a number or string */
2735 clear_tv(&var1);
2736 return NULL;
2737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
2739
2740 /* Optionally get the second index [ :expr]. */
2741 if (*p == ':')
2742 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002743 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002746 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002747 if (!empty1)
2748 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002749 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 if (rettv != NULL && (rettv->v_type != VAR_LIST
2752 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (!quiet)
2755 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (!empty1)
2757 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760 p = skipwhite(p + 1);
2761 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 else
2764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (!empty1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002772 if (get_tv_string_chk(&var2) == NULL)
2773 {
2774 /* not a number or string */
2775 if (!empty1)
2776 clear_tv(&var1);
2777 clear_tv(&var2);
2778 return NULL;
2779 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002782 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002787 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 if (!quiet)
2789 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 if (!empty1)
2791 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002792 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002793 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002794 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002795 }
2796
2797 /* Skip to past ']'. */
2798 ++p;
2799 }
2800
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002801 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002802 {
2803 if (len == -1)
2804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002805 /* "[key]": get key from "var1" */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002806 key = get_tv_string_chk(&var1); /* is number or string */
2807 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002808 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002810 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002811 }
2812 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002813 lp->ll_list = NULL;
2814 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002815 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002817 /* When assigning to a scope dictionary check that a function and
2818 * variable name is valid (only variable name unless it is l: or
2819 * g: dictionary). Disallow overwriting a builtin function. */
2820 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002821 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002822 int prevval;
2823 int wrong;
2824
2825 if (len != -1)
2826 {
2827 prevval = key[len];
2828 key[len] = NUL;
2829 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002830 else
2831 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002832 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2833 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002834 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002835 || !valid_varname(key);
2836 if (len != -1)
2837 key[len] = prevval;
2838 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 return NULL;
2840 }
2841
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 /* Can't add "v:" variable. */
2845 if (lp->ll_dict == &vimvardict)
2846 {
2847 EMSG2(_(e_illvar), name);
2848 return NULL;
2849 }
2850
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002851 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002855 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 if (len == -1)
2857 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 }
2860 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002861 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 if (len == -1)
2865 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002866 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 p = NULL;
2868 break;
2869 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002870 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002871 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002872 return NULL;
2873
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 if (len == -1)
2875 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878 else
2879 {
2880 /*
2881 * Get the number and item for the only or first index of the List.
2882 */
2883 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 else
2886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var1);
2889 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002890 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 lp->ll_list = lp->ll_tv->vval.v_list;
2892 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2893 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002895 if (lp->ll_n1 < 0)
2896 {
2897 lp->ll_n1 = 0;
2898 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2899 }
2900 }
2901 if (lp->ll_li == NULL)
2902 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 if (!quiet)
2906 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002907 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002908 }
2909
2910 /*
2911 * May need to find the item or absolute index for the second
2912 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 * When no index given: "lp->ll_empty2" is TRUE.
2914 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002915 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002917 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002918 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002919 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002921 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002923 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002924 {
2925 if (!quiet)
2926 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002928 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002930 }
2931
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2933 if (lp->ll_n1 < 0)
2934 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2935 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002936 {
2937 if (!quiet)
2938 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002939 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002940 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002941 }
2942
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002945 }
2946
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 return p;
2948}
2949
2950/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 */
2953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002954clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955{
2956 vim_free(lp->ll_exp_name);
2957 vim_free(lp->ll_newkey);
2958}
2959
2960/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002962 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002964 */
2965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002966set_var_lval(
2967 lval_T *lp,
2968 char_u *endp,
2969 typval_T *rettv,
2970 int copy,
2971 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972{
2973 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002974 listitem_T *ri;
2975 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976
2977 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002980 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002981 cc = *endp;
2982 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002983 if (op != NULL && *op != '=')
2984 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002985 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002986
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002987 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002988 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002989 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002990 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002991 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002992 if ((di == NULL
2993 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2994 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2995 FALSE)))
2996 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002997 set_var(lp->ll_name, &tv, FALSE);
2998 clear_tv(&tv);
2999 }
3000 }
3001 else
3002 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003004 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003005 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003006 else if (tv_check_lock(lp->ll_newkey == NULL
3007 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02003008 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003009 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 else if (lp->ll_range)
3011 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003012 listitem_T *ll_li = lp->ll_li;
3013 int ll_n1 = lp->ll_n1;
3014
3015 /*
3016 * Check whether any of the list items is locked
3017 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01003018 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003019 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02003020 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003021 return;
3022 ri = ri->li_next;
3023 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3024 break;
3025 ll_li = ll_li->li_next;
3026 ++ll_n1;
3027 }
3028
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 /*
3030 * Assign the List values to the list items.
3031 */
3032 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003033 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 if (op != NULL && *op != '=')
3035 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3036 else
3037 {
3038 clear_tv(&lp->ll_li->li_tv);
3039 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3040 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 ri = ri->li_next;
3042 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3043 break;
3044 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003045 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003046 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003047 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003048 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 ri = NULL;
3050 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003051 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003053 lp->ll_li = lp->ll_li->li_next;
3054 ++lp->ll_n1;
3055 }
3056 if (ri != NULL)
3057 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003058 else if (lp->ll_empty2
3059 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003060 : lp->ll_n1 != lp->ll_n2)
3061 EMSG(_("E711: List value has not enough items"));
3062 }
3063 else
3064 {
3065 /*
3066 * Assign to a List or Dictionary item.
3067 */
3068 if (lp->ll_newkey != NULL)
3069 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 if (op != NULL && *op != '=')
3071 {
3072 EMSG2(_(e_letwrong), op);
3073 return;
3074 }
3075
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003077 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 if (di == NULL)
3079 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003080 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3081 {
3082 vim_free(di);
3083 return;
3084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003085 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087 else if (op != NULL && *op != '=')
3088 {
3089 tv_op(lp->ll_tv, rettv, op);
3090 return;
3091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003093 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003094
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003095 /*
3096 * Assign the value to the variable or list item.
3097 */
3098 if (copy)
3099 copy_tv(rettv, lp->ll_tv);
3100 else
3101 {
3102 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003103 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003104 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003105 }
3106 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003107}
3108
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003110 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3111 * Returns OK or FAIL.
3112 */
3113 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003114tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003115{
3116 long n;
3117 char_u numbuf[NUMBUFLEN];
3118 char_u *s;
3119
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003120 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3121 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3122 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003123 {
3124 switch (tv1->v_type)
3125 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003126 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003127 case VAR_DICT:
3128 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003129 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003130 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003131 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003132 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 break;
3134
3135 case VAR_LIST:
3136 if (*op != '+' || tv2->v_type != VAR_LIST)
3137 break;
3138 /* List += List */
3139 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3140 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3141 return OK;
3142
3143 case VAR_NUMBER:
3144 case VAR_STRING:
3145 if (tv2->v_type == VAR_LIST)
3146 break;
3147 if (*op == '+' || *op == '-')
3148 {
3149 /* nr += nr or nr -= nr*/
3150 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151#ifdef FEAT_FLOAT
3152 if (tv2->v_type == VAR_FLOAT)
3153 {
3154 float_T f = n;
3155
3156 if (*op == '+')
3157 f += tv2->vval.v_float;
3158 else
3159 f -= tv2->vval.v_float;
3160 clear_tv(tv1);
3161 tv1->v_type = VAR_FLOAT;
3162 tv1->vval.v_float = f;
3163 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003164 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165#endif
3166 {
3167 if (*op == '+')
3168 n += get_tv_number(tv2);
3169 else
3170 n -= get_tv_number(tv2);
3171 clear_tv(tv1);
3172 tv1->v_type = VAR_NUMBER;
3173 tv1->vval.v_number = n;
3174 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 else
3177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003178 if (tv2->v_type == VAR_FLOAT)
3179 break;
3180
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 /* str .= str */
3182 s = get_tv_string(tv1);
3183 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3184 clear_tv(tv1);
3185 tv1->v_type = VAR_STRING;
3186 tv1->vval.v_string = s;
3187 }
3188 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003189
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003190 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003191#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003192 {
3193 float_T f;
3194
3195 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3196 && tv2->v_type != VAR_NUMBER
3197 && tv2->v_type != VAR_STRING))
3198 break;
3199 if (tv2->v_type == VAR_FLOAT)
3200 f = tv2->vval.v_float;
3201 else
3202 f = get_tv_number(tv2);
3203 if (*op == '+')
3204 tv1->vval.v_float += f;
3205 else
3206 tv1->vval.v_float -= f;
3207 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003208#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003209 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003210 }
3211 }
3212
3213 EMSG2(_(e_letwrong), op);
3214 return FAIL;
3215}
3216
3217/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003218 * Add a watcher to a list.
3219 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003220 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003221list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003222{
3223 lw->lw_next = l->lv_watch;
3224 l->lv_watch = lw;
3225}
3226
3227/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003228 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229 * No warning when it isn't found...
3230 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 lwp = &l->lv_watch;
3237 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3238 {
3239 if (lw == lwrem)
3240 {
3241 *lwp = lw->lw_next;
3242 break;
3243 }
3244 lwp = &lw->lw_next;
3245 }
3246}
3247
3248/*
3249 * Just before removing an item from a list: advance watchers to the next
3250 * item.
3251 */
3252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
3257 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3258 if (lw->lw_item == item)
3259 lw->lw_item = item->li_next;
3260}
3261
3262/*
3263 * Evaluate the expression used in a ":for var in expr" command.
3264 * "arg" points to "var".
3265 * Set "*errp" to TRUE for an error, FALSE otherwise;
3266 * Return a pointer that holds the info. Null when there is an error.
3267 */
3268 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003269eval_for_line(
3270 char_u *arg,
3271 int *errp,
3272 char_u **nextcmdp,
3273 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274{
Bram Moolenaar33570922005-01-25 22:26:29 +00003275 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 typval_T tv;
3278 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279
3280 *errp = TRUE; /* default: there is an error */
3281
Bram Moolenaar33570922005-01-25 22:26:29 +00003282 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 if (fi == NULL)
3284 return NULL;
3285
3286 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3287 if (expr == NULL)
3288 return fi;
3289
3290 expr = skipwhite(expr);
3291 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3292 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003293 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003294 return fi;
3295 }
3296
3297 if (skip)
3298 ++emsg_skip;
3299 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3300 {
3301 *errp = FALSE;
3302 if (!skip)
3303 {
3304 l = tv.vval.v_list;
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003305 if (tv.v_type != VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003306 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003308 clear_tv(&tv);
3309 }
Bram Moolenaard8585ed2016-05-01 23:05:53 +02003310 else if (l == NULL)
3311 {
3312 /* a null list is like an empty list: do nothing */
3313 clear_tv(&tv);
3314 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 else
3316 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003317 /* No need to increment the refcount, it's already set for the
3318 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003319 fi->fi_list = l;
3320 list_add_watch(l, &fi->fi_lw);
3321 fi->fi_lw.lw_item = l->lv_first;
3322 }
3323 }
3324 }
3325 if (skip)
3326 --emsg_skip;
3327
3328 return fi;
3329}
3330
3331/*
3332 * Use the first item in a ":for" list. Advance to the next.
3333 * Assign the values to the variable (list). "arg" points to the first one.
3334 * Return TRUE when a valid item was found, FALSE when at end of list or
3335 * something wrong.
3336 */
3337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003338next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003339{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003340 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003341 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003342 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343
3344 item = fi->fi_lw.lw_item;
3345 if (item == NULL)
3346 result = FALSE;
3347 else
3348 {
3349 fi->fi_lw.lw_item = item->li_next;
3350 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3351 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3352 }
3353 return result;
3354}
3355
3356/*
3357 * Free the structure used to store info used by ":for".
3358 */
3359 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361{
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003364 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003365 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003367 list_unref(fi->fi_list);
3368 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003369 vim_free(fi);
3370}
3371
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3373
3374 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003375set_context_for_expression(
3376 expand_T *xp,
3377 char_u *arg,
3378 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 int got_eq = FALSE;
3381 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003382 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003384 if (cmdidx == CMD_let)
3385 {
3386 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003387 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003388 {
3389 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003390 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003391 {
3392 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003394 if (vim_iswhite(*p))
3395 break;
3396 }
3397 return;
3398 }
3399 }
3400 else
3401 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3402 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 while ((xp->xp_pattern = vim_strpbrk(arg,
3404 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3405 {
3406 c = *xp->xp_pattern;
3407 if (c == '&')
3408 {
3409 c = xp->xp_pattern[1];
3410 if (c == '&')
3411 {
3412 ++xp->xp_pattern;
3413 xp->xp_context = cmdidx != CMD_let || got_eq
3414 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3415 }
3416 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003417 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003419 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3420 xp->xp_pattern += 2;
3421
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 }
3424 else if (c == '$')
3425 {
3426 /* environment variable */
3427 xp->xp_context = EXPAND_ENV_VARS;
3428 }
3429 else if (c == '=')
3430 {
3431 got_eq = TRUE;
3432 xp->xp_context = EXPAND_EXPRESSION;
3433 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003434 else if (c == '#'
3435 && xp->xp_context == EXPAND_EXPRESSION)
3436 {
3437 /* Autoload function/variable contains '#'. */
3438 break;
3439 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003440 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 && xp->xp_context == EXPAND_FUNCTIONS
3442 && vim_strchr(xp->xp_pattern, '(') == NULL)
3443 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003444 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 break;
3446 }
3447 else if (cmdidx != CMD_let || got_eq)
3448 {
3449 if (c == '"') /* string */
3450 {
3451 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3452 if (c == '\\' && xp->xp_pattern[1] != NUL)
3453 ++xp->xp_pattern;
3454 xp->xp_context = EXPAND_NOTHING;
3455 }
3456 else if (c == '\'') /* literal string */
3457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003458 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3460 /* skip */ ;
3461 xp->xp_context = EXPAND_NOTHING;
3462 }
3463 else if (c == '|')
3464 {
3465 if (xp->xp_pattern[1] == '|')
3466 {
3467 ++xp->xp_pattern;
3468 xp->xp_context = EXPAND_EXPRESSION;
3469 }
3470 else
3471 xp->xp_context = EXPAND_COMMANDS;
3472 }
3473 else
3474 xp->xp_context = EXPAND_EXPRESSION;
3475 }
3476 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003477 /* Doesn't look like something valid, expand as an expression
3478 * anyway. */
3479 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 arg = xp->xp_pattern;
3481 if (*arg != NUL)
3482 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3483 /* skip */ ;
3484 }
3485 xp->xp_pattern = arg;
3486}
3487
3488#endif /* FEAT_CMDL_COMPL */
3489
3490/*
3491 * ":1,25call func(arg1, arg2)" function call.
3492 */
3493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 char_u *arg = eap->arg;
3497 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003499 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003501 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 linenr_T lnum;
3503 int doesrange;
3504 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003505 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003506 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003508 if (eap->skip)
3509 {
3510 /* trans_function_name() doesn't work well when skipping, use eval0()
3511 * instead to skip to any following command, e.g. for:
3512 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003513 ++emsg_skip;
3514 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3515 clear_tv(&rettv);
3516 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003517 return;
3518 }
3519
Bram Moolenaar65639032016-03-16 21:40:30 +01003520 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003521 if (fudi.fd_newkey != NULL)
3522 {
3523 /* Still need to give an error message for missing key. */
3524 EMSG2(_(e_dictkey), fudi.fd_newkey);
3525 vim_free(fudi.fd_newkey);
3526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003527 if (tofree == NULL)
3528 return;
3529
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003530 /* Increase refcount on dictionary, it could get deleted when evaluating
3531 * the arguments. */
3532 if (fudi.fd_dict != NULL)
3533 ++fudi.fd_dict->dv_refcount;
3534
Bram Moolenaar65639032016-03-16 21:40:30 +01003535 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3536 * contents. For VAR_PARTIAL get its partial, unless we already have one
3537 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003538 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003539 name = deref_func_name(tofree, &len,
3540 partial != NULL ? NULL : &partial, FALSE);
3541
Bram Moolenaar532c7802005-01-27 14:44:31 +00003542 /* Skip white space to allow ":call func ()". Not good, but required for
3543 * backward compatibility. */
3544 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
3547 if (*startarg != '(')
3548 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003549 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 goto end;
3551 }
3552
3553 /*
3554 * When skipping, evaluate the function once, to find the end of the
3555 * arguments.
3556 * When the function takes a range, this is discovered after the first
3557 * call, and the loop is broken.
3558 */
3559 if (eap->skip)
3560 {
3561 ++emsg_skip;
3562 lnum = eap->line2; /* do it once, also with an invalid range */
3563 }
3564 else
3565 lnum = eap->line1;
3566 for ( ; lnum <= eap->line2; ++lnum)
3567 {
3568 if (!eap->skip && eap->addr_count > 0)
3569 {
3570 curwin->w_cursor.lnum = lnum;
3571 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003572#ifdef FEAT_VIRTUALEDIT
3573 curwin->w_cursor.coladd = 0;
3574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003577 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003578 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003579 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 failed = TRUE;
3582 break;
3583 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003584
3585 /* Handle a function returning a Funcref, Dictionary or List. */
3586 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3587 {
3588 failed = TRUE;
3589 break;
3590 }
3591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (doesrange || eap->skip)
3594 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003597 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003599 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (aborting())
3601 break;
3602 }
3603 if (eap->skip)
3604 --emsg_skip;
3605
3606 if (!failed)
3607 {
3608 /* Check for trailing illegal characters and a following command. */
3609 if (!ends_excmd(*arg))
3610 {
3611 emsg_severe = TRUE;
3612 EMSG(_(e_trailing));
3613 }
3614 else
3615 eap->nextcmd = check_nextcmd(arg);
3616 }
3617
3618end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003619 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003620 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621}
3622
3623/*
3624 * ":unlet[!] var1 ... " command.
3625 */
3626 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003629 ex_unletlock(eap, eap->arg, 0);
3630}
3631
3632/*
3633 * ":lockvar" and ":unlockvar" commands
3634 */
3635 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003636ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003639 int deep = 2;
3640
3641 if (eap->forceit)
3642 deep = -1;
3643 else if (vim_isdigit(*arg))
3644 {
3645 deep = getdigits(&arg);
3646 arg = skipwhite(arg);
3647 }
3648
3649 ex_unletlock(eap, arg, deep);
3650}
3651
3652/*
3653 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3654 */
3655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003656ex_unletlock(
3657 exarg_T *eap,
3658 char_u *argstart,
3659 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003660{
3661 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003664 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 do
3667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003668 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003669 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003670 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 if (lv.ll_name == NULL)
3672 error = TRUE; /* error but continue parsing */
3673 if (name_end == NULL || (!vim_iswhite(*name_end)
3674 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003676 if (name_end != NULL)
3677 {
3678 emsg_severe = TRUE;
3679 EMSG(_(e_trailing));
3680 }
3681 if (!(eap->skip || error))
3682 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 break;
3684 }
3685
3686 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003687 {
3688 if (eap->cmdidx == CMD_unlet)
3689 {
3690 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3691 error = TRUE;
3692 }
3693 else
3694 {
3695 if (do_lock_var(&lv, name_end, deep,
3696 eap->cmdidx == CMD_lockvar) == FAIL)
3697 error = TRUE;
3698 }
3699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003701 if (!eap->skip)
3702 clear_lval(&lv);
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 arg = skipwhite(name_end);
3705 } while (!ends_excmd(*arg));
3706
3707 eap->nextcmd = check_nextcmd(arg);
3708}
3709
Bram Moolenaar8c711452005-01-14 21:53:12 +00003710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003711do_unlet_var(
3712 lval_T *lp,
3713 char_u *name_end,
3714 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003715{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 int ret = OK;
3717 int cc;
3718
3719 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 cc = *name_end;
3722 *name_end = NUL;
3723
3724 /* Normal name or expanded name. */
3725 if (check_changedtick(lp->ll_name))
3726 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003727 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003728 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003730 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003731 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003732 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003734 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003735 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 else if (lp->ll_range)
3737 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003739 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003740 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003741
3742 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3743 {
3744 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003745 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003746 return FAIL;
3747 ll_li = li;
3748 ++ll_n1;
3749 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003750
3751 /* Delete a range of List items. */
3752 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3753 {
3754 li = lp->ll_li->li_next;
3755 listitem_remove(lp->ll_list, lp->ll_li);
3756 lp->ll_li = li;
3757 ++lp->ll_n1;
3758 }
3759 }
3760 else
3761 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003762 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003763 /* unlet a List item. */
3764 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003765 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003766 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003767 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003768 }
3769
3770 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003771}
3772
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773/*
3774 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 */
3777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 hashtab_T *ht;
3781 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003783 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003784 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
Bram Moolenaar33570922005-01-25 22:26:29 +00003786 ht = find_var_ht(name, &varname);
3787 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003789 if (ht == &globvarht)
3790 d = &globvardict;
3791 else if (current_funccal != NULL
3792 && ht == &current_funccal->l_vars.dv_hashtab)
3793 d = &current_funccal->l_vars;
3794 else if (ht == &compat_hashtab)
3795 d = &vimvardict;
3796 else
3797 {
3798 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3799 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3800 }
3801 if (d == NULL)
3802 {
3803 EMSG2(_(e_intern2), "do_unlet()");
3804 return FAIL;
3805 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003806 hi = hash_find(ht, varname);
3807 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003808 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003809 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003810 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003811 || var_check_ro(di->di_flags, name, FALSE)
3812 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003813 return FAIL;
3814
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003815 delete_var(ht, hi);
3816 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 if (forceit)
3820 return OK;
3821 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 return FAIL;
3823}
3824
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003825/*
3826 * Lock or unlock variable indicated by "lp".
3827 * "deep" is the levels to go (-1 for unlimited);
3828 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3829 */
3830 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003831do_lock_var(
3832 lval_T *lp,
3833 char_u *name_end,
3834 int deep,
3835 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003836{
3837 int ret = OK;
3838 int cc;
3839 dictitem_T *di;
3840
3841 if (deep == 0) /* nothing to do */
3842 return OK;
3843
3844 if (lp->ll_tv == NULL)
3845 {
3846 cc = *name_end;
3847 *name_end = NUL;
3848
3849 /* Normal name or expanded name. */
3850 if (check_changedtick(lp->ll_name))
3851 ret = FAIL;
3852 else
3853 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003854 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003855 if (di == NULL)
3856 ret = FAIL;
3857 else
3858 {
3859 if (lock)
3860 di->di_flags |= DI_FLAGS_LOCK;
3861 else
3862 di->di_flags &= ~DI_FLAGS_LOCK;
3863 item_lock(&di->di_tv, deep, lock);
3864 }
3865 }
3866 *name_end = cc;
3867 }
3868 else if (lp->ll_range)
3869 {
3870 listitem_T *li = lp->ll_li;
3871
3872 /* (un)lock a range of List items. */
3873 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3874 {
3875 item_lock(&li->li_tv, deep, lock);
3876 li = li->li_next;
3877 ++lp->ll_n1;
3878 }
3879 }
3880 else if (lp->ll_list != NULL)
3881 /* (un)lock a List item. */
3882 item_lock(&lp->ll_li->li_tv, deep, lock);
3883 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003884 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003885 item_lock(&lp->ll_di->di_tv, deep, lock);
3886
3887 return ret;
3888}
3889
3890/*
3891 * Lock or unlock an item. "deep" is nr of levels to go.
3892 */
3893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003894item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003895{
3896 static int recurse = 0;
3897 list_T *l;
3898 listitem_T *li;
3899 dict_T *d;
3900 hashitem_T *hi;
3901 int todo;
3902
3903 if (recurse >= DICT_MAXNEST)
3904 {
3905 EMSG(_("E743: variable nested too deep for (un)lock"));
3906 return;
3907 }
3908 if (deep == 0)
3909 return;
3910 ++recurse;
3911
3912 /* lock/unlock the item itself */
3913 if (lock)
3914 tv->v_lock |= VAR_LOCKED;
3915 else
3916 tv->v_lock &= ~VAR_LOCKED;
3917
3918 switch (tv->v_type)
3919 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003920 case VAR_UNKNOWN:
3921 case VAR_NUMBER:
3922 case VAR_STRING:
3923 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003924 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003925 case VAR_FLOAT:
3926 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003927 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003928 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003929 break;
3930
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003931 case VAR_LIST:
3932 if ((l = tv->vval.v_list) != NULL)
3933 {
3934 if (lock)
3935 l->lv_lock |= VAR_LOCKED;
3936 else
3937 l->lv_lock &= ~VAR_LOCKED;
3938 if (deep < 0 || deep > 1)
3939 /* recursive: lock/unlock the items the List contains */
3940 for (li = l->lv_first; li != NULL; li = li->li_next)
3941 item_lock(&li->li_tv, deep - 1, lock);
3942 }
3943 break;
3944 case VAR_DICT:
3945 if ((d = tv->vval.v_dict) != NULL)
3946 {
3947 if (lock)
3948 d->dv_lock |= VAR_LOCKED;
3949 else
3950 d->dv_lock &= ~VAR_LOCKED;
3951 if (deep < 0 || deep > 1)
3952 {
3953 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003954 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003955 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3956 {
3957 if (!HASHITEM_EMPTY(hi))
3958 {
3959 --todo;
3960 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3961 }
3962 }
3963 }
3964 }
3965 }
3966 --recurse;
3967}
3968
Bram Moolenaara40058a2005-07-11 22:42:07 +00003969/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003970 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3971 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003972 */
3973 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003974tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003975{
3976 return (tv->v_lock & VAR_LOCKED)
3977 || (tv->v_type == VAR_LIST
3978 && tv->vval.v_list != NULL
3979 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3980 || (tv->v_type == VAR_DICT
3981 && tv->vval.v_dict != NULL
3982 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3983}
3984
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3986/*
3987 * Delete all "menutrans_" variables.
3988 */
3989 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003990del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991{
Bram Moolenaar33570922005-01-25 22:26:29 +00003992 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003993 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
Bram Moolenaar33570922005-01-25 22:26:29 +00003995 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003996 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003997 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003998 {
3999 if (!HASHITEM_EMPTY(hi))
4000 {
4001 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00004002 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
4003 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00004004 }
4005 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004006 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007}
4008#endif
4009
4010#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4011
4012/*
4013 * Local string buffer for the next two functions to store a variable name
4014 * with its prefix. Allocated in cat_prefix_varname(), freed later in
4015 * get_user_var_name().
4016 */
4017
Bram Moolenaar48e697e2016-01-23 22:17:30 +01004018static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019
4020static char_u *varnamebuf = NULL;
4021static int varnamebuflen = 0;
4022
4023/*
4024 * Function to concatenate a prefix and a variable name.
4025 */
4026 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004027cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
4029 int len;
4030
4031 len = (int)STRLEN(name) + 3;
4032 if (len > varnamebuflen)
4033 {
4034 vim_free(varnamebuf);
4035 len += 10; /* some additional space */
4036 varnamebuf = alloc(len);
4037 if (varnamebuf == NULL)
4038 {
4039 varnamebuflen = 0;
4040 return NULL;
4041 }
4042 varnamebuflen = len;
4043 }
4044 *varnamebuf = prefix;
4045 varnamebuf[1] = ':';
4046 STRCPY(varnamebuf + 2, name);
4047 return varnamebuf;
4048}
4049
4050/*
4051 * Function given to ExpandGeneric() to obtain the list of user defined
4052 * (global/buffer/window/built-in) variable names.
4053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004055get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004057 static long_u gdone;
4058 static long_u bdone;
4059 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004060#ifdef FEAT_WINDOWS
4061 static long_u tdone;
4062#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 static int vidx;
4064 static hashitem_T *hi;
4065 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066
4067 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004068 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004069 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004070#ifdef FEAT_WINDOWS
4071 tdone = 0;
4072#endif
4073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004074
4075 /* Global variables */
4076 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004078 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004079 hi = globvarht.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 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4085 return cat_prefix_varname('g', hi->hi_key);
4086 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004088
4089 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004090 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004091 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004093 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004094 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004095 else
4096 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004097 while (HASHITEM_EMPTY(hi))
4098 ++hi;
4099 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004101 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004103 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 return (char_u *)"b:changedtick";
4105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004106
4107 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004108 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004111 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004112 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004113 else
4114 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004115 while (HASHITEM_EMPTY(hi))
4116 ++hi;
4117 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004119
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004120#ifdef FEAT_WINDOWS
4121 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004122 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004123 if (tdone < ht->ht_used)
4124 {
4125 if (tdone++ == 0)
4126 hi = ht->ht_array;
4127 else
4128 ++hi;
4129 while (HASHITEM_EMPTY(hi))
4130 ++hi;
4131 return cat_prefix_varname('t', hi->hi_key);
4132 }
4133#endif
4134
Bram Moolenaar33570922005-01-25 22:26:29 +00004135 /* v: variables */
4136 if (vidx < VV_LEN)
4137 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138
4139 vim_free(varnamebuf);
4140 varnamebuf = NULL;
4141 varnamebuflen = 0;
4142 return NULL;
4143}
4144
4145#endif /* FEAT_CMDL_COMPL */
4146
4147/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004148 * Return TRUE if "pat" matches "text".
4149 * Does not use 'cpo' and always uses 'magic'.
4150 */
4151 static int
4152pattern_match(char_u *pat, char_u *text, int ic)
4153{
4154 int matches = FALSE;
4155 char_u *save_cpo;
4156 regmatch_T regmatch;
4157
4158 /* avoid 'l' flag in 'cpoptions' */
4159 save_cpo = p_cpo;
4160 p_cpo = (char_u *)"";
4161 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4162 if (regmatch.regprog != NULL)
4163 {
4164 regmatch.rm_ic = ic;
4165 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4166 vim_regfree(regmatch.regprog);
4167 }
4168 p_cpo = save_cpo;
4169 return matches;
4170}
4171
4172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * types for expressions.
4174 */
4175typedef enum
4176{
4177 TYPE_UNKNOWN = 0
4178 , TYPE_EQUAL /* == */
4179 , TYPE_NEQUAL /* != */
4180 , TYPE_GREATER /* > */
4181 , TYPE_GEQUAL /* >= */
4182 , TYPE_SMALLER /* < */
4183 , TYPE_SEQUAL /* <= */
4184 , TYPE_MATCH /* =~ */
4185 , TYPE_NOMATCH /* !~ */
4186} exptype_T;
4187
4188/*
4189 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4192 */
4193
4194/*
4195 * Handle zero level expression.
4196 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004198 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 * Return OK or FAIL.
4200 */
4201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202eval0(
4203 char_u *arg,
4204 typval_T *rettv,
4205 char_u **nextcmd,
4206 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207{
4208 int ret;
4209 char_u *p;
4210
4211 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 if (ret == FAIL || !ends_excmd(*p))
4214 {
4215 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 /*
4218 * Report the invalid expression unless the expression evaluation has
4219 * been cancelled due to an aborting error, an interrupt, or an
4220 * exception.
4221 */
4222 if (!aborting())
4223 EMSG2(_(e_invexpr2), arg);
4224 ret = FAIL;
4225 }
4226 if (nextcmd != NULL)
4227 *nextcmd = check_nextcmd(p);
4228
4229 return ret;
4230}
4231
4232/*
4233 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004234 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 *
4236 * "arg" must point to the first non-white of the expression.
4237 * "arg" is advanced to the next non-white after the recognized expression.
4238 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004239 * Note: "rettv.v_lock" is not set.
4240 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 * Return OK or FAIL.
4242 */
4243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004247 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
4249 /*
4250 * Get the first variable.
4251 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return FAIL;
4254
4255 if ((*arg)[0] == '?')
4256 {
4257 result = FALSE;
4258 if (evaluate)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 int error = FALSE;
4261
4262 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004264 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004265 if (error)
4266 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
4269 /*
4270 * Get the second variable.
4271 */
4272 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 return FAIL;
4275
4276 /*
4277 * Check for the ":".
4278 */
4279 if ((*arg)[0] != ':')
4280 {
4281 EMSG(_("E109: Missing ':' after '?'"));
4282 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 return FAIL;
4285 }
4286
4287 /*
4288 * Get the third variable.
4289 */
4290 *arg = skipwhite(*arg + 1);
4291 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4292 {
4293 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 return FAIL;
4296 }
4297 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 }
4300
4301 return OK;
4302}
4303
4304/*
4305 * Handle first level expression:
4306 * expr2 || expr2 || expr2 logical OR
4307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004314eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 long result;
4318 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321 /*
4322 * Get the first variable.
4323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326
4327 /*
4328 * Repeat until there is no following "||".
4329 */
4330 first = TRUE;
4331 result = FALSE;
4332 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4333 {
4334 if (evaluate && first)
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (error)
4340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 first = FALSE;
4342 }
4343
4344 /*
4345 * Get the second variable.
4346 */
4347 *arg = skipwhite(*arg + 2);
4348 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4349 return FAIL;
4350
4351 /*
4352 * Compute the result.
4353 */
4354 if (evaluate && !result)
4355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (error)
4360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * Handle second level expression:
4374 * expr3 && expr3 && expr3 logical AND
4375 *
4376 * "arg" must point to the first non-white of the expression.
4377 * "arg" is advanced to the next non-white after the recognized expression.
4378 *
4379 * Return OK or FAIL.
4380 */
4381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004382eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
Bram Moolenaar33570922005-01-25 22:26:29 +00004384 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 long result;
4386 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004387 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /*
4390 * Get the first variable.
4391 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 return FAIL;
4394
4395 /*
4396 * Repeat until there is no following "&&".
4397 */
4398 first = TRUE;
4399 result = TRUE;
4400 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4401 {
4402 if (evaluate && first)
4403 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004404 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004407 if (error)
4408 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 first = FALSE;
4410 }
4411
4412 /*
4413 * Get the second variable.
4414 */
4415 *arg = skipwhite(*arg + 2);
4416 if (eval4(arg, &var2, evaluate && result) == FAIL)
4417 return FAIL;
4418
4419 /*
4420 * Compute the result.
4421 */
4422 if (evaluate && result)
4423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004424 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004427 if (error)
4428 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430 if (evaluate)
4431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004432 rettv->v_type = VAR_NUMBER;
4433 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
4435 }
4436
4437 return OK;
4438}
4439
4440/*
4441 * Handle third level expression:
4442 * var1 == var2
4443 * var1 =~ var2
4444 * var1 != var2
4445 * var1 !~ var2
4446 * var1 > var2
4447 * var1 >= var2
4448 * var1 < var2
4449 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004450 * var1 is var2
4451 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 *
4453 * "arg" must point to the first non-white of the expression.
4454 * "arg" is advanced to the next non-white after the recognized expression.
4455 *
4456 * Return OK or FAIL.
4457 */
4458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004459eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460{
Bram Moolenaar33570922005-01-25 22:26:29 +00004461 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 char_u *p;
4463 int i;
4464 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 int len = 2;
4467 long n1, n2;
4468 char_u *s1, *s2;
4469 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
4472 /*
4473 * Get the first variable.
4474 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004475 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FAIL;
4477
4478 p = *arg;
4479 switch (p[0])
4480 {
4481 case '=': if (p[1] == '=')
4482 type = TYPE_EQUAL;
4483 else if (p[1] == '~')
4484 type = TYPE_MATCH;
4485 break;
4486 case '!': if (p[1] == '=')
4487 type = TYPE_NEQUAL;
4488 else if (p[1] == '~')
4489 type = TYPE_NOMATCH;
4490 break;
4491 case '>': if (p[1] != '=')
4492 {
4493 type = TYPE_GREATER;
4494 len = 1;
4495 }
4496 else
4497 type = TYPE_GEQUAL;
4498 break;
4499 case '<': if (p[1] != '=')
4500 {
4501 type = TYPE_SMALLER;
4502 len = 1;
4503 }
4504 else
4505 type = TYPE_SEQUAL;
4506 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004507 case 'i': if (p[1] == 's')
4508 {
4509 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4510 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004511 i = p[len];
4512 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004513 {
4514 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4515 type_is = TRUE;
4516 }
4517 }
4518 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520
4521 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004522 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 */
4524 if (type != TYPE_UNKNOWN)
4525 {
4526 /* extra question mark appended: ignore case */
4527 if (p[len] == '?')
4528 {
4529 ic = TRUE;
4530 ++len;
4531 }
4532 /* extra '#' appended: match case */
4533 else if (p[len] == '#')
4534 {
4535 ic = FALSE;
4536 ++len;
4537 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004538 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
4540 ic = p_ic;
4541
4542 /*
4543 * Get the second variable.
4544 */
4545 *arg = skipwhite(p + len);
4546 if (eval5(arg, &var2, evaluate) == FAIL)
4547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004548 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 return FAIL;
4550 }
4551
4552 if (evaluate)
4553 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 if (type_is && rettv->v_type != var2.v_type)
4555 {
4556 /* For "is" a different type always means FALSE, for "notis"
4557 * it means TRUE. */
4558 n1 = (type == TYPE_NEQUAL);
4559 }
4560 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4561 {
4562 if (type_is)
4563 {
4564 n1 = (rettv->v_type == var2.v_type
4565 && rettv->vval.v_list == var2.vval.v_list);
4566 if (type == TYPE_NEQUAL)
4567 n1 = !n1;
4568 }
4569 else if (rettv->v_type != var2.v_type
4570 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4571 {
4572 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004573 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004574 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004575 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004576 clear_tv(rettv);
4577 clear_tv(&var2);
4578 return FAIL;
4579 }
4580 else
4581 {
4582 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004583 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4584 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004585 if (type == TYPE_NEQUAL)
4586 n1 = !n1;
4587 }
4588 }
4589
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004590 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4591 {
4592 if (type_is)
4593 {
4594 n1 = (rettv->v_type == var2.v_type
4595 && rettv->vval.v_dict == var2.vval.v_dict);
4596 if (type == TYPE_NEQUAL)
4597 n1 = !n1;
4598 }
4599 else if (rettv->v_type != var2.v_type
4600 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4601 {
4602 if (rettv->v_type != var2.v_type)
4603 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4604 else
4605 EMSG(_("E736: Invalid operation for Dictionary"));
4606 clear_tv(rettv);
4607 clear_tv(&var2);
4608 return FAIL;
4609 }
4610 else
4611 {
4612 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004613 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4614 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004615 if (type == TYPE_NEQUAL)
4616 n1 = !n1;
4617 }
4618 }
4619
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004620 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4621 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004622 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004623 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004624 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004625 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004626 clear_tv(rettv);
4627 clear_tv(&var2);
4628 return FAIL;
4629 }
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004630 if ((rettv->v_type == VAR_PARTIAL
4631 && rettv->vval.v_partial == NULL)
4632 || (var2.v_type == VAR_PARTIAL
4633 && var2.vval.v_partial == NULL))
4634 /* when a partial is NULL assume not equal */
4635 n1 = FALSE;
4636 else if (type_is)
4637 {
4638 if (rettv->v_type == VAR_FUNC && var2.v_type == VAR_FUNC)
4639 /* strings are considered the same if their value is
4640 * the same */
4641 n1 = tv_equal(rettv, &var2, ic, FALSE);
4642 else if (rettv->v_type == VAR_PARTIAL
4643 && var2.v_type == VAR_PARTIAL)
4644 n1 = (rettv->vval.v_partial == var2.vval.v_partial);
4645 else
4646 n1 = FALSE;
4647 }
4648 else
4649 n1 = tv_equal(rettv, &var2, ic, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004650 if (type == TYPE_NEQUAL)
4651 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004652 }
4653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004654#ifdef FEAT_FLOAT
4655 /*
4656 * If one of the two variables is a float, compare as a float.
4657 * When using "=~" or "!~", always compare as string.
4658 */
4659 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4660 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4661 {
4662 float_T f1, f2;
4663
4664 if (rettv->v_type == VAR_FLOAT)
4665 f1 = rettv->vval.v_float;
4666 else
4667 f1 = get_tv_number(rettv);
4668 if (var2.v_type == VAR_FLOAT)
4669 f2 = var2.vval.v_float;
4670 else
4671 f2 = get_tv_number(&var2);
4672 n1 = FALSE;
4673 switch (type)
4674 {
4675 case TYPE_EQUAL: n1 = (f1 == f2); break;
4676 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4677 case TYPE_GREATER: n1 = (f1 > f2); break;
4678 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4679 case TYPE_SMALLER: n1 = (f1 < f2); break;
4680 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4681 case TYPE_UNKNOWN:
4682 case TYPE_MATCH:
4683 case TYPE_NOMATCH: break; /* avoid gcc warning */
4684 }
4685 }
4686#endif
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 /*
4689 * If one of the two variables is a number, compare as a number.
4690 * When using "=~" or "!~", always compare as string.
4691 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004692 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 n1 = get_tv_number(rettv);
4696 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 switch (type)
4698 {
4699 case TYPE_EQUAL: n1 = (n1 == n2); break;
4700 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4701 case TYPE_GREATER: n1 = (n1 > n2); break;
4702 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4703 case TYPE_SMALLER: n1 = (n1 < n2); break;
4704 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4705 case TYPE_UNKNOWN:
4706 case TYPE_MATCH:
4707 case TYPE_NOMATCH: break; /* avoid gcc warning */
4708 }
4709 }
4710 else
4711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 s1 = get_tv_string_buf(rettv, buf1);
4713 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4715 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4716 else
4717 i = 0;
4718 n1 = FALSE;
4719 switch (type)
4720 {
4721 case TYPE_EQUAL: n1 = (i == 0); break;
4722 case TYPE_NEQUAL: n1 = (i != 0); break;
4723 case TYPE_GREATER: n1 = (i > 0); break;
4724 case TYPE_GEQUAL: n1 = (i >= 0); break;
4725 case TYPE_SMALLER: n1 = (i < 0); break;
4726 case TYPE_SEQUAL: n1 = (i <= 0); break;
4727
4728 case TYPE_MATCH:
4729 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004730 n1 = pattern_match(s2, s1, ic);
4731 if (type == TYPE_NOMATCH)
4732 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 break;
4734
4735 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4736 }
4737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004738 clear_tv(rettv);
4739 clear_tv(&var2);
4740 rettv->v_type = VAR_NUMBER;
4741 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
4743 }
4744
4745 return OK;
4746}
4747
4748/*
4749 * Handle fourth level expression:
4750 * + number addition
4751 * - number subtraction
4752 * . string concatenation
4753 *
4754 * "arg" must point to the first non-white of the expression.
4755 * "arg" is advanced to the next non-white after the recognized expression.
4756 *
4757 * Return OK or FAIL.
4758 */
4759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004760eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761{
Bram Moolenaar33570922005-01-25 22:26:29 +00004762 typval_T var2;
4763 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 int op;
4765 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004766#ifdef FEAT_FLOAT
4767 float_T f1 = 0, f2 = 0;
4768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 char_u *s1, *s2;
4770 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4771 char_u *p;
4772
4773 /*
4774 * Get the first variable.
4775 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004776 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 return FAIL;
4778
4779 /*
4780 * Repeat computing, until no '+', '-' or '.' is following.
4781 */
4782 for (;;)
4783 {
4784 op = **arg;
4785 if (op != '+' && op != '-' && op != '.')
4786 break;
4787
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004788 if ((op != '+' || rettv->v_type != VAR_LIST)
4789#ifdef FEAT_FLOAT
4790 && (op == '.' || rettv->v_type != VAR_FLOAT)
4791#endif
4792 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004793 {
4794 /* For "list + ...", an illegal use of the first operand as
4795 * a number cannot be determined before evaluating the 2nd
4796 * operand: if this is also a list, all is ok.
4797 * For "something . ...", "something - ..." or "non-list + ...",
4798 * we know that the first operand needs to be a string or number
4799 * without evaluating the 2nd operand. So check before to avoid
4800 * side effects after an error. */
4801 if (evaluate && get_tv_string_chk(rettv) == NULL)
4802 {
4803 clear_tv(rettv);
4804 return FAIL;
4805 }
4806 }
4807
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 /*
4809 * Get the second variable.
4810 */
4811 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004812 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 return FAIL;
4816 }
4817
4818 if (evaluate)
4819 {
4820 /*
4821 * Compute the result.
4822 */
4823 if (op == '.')
4824 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004825 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4826 s2 = get_tv_string_buf_chk(&var2, buf2);
4827 if (s2 == NULL) /* type error ? */
4828 {
4829 clear_tv(rettv);
4830 clear_tv(&var2);
4831 return FAIL;
4832 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004833 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004834 clear_tv(rettv);
4835 rettv->v_type = VAR_STRING;
4836 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004838 else if (op == '+' && rettv->v_type == VAR_LIST
4839 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004840 {
4841 /* concatenate Lists */
4842 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4843 &var3) == FAIL)
4844 {
4845 clear_tv(rettv);
4846 clear_tv(&var2);
4847 return FAIL;
4848 }
4849 clear_tv(rettv);
4850 *rettv = var3;
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else
4853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 int error = FALSE;
4855
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856#ifdef FEAT_FLOAT
4857 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004858 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004859 f1 = rettv->vval.v_float;
4860 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004861 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004862 else
4863#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004864 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004865 n1 = get_tv_number_chk(rettv, &error);
4866 if (error)
4867 {
4868 /* This can only happen for "list + non-list". For
4869 * "non-list + ..." or "something - ...", we returned
4870 * before evaluating the 2nd operand. */
4871 clear_tv(rettv);
4872 return FAIL;
4873 }
4874#ifdef FEAT_FLOAT
4875 if (var2.v_type == VAR_FLOAT)
4876 f1 = n1;
4877#endif
4878 }
4879#ifdef FEAT_FLOAT
4880 if (var2.v_type == VAR_FLOAT)
4881 {
4882 f2 = var2.vval.v_float;
4883 n2 = 0;
4884 }
4885 else
4886#endif
4887 {
4888 n2 = get_tv_number_chk(&var2, &error);
4889 if (error)
4890 {
4891 clear_tv(rettv);
4892 clear_tv(&var2);
4893 return FAIL;
4894 }
4895#ifdef FEAT_FLOAT
4896 if (rettv->v_type == VAR_FLOAT)
4897 f2 = n2;
4898#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004899 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004900 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901
4902#ifdef FEAT_FLOAT
4903 /* If there is a float on either side the result is a float. */
4904 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4905 {
4906 if (op == '+')
4907 f1 = f1 + f2;
4908 else
4909 f1 = f1 - f2;
4910 rettv->v_type = VAR_FLOAT;
4911 rettv->vval.v_float = f1;
4912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004914#endif
4915 {
4916 if (op == '+')
4917 n1 = n1 + n2;
4918 else
4919 n1 = n1 - n2;
4920 rettv->v_type = VAR_NUMBER;
4921 rettv->vval.v_number = n1;
4922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004924 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 }
4927 return OK;
4928}
4929
4930/*
4931 * Handle fifth level expression:
4932 * * number multiplication
4933 * / number division
4934 * % number modulo
4935 *
4936 * "arg" must point to the first non-white of the expression.
4937 * "arg" is advanced to the next non-white after the recognized expression.
4938 *
4939 * Return OK or FAIL.
4940 */
4941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004942eval6(
4943 char_u **arg,
4944 typval_T *rettv,
4945 int evaluate,
4946 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947{
Bram Moolenaar33570922005-01-25 22:26:29 +00004948 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 int op;
4950 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004951#ifdef FEAT_FLOAT
4952 int use_float = FALSE;
4953 float_T f1 = 0, f2;
4954#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004955 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 /*
4958 * Get the first variable.
4959 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004960 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 return FAIL;
4962
4963 /*
4964 * Repeat computing, until no '*', '/' or '%' is following.
4965 */
4966 for (;;)
4967 {
4968 op = **arg;
4969 if (op != '*' && op != '/' && op != '%')
4970 break;
4971
4972 if (evaluate)
4973 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004974#ifdef FEAT_FLOAT
4975 if (rettv->v_type == VAR_FLOAT)
4976 {
4977 f1 = rettv->vval.v_float;
4978 use_float = TRUE;
4979 n1 = 0;
4980 }
4981 else
4982#endif
4983 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004985 if (error)
4986 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else
4989 n1 = 0;
4990
4991 /*
4992 * Get the second variable.
4993 */
4994 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004995 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 return FAIL;
4997
4998 if (evaluate)
4999 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005000#ifdef FEAT_FLOAT
5001 if (var2.v_type == VAR_FLOAT)
5002 {
5003 if (!use_float)
5004 {
5005 f1 = n1;
5006 use_float = TRUE;
5007 }
5008 f2 = var2.vval.v_float;
5009 n2 = 0;
5010 }
5011 else
5012#endif
5013 {
5014 n2 = get_tv_number_chk(&var2, &error);
5015 clear_tv(&var2);
5016 if (error)
5017 return FAIL;
5018#ifdef FEAT_FLOAT
5019 if (use_float)
5020 f2 = n2;
5021#endif
5022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 /*
5025 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005026 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005028#ifdef FEAT_FLOAT
5029 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031 if (op == '*')
5032 f1 = f1 * f2;
5033 else if (op == '/')
5034 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005035# ifdef VMS
5036 /* VMS crashes on divide by zero, work around it */
5037 if (f2 == 0.0)
5038 {
5039 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005040 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005041 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005042 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005043 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02005044 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005045 }
5046 else
5047 f1 = f1 / f2;
5048# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005049 /* We rely on the floating point library to handle divide
5050 * by zero to result in "inf" and not a crash. */
5051 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005052# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005055 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005056 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005057 return FAIL;
5058 }
5059 rettv->v_type = VAR_FLOAT;
5060 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005063#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005065 if (op == '*')
5066 n1 = n1 * n2;
5067 else if (op == '/')
5068 {
5069 if (n2 == 0) /* give an error message? */
5070 {
5071 if (n1 == 0)
5072 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5073 else if (n1 < 0)
5074 n1 = -0x7fffffffL;
5075 else
5076 n1 = 0x7fffffffL;
5077 }
5078 else
5079 n1 = n1 / n2;
5080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005082 {
5083 if (n2 == 0) /* give an error message? */
5084 n1 = 0;
5085 else
5086 n1 = n1 % n2;
5087 }
5088 rettv->v_type = VAR_NUMBER;
5089 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 }
5093
5094 return OK;
5095}
5096
5097/*
5098 * Handle sixth level expression:
5099 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005100 * "string" string constant
5101 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 * &option-name option value
5103 * @r register contents
5104 * identifier variable value
5105 * function() function call
5106 * $VAR environment variable
5107 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005108 * [expr, expr] List
5109 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 *
5111 * Also handle:
5112 * ! in front logical NOT
5113 * - in front unary minus
5114 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005115 * trailing [] subscript in String or List
5116 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 *
5118 * "arg" must point to the first non-white of the expression.
5119 * "arg" is advanced to the next non-white after the recognized expression.
5120 *
5121 * Return OK or FAIL.
5122 */
5123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005124eval7(
5125 char_u **arg,
5126 typval_T *rettv,
5127 int evaluate,
5128 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 long n;
5131 int len;
5132 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 char_u *start_leader, *end_leader;
5134 int ret = OK;
5135 char_u *alias;
5136
5137 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005138 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005139 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142
5143 /*
5144 * Skip '!' and '-' characters. They are handled later.
5145 */
5146 start_leader = *arg;
5147 while (**arg == '!' || **arg == '-' || **arg == '+')
5148 *arg = skipwhite(*arg + 1);
5149 end_leader = *arg;
5150
5151 switch (**arg)
5152 {
5153 /*
5154 * Number constant.
5155 */
5156 case '0':
5157 case '1':
5158 case '2':
5159 case '3':
5160 case '4':
5161 case '5':
5162 case '6':
5163 case '7':
5164 case '8':
5165 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005166 {
5167#ifdef FEAT_FLOAT
5168 char_u *p = skipdigits(*arg + 1);
5169 int get_float = FALSE;
5170
5171 /* We accept a float when the format matches
5172 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005173 * strict to avoid backwards compatibility problems.
5174 * Don't look for a float after the "." operator, so that
5175 * ":let vers = 1.2.3" doesn't fail. */
5176 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005178 get_float = TRUE;
5179 p = skipdigits(p + 2);
5180 if (*p == 'e' || *p == 'E')
5181 {
5182 ++p;
5183 if (*p == '-' || *p == '+')
5184 ++p;
5185 if (!vim_isdigit(*p))
5186 get_float = FALSE;
5187 else
5188 p = skipdigits(p + 1);
5189 }
5190 if (ASCII_ISALPHA(*p) || *p == '.')
5191 get_float = FALSE;
5192 }
5193 if (get_float)
5194 {
5195 float_T f;
5196
5197 *arg += string2float(*arg, &f);
5198 if (evaluate)
5199 {
5200 rettv->v_type = VAR_FLOAT;
5201 rettv->vval.v_float = f;
5202 }
5203 }
5204 else
5205#endif
5206 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005207 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005208 *arg += len;
5209 if (evaluate)
5210 {
5211 rettv->v_type = VAR_NUMBER;
5212 rettv->vval.v_number = n;
5213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 /*
5219 * String constant: "string".
5220 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 break;
5223
5224 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228 break;
5229
5230 /*
5231 * List: [expr, expr]
5232 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005233 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 break;
5235
5236 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 * Dictionary: {key: val, key: val}
5238 */
5239 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5240 break;
5241
5242 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005243 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005245 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 break;
5247
5248 /*
5249 * Environment variable: $VAR.
5250 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005251 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 break;
5253
5254 /*
5255 * Register contents: @r.
5256 */
5257 case '@': ++*arg;
5258 if (evaluate)
5259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005260 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005261 rettv->vval.v_string = get_reg_contents(**arg,
5262 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264 if (**arg != NUL)
5265 ++*arg;
5266 break;
5267
5268 /*
5269 * nested expression: (expression).
5270 */
5271 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005272 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 if (**arg == ')')
5274 ++*arg;
5275 else if (ret == OK)
5276 {
5277 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 ret = FAIL;
5280 }
5281 break;
5282
Bram Moolenaar8c711452005-01-14 21:53:12 +00005283 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 break;
5285 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005286
5287 if (ret == NOTDONE)
5288 {
5289 /*
5290 * Must be a variable or function name.
5291 * Can also be a curly-braces kind of name: {expr}.
5292 */
5293 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005294 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 if (alias != NULL)
5296 s = alias;
5297
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005298 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 ret = FAIL;
5300 else
5301 {
5302 if (**arg == '(') /* recursive! */
5303 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005304 partial_T *partial;
5305
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 /* If "s" is the name of a variable of type VAR_FUNC
5307 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005308 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005309
5310 /* Invoke the function. */
5311 ret = get_func_tv(s, len, rettv, arg,
5312 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005313 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005314
5315 /* If evaluate is FALSE rettv->v_type was not set in
5316 * get_func_tv, but it's needed in handle_subscript() to parse
5317 * what follows. So set it here. */
5318 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5319 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02005320 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01005321 rettv->v_type = VAR_FUNC;
5322 }
5323
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324 /* Stop the expression evaluation when immediately
5325 * aborting on error, or when an interrupt occurred or
5326 * an exception was thrown but not caught. */
5327 if (aborting())
5328 {
5329 if (ret == OK)
5330 clear_tv(rettv);
5331 ret = FAIL;
5332 }
5333 }
5334 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005335 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005336 else
5337 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005338 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005339 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005340 }
5341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 *arg = skipwhite(*arg);
5343
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005344 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5345 * expr(expr). */
5346 if (ret == OK)
5347 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
5349 /*
5350 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5351 */
5352 if (ret == OK && evaluate && end_leader > start_leader)
5353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005354 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005355 int val = 0;
5356#ifdef FEAT_FLOAT
5357 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005359 if (rettv->v_type == VAR_FLOAT)
5360 f = rettv->vval.v_float;
5361 else
5362#endif
5363 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005364 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005366 clear_tv(rettv);
5367 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005369 else
5370 {
5371 while (end_leader > start_leader)
5372 {
5373 --end_leader;
5374 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005375 {
5376#ifdef FEAT_FLOAT
5377 if (rettv->v_type == VAR_FLOAT)
5378 f = !f;
5379 else
5380#endif
5381 val = !val;
5382 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005383 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005384 {
5385#ifdef FEAT_FLOAT
5386 if (rettv->v_type == VAR_FLOAT)
5387 f = -f;
5388 else
5389#endif
5390 val = -val;
5391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005392 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005393#ifdef FEAT_FLOAT
5394 if (rettv->v_type == VAR_FLOAT)
5395 {
5396 clear_tv(rettv);
5397 rettv->vval.v_float = f;
5398 }
5399 else
5400#endif
5401 {
5402 clear_tv(rettv);
5403 rettv->v_type = VAR_NUMBER;
5404 rettv->vval.v_number = val;
5405 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 }
5408
5409 return ret;
5410}
5411
5412/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005413 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5414 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5416 */
5417 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005418eval_index(
5419 char_u **arg,
5420 typval_T *rettv,
5421 int evaluate,
5422 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423{
5424 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 long len = -1;
5428 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431
Bram Moolenaara03f2332016-02-06 18:09:59 +01005432 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005434 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005435 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005436 if (verbose)
5437 EMSG(_("E695: Cannot index a Funcref"));
5438 return FAIL;
5439 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005440#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005441 if (verbose)
5442 EMSG(_(e_float_as_string));
5443 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005444#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005445 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005446 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005447 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005448 if (verbose)
5449 EMSG(_("E909: Cannot index a special variable"));
5450 return FAIL;
5451 case VAR_UNKNOWN:
5452 if (evaluate)
5453 return FAIL;
5454 /* FALLTHROUGH */
5455
5456 case VAR_STRING:
5457 case VAR_NUMBER:
5458 case VAR_LIST:
5459 case VAR_DICT:
5460 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005461 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005463 init_tv(&var1);
5464 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005465 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 /*
5468 * dict.name
5469 */
5470 key = *arg + 1;
5471 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5472 ;
5473 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 }
5477 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005478 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005479 /*
5480 * something[idx]
5481 *
5482 * Get the (first) variable from inside the [].
5483 */
5484 *arg = skipwhite(*arg + 1);
5485 if (**arg == ':')
5486 empty1 = TRUE;
5487 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5488 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005489 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5490 {
5491 /* not a number or string */
5492 clear_tv(&var1);
5493 return FAIL;
5494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495
5496 /*
5497 * Get the second variable from inside the [:].
5498 */
5499 if (**arg == ':')
5500 {
5501 range = TRUE;
5502 *arg = skipwhite(*arg + 1);
5503 if (**arg == ']')
5504 empty2 = TRUE;
5505 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005507 if (!empty1)
5508 clear_tv(&var1);
5509 return FAIL;
5510 }
5511 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5512 {
5513 /* not a number or string */
5514 if (!empty1)
5515 clear_tv(&var1);
5516 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 return FAIL;
5518 }
5519 }
5520
5521 /* Check for the ']'. */
5522 if (**arg != ']')
5523 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005524 if (verbose)
5525 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005526 clear_tv(&var1);
5527 if (range)
5528 clear_tv(&var2);
5529 return FAIL;
5530 }
5531 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 }
5533
5534 if (evaluate)
5535 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 n1 = 0;
5537 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005539 n1 = get_tv_number(&var1);
5540 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005541 }
5542 if (range)
5543 {
5544 if (empty2)
5545 n2 = -1;
5546 else
5547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 n2 = get_tv_number(&var2);
5549 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005550 }
5551 }
5552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005553 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005555 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005556 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005557 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005558 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005559 case VAR_SPECIAL:
5560 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005561 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005562 break; /* not evaluating, skipping over subscript */
5563
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 case VAR_NUMBER:
5565 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 len = (long)STRLEN(s);
5568 if (range)
5569 {
5570 /* The resulting variable is a substring. If the indexes
5571 * are out of range the result is empty. */
5572 if (n1 < 0)
5573 {
5574 n1 = len + n1;
5575 if (n1 < 0)
5576 n1 = 0;
5577 }
5578 if (n2 < 0)
5579 n2 = len + n2;
5580 else if (n2 >= len)
5581 n2 = len;
5582 if (n1 >= len || n2 < 0 || n1 > n2)
5583 s = NULL;
5584 else
5585 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5586 }
5587 else
5588 {
5589 /* The resulting variable is a string of a single
5590 * character. If the index is too big or negative the
5591 * result is empty. */
5592 if (n1 >= len || n1 < 0)
5593 s = NULL;
5594 else
5595 s = vim_strnsave(s + n1, 1);
5596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 clear_tv(rettv);
5598 rettv->v_type = VAR_STRING;
5599 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005600 break;
5601
5602 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 if (n1 < 0)
5605 n1 = len + n1;
5606 if (!empty1 && (n1 < 0 || n1 >= len))
5607 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005608 /* For a range we allow invalid values and return an empty
5609 * list. A list index out of range is an error. */
5610 if (!range)
5611 {
5612 if (verbose)
5613 EMSGN(_(e_listidx), n1);
5614 return FAIL;
5615 }
5616 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005617 }
5618 if (range)
5619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 list_T *l;
5621 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622
5623 if (n2 < 0)
5624 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005625 else if (n2 >= len)
5626 n2 = len - 1;
5627 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005628 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005629 l = list_alloc();
5630 if (l == NULL)
5631 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005632 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005633 n1 <= n2; ++n1)
5634 {
5635 if (list_append_tv(l, &item->li_tv) == FAIL)
5636 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005637 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 return FAIL;
5639 }
5640 item = item->li_next;
5641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005642 clear_tv(rettv);
5643 rettv->v_type = VAR_LIST;
5644 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005645 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005646 }
5647 else
5648 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005649 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 clear_tv(rettv);
5651 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005652 }
5653 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654
5655 case VAR_DICT:
5656 if (range)
5657 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005658 if (verbose)
5659 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005660 if (len == -1)
5661 clear_tv(&var1);
5662 return FAIL;
5663 }
5664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005665 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005666
5667 if (len == -1)
5668 {
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02005669 key = get_tv_string_chk(&var1);
5670 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005671 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005672 clear_tv(&var1);
5673 return FAIL;
5674 }
5675 }
5676
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005677 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005678
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005679 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005680 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005681 if (len == -1)
5682 clear_tv(&var1);
5683 if (item == NULL)
5684 return FAIL;
5685
5686 copy_tv(&item->di_tv, &var1);
5687 clear_tv(rettv);
5688 *rettv = var1;
5689 }
5690 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 }
5692 }
5693
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 return OK;
5695}
5696
5697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 * Get an option value.
5699 * "arg" points to the '&' or '+' before the option name.
5700 * "arg" is advanced to character after the option name.
5701 * Return OK or FAIL.
5702 */
5703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704get_option_tv(
5705 char_u **arg,
5706 typval_T *rettv, /* when NULL, only check if option exists */
5707 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 char_u *option_end;
5710 long numval;
5711 char_u *stringval;
5712 int opt_type;
5713 int c;
5714 int working = (**arg == '+'); /* has("+option") */
5715 int ret = OK;
5716 int opt_flags;
5717
5718 /*
5719 * Isolate the option name and find its value.
5720 */
5721 option_end = find_option_end(arg, &opt_flags);
5722 if (option_end == NULL)
5723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005724 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 EMSG2(_("E112: Option name missing: %s"), *arg);
5726 return FAIL;
5727 }
5728
5729 if (!evaluate)
5730 {
5731 *arg = option_end;
5732 return OK;
5733 }
5734
5735 c = *option_end;
5736 *option_end = NUL;
5737 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005738 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739
5740 if (opt_type == -3) /* invalid name */
5741 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005742 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 EMSG2(_("E113: Unknown option: %s"), *arg);
5744 ret = FAIL;
5745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005746 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (opt_type == -2) /* hidden string option */
5749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005750 rettv->v_type = VAR_STRING;
5751 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
5753 else if (opt_type == -1) /* hidden number option */
5754 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005755 rettv->v_type = VAR_NUMBER;
5756 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 }
5758 else if (opt_type == 1) /* number option */
5759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005760 rettv->v_type = VAR_NUMBER;
5761 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 }
5763 else /* string option */
5764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 rettv->v_type = VAR_STRING;
5766 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
5768 }
5769 else if (working && (opt_type == -2 || opt_type == -1))
5770 ret = FAIL;
5771
5772 *option_end = c; /* put back for error messages */
5773 *arg = option_end;
5774
5775 return ret;
5776}
5777
5778/*
5779 * Allocate a variable for a string constant.
5780 * Return OK or FAIL.
5781 */
5782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005783get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 char_u *p;
5786 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 int extra = 0;
5788
5789 /*
5790 * Find the end of the string, skipping backslashed characters.
5791 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 if (*p == '\\' && p[1] != NUL)
5795 {
5796 ++p;
5797 /* A "\<x>" form occupies at least 4 characters, and produces up
5798 * to 6 characters: reserve space for 2 extra */
5799 if (*p == '<')
5800 extra += 2;
5801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
5803
5804 if (*p != '"')
5805 {
5806 EMSG2(_("E114: Missing quote: %s"), *arg);
5807 return FAIL;
5808 }
5809
5810 /* If only parsing, set *arg and return here */
5811 if (!evaluate)
5812 {
5813 *arg = p + 1;
5814 return OK;
5815 }
5816
5817 /*
5818 * Copy the string into allocated memory, handling backslashed
5819 * characters.
5820 */
5821 name = alloc((unsigned)(p - *arg + extra));
5822 if (name == NULL)
5823 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 rettv->v_type = VAR_STRING;
5825 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {
5829 if (*p == '\\')
5830 {
5831 switch (*++p)
5832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 case 'b': *name++ = BS; ++p; break;
5834 case 'e': *name++ = ESC; ++p; break;
5835 case 'f': *name++ = FF; ++p; break;
5836 case 'n': *name++ = NL; ++p; break;
5837 case 'r': *name++ = CAR; ++p; break;
5838 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 case 'X': /* hex: "\x1", "\x12" */
5841 case 'x':
5842 case 'u': /* Unicode: "\u0023" */
5843 case 'U':
5844 if (vim_isxdigit(p[1]))
5845 {
5846 int n, nr;
5847 int c = toupper(*p);
5848
5849 if (c == 'X')
5850 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005851 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005853 else
5854 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 nr = 0;
5856 while (--n >= 0 && vim_isxdigit(p[1]))
5857 {
5858 ++p;
5859 nr = (nr << 4) + hex2nr(*p);
5860 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862#ifdef FEAT_MBYTE
5863 /* For "\u" store the number according to
5864 * 'encoding'. */
5865 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 else
5868#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 break;
5872
5873 /* octal: "\1", "\12", "\123" */
5874 case '0':
5875 case '1':
5876 case '2':
5877 case '3':
5878 case '4':
5879 case '5':
5880 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 case '7': *name = *p++ - '0';
5882 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 *name = (*name << 3) + *p++ - '0';
5885 if (*p >= '0' && *p <= '7')
5886 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005888 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 break;
5890
5891 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 if (extra != 0)
5894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 break;
5897 }
5898 /* FALLTHROUGH */
5899
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 break;
5902 }
5903 }
5904 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005908 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 *arg = p + 1;
5910
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 return OK;
5912}
5913
5914/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 * Return OK or FAIL.
5917 */
5918 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005922 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 int reduce = 0;
5924
5925 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 */
5928 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5929 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005930 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005932 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005933 break;
5934 ++reduce;
5935 ++p;
5936 }
5937 }
5938
Bram Moolenaar8c711452005-01-14 21:53:12 +00005939 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005940 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005941 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005942 return FAIL;
5943 }
5944
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005946 if (!evaluate)
5947 {
5948 *arg = p + 1;
5949 return OK;
5950 }
5951
5952 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005954 */
5955 str = alloc((unsigned)((p - *arg) - reduce));
5956 if (str == NULL)
5957 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005958 rettv->v_type = VAR_STRING;
5959 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005960
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005962 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005963 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005964 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005965 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005966 break;
5967 ++p;
5968 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005969 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005970 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972 *arg = p + 1;
5973
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005974 return OK;
5975}
5976
Bram Moolenaarddecc252016-04-06 22:59:37 +02005977 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005978partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005979{
5980 int i;
5981
5982 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005983 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005984 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005985 dict_unref(pt->pt_dict);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005986 func_unref(pt->pt_name);
5987 vim_free(pt->pt_name);
5988 vim_free(pt);
5989}
5990
5991/*
5992 * Unreference a closure: decrement the reference count and free it when it
5993 * becomes zero.
5994 */
5995 void
5996partial_unref(partial_T *pt)
5997{
5998 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005999 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02006000}
6001
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006002/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 * Allocate a variable for a List and fill it from "*arg".
6004 * Return OK or FAIL.
6005 */
6006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006007get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006008{
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 list_T *l = NULL;
6010 typval_T tv;
6011 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012
6013 if (evaluate)
6014 {
6015 l = list_alloc();
6016 if (l == NULL)
6017 return FAIL;
6018 }
6019
6020 *arg = skipwhite(*arg + 1);
6021 while (**arg != ']' && **arg != NUL)
6022 {
6023 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6024 goto failret;
6025 if (evaluate)
6026 {
6027 item = listitem_alloc();
6028 if (item != NULL)
6029 {
6030 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006031 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 list_append(l, item);
6033 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006034 else
6035 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 }
6037
6038 if (**arg == ']')
6039 break;
6040 if (**arg != ',')
6041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 goto failret;
6044 }
6045 *arg = skipwhite(*arg + 1);
6046 }
6047
6048 if (**arg != ']')
6049 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006050 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051failret:
6052 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006053 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 return FAIL;
6055 }
6056
6057 *arg = skipwhite(*arg + 1);
6058 if (evaluate)
6059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006060 rettv->v_type = VAR_LIST;
6061 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 ++l->lv_refcount;
6063 }
6064
6065 return OK;
6066}
6067
6068/*
6069 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006070 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006072 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006075 list_T *l;
6076
6077 l = (list_T *)alloc_clear(sizeof(list_T));
6078 if (l != NULL)
6079 {
6080 /* Prepend the list to the list of lists for garbage collection. */
6081 if (first_list != NULL)
6082 first_list->lv_used_prev = l;
6083 l->lv_used_prev = NULL;
6084 l->lv_used_next = first_list;
6085 first_list = l;
6086 }
6087 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088}
6089
6090/*
Bram Moolenaar517ffbe2016-04-20 14:59:29 +02006091 * Allocate an empty list for a return value, with reference count set.
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006092 * Returns OK or FAIL.
6093 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006094 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006096{
6097 list_T *l = list_alloc();
6098
6099 if (l == NULL)
6100 return FAIL;
6101
6102 rettv->vval.v_list = l;
6103 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006104 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006105 ++l->lv_refcount;
6106 return OK;
6107}
6108
6109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110 * Unreference a list: decrement the reference count and free it when it
6111 * becomes zero.
6112 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006114list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006116 if (l != NULL && --l->lv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006117 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118}
6119
6120/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006121 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122 * Ignores the reference count.
6123 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006124 static void
6125list_free_contents(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006126{
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006128
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006129 for (item = l->lv_first; item != NULL; item = l->lv_first)
6130 {
6131 /* Remove the item before deleting it. */
6132 l->lv_first = item->li_next;
6133 clear_tv(&item->li_tv);
6134 vim_free(item);
6135 }
6136}
6137
6138 static void
6139list_free_list(list_T *l)
6140{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006141 /* Remove the list from the list of lists for garbage collection. */
6142 if (l->lv_used_prev == NULL)
6143 first_list = l->lv_used_next;
6144 else
6145 l->lv_used_prev->lv_used_next = l->lv_used_next;
6146 if (l->lv_used_next != NULL)
6147 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6148
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 vim_free(l);
6150}
6151
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02006152 void
6153list_free(list_T *l)
6154{
6155 if (!in_free_unref_items)
6156 {
6157 list_free_contents(l);
6158 list_free_list(l);
6159 }
6160}
6161
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162/*
6163 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006164 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006166 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006167listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006168{
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006170}
6171
6172/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006173 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006176listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006177{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006178 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006179 vim_free(item);
6180}
6181
6182/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006183 * Remove a list item from a List and free it. Also clears the value.
6184 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006186listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006187{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006188 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006189 listitem_free(item);
6190}
6191
6192/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 * Get the number of items in a list.
6194 */
6195 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006196list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006198 if (l == NULL)
6199 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006200 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006201}
6202
6203/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 * Return TRUE when two lists have exactly the same values.
6205 */
6206 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006207list_equal(
6208 list_T *l1,
6209 list_T *l2,
6210 int ic, /* ignore case for strings */
6211 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212{
Bram Moolenaar33570922005-01-25 22:26:29 +00006213 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006214
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006215 if (l1 == NULL || l2 == NULL)
6216 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006217 if (l1 == l2)
6218 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006219 if (list_len(l1) != list_len(l2))
6220 return FALSE;
6221
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006222 for (item1 = l1->lv_first, item2 = l2->lv_first;
6223 item1 != NULL && item2 != NULL;
6224 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006225 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006226 return FALSE;
6227 return item1 == NULL && item2 == NULL;
6228}
6229
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006230/*
6231 * Return the dictitem that an entry in a hashtable points to.
6232 */
6233 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006234dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006235{
6236 return HI2DI(hi);
6237}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006238
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006239/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006240 * Return TRUE when two dictionaries have exactly the same key/values.
6241 */
6242 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006243dict_equal(
6244 dict_T *d1,
6245 dict_T *d2,
6246 int ic, /* ignore case for strings */
6247 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006248{
Bram Moolenaar33570922005-01-25 22:26:29 +00006249 hashitem_T *hi;
6250 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006251 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02006253 if (d1 == NULL && d2 == NULL)
6254 return TRUE;
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006255 if (d1 == NULL || d2 == NULL)
6256 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006257 if (d1 == d2)
6258 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006259 if (dict_len(d1) != dict_len(d2))
6260 return FALSE;
6261
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006262 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006263 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006264 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006265 if (!HASHITEM_EMPTY(hi))
6266 {
6267 item2 = dict_find(d2, hi->hi_key, -1);
6268 if (item2 == NULL)
6269 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006270 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 return FALSE;
6272 --todo;
6273 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006274 }
6275 return TRUE;
6276}
6277
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006278static int tv_equal_recurse_limit;
6279
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006280 static int
6281func_equal(
6282 typval_T *tv1,
6283 typval_T *tv2,
6284 int ic) /* ignore case */
6285{
6286 char_u *s1, *s2;
6287 dict_T *d1, *d2;
6288 int a1, a2;
6289 int i;
6290
6291 /* empty and NULL function name considered the same */
6292 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6293 : tv1->vval.v_partial->pt_name;
6294 if (s1 != NULL && *s1 == NUL)
6295 s1 = NULL;
6296 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6297 : tv2->vval.v_partial->pt_name;
6298 if (s2 != NULL && *s2 == NUL)
6299 s2 = NULL;
6300 if (s1 == NULL || s2 == NULL)
6301 {
6302 if (s1 != s2)
6303 return FALSE;
6304 }
6305 else if (STRCMP(s1, s2) != 0)
6306 return FALSE;
6307
6308 /* empty dict and NULL dict is different */
6309 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
6310 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
6311 if (d1 == NULL || d2 == NULL)
6312 {
6313 if (d1 != d2)
6314 return FALSE;
6315 }
6316 else if (!dict_equal(d1, d2, ic, TRUE))
6317 return FALSE;
6318
6319 /* empty list and no list considered the same */
6320 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
6321 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
6322 if (a1 != a2)
6323 return FALSE;
6324 for (i = 0; i < a1; ++i)
6325 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
6326 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
6327 return FALSE;
6328
6329 return TRUE;
6330}
6331
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006332/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006333 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006334 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006335 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006336 */
6337 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338tv_equal(
6339 typval_T *tv1,
6340 typval_T *tv2,
6341 int ic, /* ignore case */
6342 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006343{
6344 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006345 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006346 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006347 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006348
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006349 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006350 * recursiveness to a limit. We guess they are equal then.
6351 * A fixed limit has the problem of still taking an awful long time.
6352 * Reduce the limit every time running into it. That should work fine for
6353 * deeply linked structures that are not recursively linked and catch
6354 * recursiveness quickly. */
6355 if (!recursive)
6356 tv_equal_recurse_limit = 1000;
6357 if (recursive_cnt >= tv_equal_recurse_limit)
6358 {
6359 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006360 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006361 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006362
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02006363 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6364 if ((tv1->v_type == VAR_FUNC
6365 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6366 && (tv2->v_type == VAR_FUNC
6367 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6368 {
6369 ++recursive_cnt;
6370 r = func_equal(tv1, tv2, ic);
6371 --recursive_cnt;
6372 return r;
6373 }
6374
6375 if (tv1->v_type != tv2->v_type)
6376 return FALSE;
6377
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006378 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006379 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006380 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006381 ++recursive_cnt;
6382 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6383 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006384 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006385
6386 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006387 ++recursive_cnt;
6388 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6389 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006390 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006391
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006392 case VAR_NUMBER:
6393 return tv1->vval.v_number == tv2->vval.v_number;
6394
6395 case VAR_STRING:
6396 s1 = get_tv_string_buf(tv1, buf1);
6397 s2 = get_tv_string_buf(tv2, buf2);
6398 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006399
6400 case VAR_SPECIAL:
6401 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006402
6403 case VAR_FLOAT:
6404#ifdef FEAT_FLOAT
6405 return tv1->vval.v_float == tv2->vval.v_float;
6406#endif
6407 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006408#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006409 return tv1->vval.v_job == tv2->vval.v_job;
6410#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006411 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006412#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006413 return tv1->vval.v_channel == tv2->vval.v_channel;
6414#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006415 case VAR_FUNC:
6416 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006417 case VAR_UNKNOWN:
6418 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006419 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006420
Bram Moolenaara03f2332016-02-06 18:09:59 +01006421 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6422 * does not equal anything, not even itself. */
6423 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006424}
6425
6426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 * Locate item with index "n" in list "l" and return it.
6428 * A negative index is counted from the end; -1 is the last item.
6429 * Returns NULL when "n" is out of range.
6430 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006431 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006432list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433{
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 long idx;
6436
6437 if (l == NULL)
6438 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006439
6440 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006441 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006442 n = l->lv_len + n;
6443
6444 /* Check for index out of range. */
6445 if (n < 0 || n >= l->lv_len)
6446 return NULL;
6447
6448 /* When there is a cached index may start search from there. */
6449 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006450 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006451 if (n < l->lv_idx / 2)
6452 {
6453 /* closest to the start of the list */
6454 item = l->lv_first;
6455 idx = 0;
6456 }
6457 else if (n > (l->lv_idx + l->lv_len) / 2)
6458 {
6459 /* closest to the end of the list */
6460 item = l->lv_last;
6461 idx = l->lv_len - 1;
6462 }
6463 else
6464 {
6465 /* closest to the cached index */
6466 item = l->lv_idx_item;
6467 idx = l->lv_idx;
6468 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006469 }
6470 else
6471 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006472 if (n < l->lv_len / 2)
6473 {
6474 /* closest to the start of the list */
6475 item = l->lv_first;
6476 idx = 0;
6477 }
6478 else
6479 {
6480 /* closest to the end of the list */
6481 item = l->lv_last;
6482 idx = l->lv_len - 1;
6483 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006484 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006485
6486 while (n > idx)
6487 {
6488 /* search forward */
6489 item = item->li_next;
6490 ++idx;
6491 }
6492 while (n < idx)
6493 {
6494 /* search backward */
6495 item = item->li_prev;
6496 --idx;
6497 }
6498
6499 /* cache the used index */
6500 l->lv_idx = idx;
6501 l->lv_idx_item = item;
6502
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503 return item;
6504}
6505
6506/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006507 * Get list item "l[idx]" as a number.
6508 */
6509 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_find_nr(
6511 list_T *l,
6512 long idx,
6513 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006514{
6515 listitem_T *li;
6516
6517 li = list_find(l, idx);
6518 if (li == NULL)
6519 {
6520 if (errorp != NULL)
6521 *errorp = TRUE;
6522 return -1L;
6523 }
6524 return get_tv_number_chk(&li->li_tv, errorp);
6525}
6526
6527/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006528 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6529 */
6530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006531list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006532{
6533 listitem_T *li;
6534
6535 li = list_find(l, idx - 1);
6536 if (li == NULL)
6537 {
6538 EMSGN(_(e_listidx), idx);
6539 return NULL;
6540 }
6541 return get_tv_string(&li->li_tv);
6542}
6543
6544/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006545 * Locate "item" list "l" and return its index.
6546 * Returns -1 when "item" is not in the list.
6547 */
6548 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006549list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006550{
6551 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006553
6554 if (l == NULL)
6555 return -1;
6556 idx = 0;
6557 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6558 ++idx;
6559 if (li == NULL)
6560 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006561 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006562}
6563
6564/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 * Append item "item" to the end of list "l".
6566 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006568list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006569{
6570 if (l->lv_last == NULL)
6571 {
6572 /* empty list */
6573 l->lv_first = item;
6574 l->lv_last = item;
6575 item->li_prev = NULL;
6576 }
6577 else
6578 {
6579 l->lv_last->li_next = item;
6580 item->li_prev = l->lv_last;
6581 l->lv_last = item;
6582 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006583 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006584 item->li_next = NULL;
6585}
6586
6587/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006594 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
Bram Moolenaar05159a02005-02-26 23:04:13 +00006596 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006598 copy_tv(tv, &li->li_tv);
6599 list_append(l, li);
6600 return OK;
6601}
6602
6603/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006604 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006605 * Return FAIL when out of memory.
6606 */
6607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006608list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006609{
6610 listitem_T *li = listitem_alloc();
6611
6612 if (li == NULL)
6613 return FAIL;
6614 li->li_tv.v_type = VAR_DICT;
6615 li->li_tv.v_lock = 0;
6616 li->li_tv.vval.v_dict = dict;
6617 list_append(list, li);
6618 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 return OK;
6620}
6621
6622/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006623 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006624 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006625 * Returns FAIL when out of memory.
6626 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006628list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006629{
6630 listitem_T *li = listitem_alloc();
6631
6632 if (li == NULL)
6633 return FAIL;
6634 list_append(l, li);
6635 li->li_tv.v_type = VAR_STRING;
6636 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006637 if (str == NULL)
6638 li->li_tv.vval.v_string = NULL;
6639 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006640 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006641 return FAIL;
6642 return OK;
6643}
6644
6645/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006646 * Append "n" to list "l".
6647 * Returns FAIL when out of memory.
6648 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006649 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006650list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006651{
6652 listitem_T *li;
6653
6654 li = listitem_alloc();
6655 if (li == NULL)
6656 return FAIL;
6657 li->li_tv.v_type = VAR_NUMBER;
6658 li->li_tv.v_lock = 0;
6659 li->li_tv.vval.v_number = n;
6660 list_append(l, li);
6661 return OK;
6662}
6663
6664/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006665 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666 * If "item" is NULL append at the end.
6667 * Return FAIL when out of memory.
6668 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006669 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006670list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006671{
Bram Moolenaar33570922005-01-25 22:26:29 +00006672 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673
6674 if (ni == NULL)
6675 return FAIL;
6676 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006677 list_insert(l, ni, item);
6678 return OK;
6679}
6680
6681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006682list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006683{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006684 if (item == NULL)
6685 /* Append new item at end of list. */
6686 list_append(l, ni);
6687 else
6688 {
6689 /* Insert new item before existing item. */
6690 ni->li_prev = item->li_prev;
6691 ni->li_next = item;
6692 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006694 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006695 ++l->lv_idx;
6696 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006697 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006698 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006699 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006700 l->lv_idx_item = NULL;
6701 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006702 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006703 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006704 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006705}
6706
6707/*
6708 * Extend "l1" with "l2".
6709 * If "bef" is NULL append at the end, otherwise insert before this item.
6710 * Returns FAIL when out of memory.
6711 */
6712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006713list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006714{
Bram Moolenaar33570922005-01-25 22:26:29 +00006715 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006716 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006717
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006718 /* We also quit the loop when we have inserted the original item count of
6719 * the list, avoid a hang when we extend a list with itself. */
6720 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006721 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6722 return FAIL;
6723 return OK;
6724}
6725
6726/*
6727 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6728 * Return FAIL when out of memory.
6729 */
6730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006731list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006732{
Bram Moolenaar33570922005-01-25 22:26:29 +00006733 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006734
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006735 if (l1 == NULL || l2 == NULL)
6736 return FAIL;
6737
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006738 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006739 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006740 if (l == NULL)
6741 return FAIL;
6742 tv->v_type = VAR_LIST;
6743 tv->vval.v_list = l;
6744
6745 /* append all items from the second list */
6746 return list_extend(l, l2, NULL);
6747}
6748
6749/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006750 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006751 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006752 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006753 * Returns NULL when out of memory.
6754 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006755 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006756list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006757{
Bram Moolenaar33570922005-01-25 22:26:29 +00006758 list_T *copy;
6759 listitem_T *item;
6760 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761
6762 if (orig == NULL)
6763 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764
6765 copy = list_alloc();
6766 if (copy != NULL)
6767 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006768 if (copyID != 0)
6769 {
6770 /* Do this before adding the items, because one of the items may
6771 * refer back to this list. */
6772 orig->lv_copyID = copyID;
6773 orig->lv_copylist = copy;
6774 }
6775 for (item = orig->lv_first; item != NULL && !got_int;
6776 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006777 {
6778 ni = listitem_alloc();
6779 if (ni == NULL)
6780 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006781 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006782 {
6783 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6784 {
6785 vim_free(ni);
6786 break;
6787 }
6788 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006790 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006791 list_append(copy, ni);
6792 }
6793 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006794 if (item != NULL)
6795 {
6796 list_unref(copy);
6797 copy = NULL;
6798 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006799 }
6800
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006801 return copy;
6802}
6803
6804/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006805 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006806 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006807 * This used to be called list_remove, but that conflicts with a Sun header
6808 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006809 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006812{
Bram Moolenaar33570922005-01-25 22:26:29 +00006813 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006814
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006815 /* notify watchers */
6816 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006817 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006818 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006819 list_fix_watch(l, ip);
6820 if (ip == item2)
6821 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006822 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006823
6824 if (item2->li_next == NULL)
6825 l->lv_last = item->li_prev;
6826 else
6827 item2->li_next->li_prev = item->li_prev;
6828 if (item->li_prev == NULL)
6829 l->lv_first = item2->li_next;
6830 else
6831 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006832 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833}
6834
6835/*
6836 * Return an allocated string with the string representation of a list.
6837 * May return NULL.
6838 */
6839 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006840list2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006841{
6842 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006843
6844 if (tv->vval.v_list == NULL)
6845 return NULL;
6846 ga_init2(&ga, (int)sizeof(char), 80);
6847 ga_append(&ga, '[');
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006848 if (list_join(&ga, tv->vval.v_list, (char_u *)", ",
6849 FALSE, restore_copyID, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006850 {
6851 vim_free(ga.ga_data);
6852 return NULL;
6853 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006854 ga_append(&ga, ']');
6855 ga_append(&ga, NUL);
6856 return (char_u *)ga.ga_data;
6857}
6858
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006859typedef struct join_S {
6860 char_u *s;
6861 char_u *tofree;
6862} join_T;
6863
6864 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006865list_join_inner(
6866 garray_T *gap, /* to store the result in */
6867 list_T *l,
6868 char_u *sep,
6869 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006870 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006871 int copyID,
6872 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006873{
6874 int i;
6875 join_T *p;
6876 int len;
6877 int sumlen = 0;
6878 int first = TRUE;
6879 char_u *tofree;
6880 char_u numbuf[NUMBUFLEN];
6881 listitem_T *item;
6882 char_u *s;
6883
6884 /* Stringify each item in the list. */
6885 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6886 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006887 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID,
6888 echo_style, restore_copyID, FALSE);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006889 if (s == NULL)
6890 return FAIL;
6891
6892 len = (int)STRLEN(s);
6893 sumlen += len;
6894
Bram Moolenaarcde88542015-08-11 19:14:00 +02006895 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006896 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6897 if (tofree != NULL || s != numbuf)
6898 {
6899 p->s = s;
6900 p->tofree = tofree;
6901 }
6902 else
6903 {
6904 p->s = vim_strnsave(s, len);
6905 p->tofree = p->s;
6906 }
6907
6908 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006909 if (did_echo_string_emsg) /* recursion error, bail out */
6910 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006911 }
6912
6913 /* Allocate result buffer with its total size, avoid re-allocation and
6914 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6915 if (join_gap->ga_len >= 2)
6916 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6917 if (ga_grow(gap, sumlen + 2) == FAIL)
6918 return FAIL;
6919
6920 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6921 {
6922 if (first)
6923 first = FALSE;
6924 else
6925 ga_concat(gap, sep);
6926 p = ((join_T *)join_gap->ga_data) + i;
6927
6928 if (p->s != NULL)
6929 ga_concat(gap, p->s);
6930 line_breakcheck();
6931 }
6932
6933 return OK;
6934}
6935
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006936/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006937 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006938 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006939 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006940 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006942list_join(
6943 garray_T *gap,
6944 list_T *l,
6945 char_u *sep,
6946 int echo_style,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006947 int restore_copyID,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006948 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006949{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006950 garray_T join_ga;
6951 int retval;
6952 join_T *p;
6953 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006954
Bram Moolenaard39a7512015-04-16 22:51:22 +02006955 if (l->lv_len < 1)
6956 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006957 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006958 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
6959 copyID, &join_ga);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006960
6961 /* Dispose each item in join_ga. */
6962 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006963 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006964 p = (join_T *)join_ga.ga_data;
6965 for (i = 0; i < join_ga.ga_len; ++i)
6966 {
6967 vim_free(p->tofree);
6968 ++p;
6969 }
6970 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006971 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006972
6973 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006974}
6975
6976/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006977 * Return the next (unique) copy ID.
6978 * Used for serializing nested structures.
6979 */
6980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006981get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006982{
6983 current_copyID += COPYID_INC;
6984 return current_copyID;
6985}
6986
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02006987/* Used by get_func_tv() */
6988static garray_T funcargs = GA_EMPTY;
6989
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006990/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 * Garbage collection for lists and dictionaries.
6992 *
6993 * We use reference counts to be able to free most items right away when they
6994 * are no longer used. But for composite items it's possible that it becomes
6995 * unused while the reference count is > 0: When there is a recursive
6996 * reference. Example:
6997 * :let l = [1, 2, 3]
6998 * :let d = {9: l}
6999 * :let l[1] = d
7000 *
7001 * Since this is quite unusual we handle this with garbage collection: every
7002 * once in a while find out which lists and dicts are not referenced from any
7003 * variable.
7004 *
7005 * Here is a good reference text about garbage collection (refers to Python
7006 * but it applies to all reference-counting mechanisms):
7007 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00007008 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007009
7010/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02007012 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00007014 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007016garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007017{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007019 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 buf_T *buf;
7021 win_T *wp;
7022 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007023 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01007024 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007025 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007026#ifdef FEAT_WINDOWS
7027 tabpage_T *tp;
7028#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007030 if (!testing)
7031 {
7032 /* Only do this once. */
7033 want_garbage_collect = FALSE;
7034 may_garbage_collect = FALSE;
7035 garbage_collect_at_exit = FALSE;
7036 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00007037
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007038 /* We advance by two because we add one for items referenced through
7039 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007040 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007041
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007042 /*
7043 * 1. Go through all accessible variables and mark all lists and dicts
7044 * with copyID.
7045 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007046
7047 /* Don't free variables in the previous_funccal list unless they are only
7048 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00007049 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007050 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
7051 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007052 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
7053 NULL);
7054 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
7055 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007056 }
7057
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 /* script-local variables */
7059 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007061
7062 /* buffer-local variables */
7063 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
7065 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066
7067 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007068 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
7070 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007071#ifdef FEAT_AUTOCMD
7072 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
7074 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02007075#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007077#ifdef FEAT_WINDOWS
7078 /* tabpage-local variables */
7079 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
7081 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007082#endif
7083
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007086
7087 /* function-local variables */
7088 for (fc = current_funccal; fc != NULL; fc = fc->caller)
7089 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007090 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
7091 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007092 }
7093
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007094 /* function call arguments, if v:testing is set. */
7095 for (i = 0; i < funcargs.ga_len; ++i)
7096 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
7097 copyID, NULL, NULL);
7098
Bram Moolenaard812df62008-11-09 12:46:09 +00007099 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00007101
Bram Moolenaar1dced572012-04-05 16:54:08 +02007102#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007103 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02007104#endif
7105
Bram Moolenaardb913952012-06-29 12:54:53 +02007106#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007108#endif
7109
7110#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02007112#endif
7113
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007114#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02007115 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02007116 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007117#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02007118#ifdef FEAT_NETBEANS_INTG
7119 abort = abort || set_ref_in_nb_channel(copyID);
7120#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01007121
Bram Moolenaare3188e22016-05-31 21:13:04 +02007122#ifdef FEAT_TIMERS
7123 abort = abort || set_ref_in_timer(copyID);
7124#endif
7125
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007126 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007127 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007128 /*
7129 * 2. Free lists and dictionaries that are not referenced.
7130 */
7131 did_free = free_unref_items(copyID);
7132
7133 /*
7134 * 3. Check if any funccal can be freed now.
7135 */
7136 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007137 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 if (can_free_funccal(*pfc, copyID))
7139 {
7140 fc = *pfc;
7141 *pfc = fc->caller;
7142 free_funccal(fc, TRUE);
7143 did_free = TRUE;
7144 did_free_funccal = TRUE;
7145 }
7146 else
7147 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007148 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 if (did_free_funccal)
7150 /* When a funccal was freed some more items might be garbage
7151 * collected, so run again. */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02007152 (void)garbage_collect(testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007153 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007154 else if (p_verbose > 0)
7155 {
7156 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7157 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007158
7159 return did_free;
7160}
7161
7162/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007163 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007164 */
7165 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007166free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007167{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007168 dict_T *dd, *dd_next;
7169 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007170 int did_free = FALSE;
7171
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007172 /* Let all "free" functions know that we are here. This means no
7173 * dictionaries, lists, channels or jobs are to be freed, because we will
7174 * do that here. */
7175 in_free_unref_items = TRUE;
7176
7177 /*
7178 * PASS 1: free the contents of the items. We don't free the items
7179 * themselves yet, so that it is possible to decrement refcount counters
7180 */
7181
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007182 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007183 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007184 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007185 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007186 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007188 /* Free the Dictionary and ordinary items it contains, but don't
7189 * recurse into Lists and Dictionaries, they will be in the list
7190 * of dicts or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007191 dict_free_contents(dd);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007192 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007194
7195 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007196 * Go through the list of lists and free items without the copyID.
7197 * But don't free a list that has a watcher (used in a for loop), these
7198 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007199 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007200 for (ll = first_list; ll != NULL; ll = ll->lv_used_next)
7201 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7202 && ll->lv_watch == NULL)
7203 {
7204 /* Free the List and ordinary items it contains, but don't recurse
7205 * into Lists and Dictionaries, they will be in the list of dicts
7206 * or list of lists. */
7207 list_free_contents(ll);
7208 did_free = TRUE;
7209 }
7210
7211#ifdef FEAT_JOB_CHANNEL
7212 /* Go through the list of jobs and free items without the copyID. This
7213 * must happen before doing channels, because jobs refer to channels, but
7214 * the reference from the channel to the job isn't tracked. */
7215 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
7216
7217 /* Go through the list of channels and free items without the copyID. */
7218 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
7219#endif
7220
7221 /*
7222 * PASS 2: free the items themselves.
7223 */
7224 for (dd = first_dict; dd != NULL; dd = dd_next)
7225 {
7226 dd_next = dd->dv_used_next;
7227 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
7228 dict_free_dict(dd);
7229 }
7230
7231 for (ll = first_list; ll != NULL; ll = ll_next)
Bram Moolenaare71eea82015-02-03 17:10:06 +01007232 {
7233 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007234 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7235 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007236 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007237 /* Free the List and ordinary items it contains, but don't recurse
7238 * into Lists and Dictionaries, they will be in the list of dicts
7239 * or list of lists. */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007240 list_free_list(ll);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007241 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007242 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007243
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007244#ifdef FEAT_JOB_CHANNEL
7245 /* Go through the list of jobs and free items without the copyID. This
7246 * must happen before doing channels, because jobs refer to channels, but
7247 * the reference from the channel to the job isn't tracked. */
7248 free_unused_jobs(copyID, COPYID_MASK);
7249
7250 /* Go through the list of channels and free items without the copyID. */
7251 free_unused_channels(copyID, COPYID_MASK);
7252#endif
7253
7254 in_free_unref_items = FALSE;
7255
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256 return did_free;
7257}
7258
7259/*
7260 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007261 * "list_stack" is used to add lists to be marked. Can be NULL.
7262 *
7263 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007264 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007265 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007266set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007267{
7268 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007269 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007270 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007271 hashtab_T *cur_ht;
7272 ht_stack_T *ht_stack = NULL;
7273 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007274
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007275 cur_ht = ht;
7276 for (;;)
7277 {
7278 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007279 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007280 /* Mark each item in the hashtab. If the item contains a hashtab
7281 * it is added to ht_stack, if it contains a list it is added to
7282 * list_stack. */
7283 todo = (int)cur_ht->ht_used;
7284 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7285 if (!HASHITEM_EMPTY(hi))
7286 {
7287 --todo;
7288 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7289 &ht_stack, list_stack);
7290 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007291 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007292
7293 if (ht_stack == NULL)
7294 break;
7295
7296 /* take an item from the stack */
7297 cur_ht = ht_stack->ht;
7298 tempitem = ht_stack;
7299 ht_stack = ht_stack->prev;
7300 free(tempitem);
7301 }
7302
7303 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007304}
7305
7306/*
7307 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007308 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7309 *
7310 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007311 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007312 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007313set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007314{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007315 listitem_T *li;
7316 int abort = FALSE;
7317 list_T *cur_l;
7318 list_stack_T *list_stack = NULL;
7319 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007320
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007321 cur_l = l;
7322 for (;;)
7323 {
7324 if (!abort)
7325 /* Mark each item in the list. If the item contains a hashtab
7326 * it is added to ht_stack, if it contains a list it is added to
7327 * list_stack. */
7328 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7329 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7330 ht_stack, &list_stack);
7331 if (list_stack == NULL)
7332 break;
7333
7334 /* take an item from the stack */
7335 cur_l = list_stack->list;
7336 tempitem = list_stack;
7337 list_stack = list_stack->prev;
7338 free(tempitem);
7339 }
7340
7341 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007342}
7343
7344/*
7345 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007346 * "list_stack" is used to add lists to be marked. Can be NULL.
7347 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7348 *
7349 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007350 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007352set_ref_in_item(
7353 typval_T *tv,
7354 int copyID,
7355 ht_stack_T **ht_stack,
7356 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007357{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007358 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007359
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007360 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007361 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007362 dict_T *dd = tv->vval.v_dict;
7363
Bram Moolenaara03f2332016-02-06 18:09:59 +01007364 if (dd != NULL && dd->dv_copyID != copyID)
7365 {
7366 /* Didn't see this dict yet. */
7367 dd->dv_copyID = copyID;
7368 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007369 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007370 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7371 }
7372 else
7373 {
7374 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7375 if (newitem == NULL)
7376 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007377 else
7378 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007379 newitem->ht = &dd->dv_hashtab;
7380 newitem->prev = *ht_stack;
7381 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007382 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007383 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007384 }
7385 }
7386 else if (tv->v_type == VAR_LIST)
7387 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007388 list_T *ll = tv->vval.v_list;
7389
Bram Moolenaara03f2332016-02-06 18:09:59 +01007390 if (ll != NULL && ll->lv_copyID != copyID)
7391 {
7392 /* Didn't see this list yet. */
7393 ll->lv_copyID = copyID;
7394 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007395 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007396 abort = set_ref_in_list(ll, copyID, ht_stack);
7397 }
7398 else
7399 {
7400 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007401 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007402 if (newitem == NULL)
7403 abort = TRUE;
7404 else
7405 {
7406 newitem->list = ll;
7407 newitem->prev = *list_stack;
7408 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007409 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007410 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007411 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007412 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007413 else if (tv->v_type == VAR_PARTIAL)
7414 {
7415 partial_T *pt = tv->vval.v_partial;
7416 int i;
7417
7418 /* A partial does not have a copyID, because it cannot contain itself.
7419 */
7420 if (pt != NULL)
7421 {
7422 if (pt->pt_dict != NULL)
7423 {
7424 typval_T dtv;
7425
7426 dtv.v_type = VAR_DICT;
7427 dtv.vval.v_dict = pt->pt_dict;
7428 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7429 }
7430
7431 for (i = 0; i < pt->pt_argc; ++i)
7432 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
7433 ht_stack, list_stack);
7434 }
7435 }
7436#ifdef FEAT_JOB_CHANNEL
7437 else if (tv->v_type == VAR_JOB)
7438 {
7439 job_T *job = tv->vval.v_job;
7440 typval_T dtv;
7441
7442 if (job != NULL && job->jv_copyID != copyID)
7443 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007444 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007445 if (job->jv_channel != NULL)
7446 {
7447 dtv.v_type = VAR_CHANNEL;
7448 dtv.vval.v_channel = job->jv_channel;
7449 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7450 }
7451 if (job->jv_exit_partial != NULL)
7452 {
7453 dtv.v_type = VAR_PARTIAL;
7454 dtv.vval.v_partial = job->jv_exit_partial;
7455 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7456 }
7457 }
7458 }
7459 else if (tv->v_type == VAR_CHANNEL)
7460 {
7461 channel_T *ch =tv->vval.v_channel;
7462 int part;
7463 typval_T dtv;
7464 jsonq_T *jq;
7465 cbq_T *cq;
7466
7467 if (ch != NULL && ch->ch_copyID != copyID)
7468 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02007469 ch->ch_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007470 for (part = PART_SOCK; part <= PART_IN; ++part)
7471 {
7472 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
7473 jq = jq->jq_next)
7474 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
7475 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
7476 cq = cq->cq_next)
7477 if (cq->cq_partial != NULL)
7478 {
7479 dtv.v_type = VAR_PARTIAL;
7480 dtv.vval.v_partial = cq->cq_partial;
7481 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7482 }
7483 if (ch->ch_part[part].ch_partial != NULL)
7484 {
7485 dtv.v_type = VAR_PARTIAL;
7486 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
7487 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7488 }
7489 }
7490 if (ch->ch_partial != NULL)
7491 {
7492 dtv.v_type = VAR_PARTIAL;
7493 dtv.vval.v_partial = ch->ch_partial;
7494 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7495 }
7496 if (ch->ch_close_partial != NULL)
7497 {
7498 dtv.v_type = VAR_PARTIAL;
7499 dtv.vval.v_partial = ch->ch_close_partial;
7500 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
7501 }
7502 }
7503 }
7504#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007505 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007506}
7507
7508/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509 * Allocate an empty header for a dictionary.
7510 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007511 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007512dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007513{
Bram Moolenaar33570922005-01-25 22:26:29 +00007514 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007518 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007519 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007520 if (first_dict != NULL)
7521 first_dict->dv_used_prev = d;
7522 d->dv_used_next = first_dict;
7523 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007524 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007525
Bram Moolenaar33570922005-01-25 22:26:29 +00007526 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007527 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007528 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007529 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007530 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533}
7534
7535/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007536 * Allocate an empty dict for a return value.
7537 * Returns OK or FAIL.
7538 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007541{
7542 dict_T *d = dict_alloc();
7543
7544 if (d == NULL)
7545 return FAIL;
7546
7547 rettv->vval.v_dict = d;
7548 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007549 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007550 ++d->dv_refcount;
7551 return OK;
7552}
7553
7554
7555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 * Unreference a Dictionary: decrement the reference count and free it when it
7557 * becomes zero.
7558 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007559 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007560dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007561{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007562 if (d != NULL && --d->dv_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007563 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564}
7565
7566/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007567 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568 * Ignores the reference count.
7569 */
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007570 static void
7571dict_free_contents(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007575 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007577 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007578 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007579 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if (!HASHITEM_EMPTY(hi))
7583 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007584 /* Remove the item before deleting it, just in case there is
7585 * something recursive causing trouble. */
7586 di = HI2DI(hi);
7587 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007588 clear_tv(&di->di_tv);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007589 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 --todo;
7591 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007592 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hash_clear(&d->dv_hashtab);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007594}
7595
7596 static void
7597dict_free_dict(dict_T *d)
7598{
7599 /* Remove the dict from the list of dicts for garbage collection. */
7600 if (d->dv_used_prev == NULL)
7601 first_dict = d->dv_used_next;
7602 else
7603 d->dv_used_prev->dv_used_next = d->dv_used_next;
7604 if (d->dv_used_next != NULL)
7605 d->dv_used_next->dv_used_prev = d->dv_used_prev;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 vim_free(d);
7607}
7608
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02007609 void
7610dict_free(dict_T *d)
7611{
7612 if (!in_free_unref_items)
7613 {
7614 dict_free_contents(d);
7615 dict_free_dict(d);
7616 }
7617}
7618
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619/*
7620 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007621 * The "key" is copied to the new item.
7622 * Note that the value of the item "di_tv" still needs to be initialized!
7623 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007624 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007625 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007626dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007627{
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007630 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007631 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007633 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007634 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007635 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007636 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637}
7638
7639/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 * Make a copy of a Dictionary item.
7641 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007642 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007643dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007644{
Bram Moolenaar33570922005-01-25 22:26:29 +00007645 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007646
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007647 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7648 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649 if (di != NULL)
7650 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007651 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007652 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007653 copy_tv(&org->di_tv, &di->di_tv);
7654 }
7655 return di;
7656}
7657
7658/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 * Remove item "item" from Dictionary "dict" and free it.
7660 */
7661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007662dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663{
Bram Moolenaar33570922005-01-25 22:26:29 +00007664 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665
Bram Moolenaar33570922005-01-25 22:26:29 +00007666 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 if (HASHITEM_EMPTY(hi))
7668 EMSG2(_(e_intern2), "dictitem_remove()");
7669 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007671 dictitem_free(item);
7672}
7673
7674/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 * Free a dict item. Also clears the value.
7676 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007678dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007679{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007681 if (item->di_flags & DI_FLAGS_ALLOC)
7682 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683}
7684
7685/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007686 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7687 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007688 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 * Returns NULL when out of memory.
7690 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007691 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007692dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693{
Bram Moolenaar33570922005-01-25 22:26:29 +00007694 dict_T *copy;
7695 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007697 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007698
7699 if (orig == NULL)
7700 return NULL;
7701
7702 copy = dict_alloc();
7703 if (copy != NULL)
7704 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007705 if (copyID != 0)
7706 {
7707 orig->dv_copyID = copyID;
7708 orig->dv_copydict = copy;
7709 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007710 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007711 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007712 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 --todo;
7716
7717 di = dictitem_alloc(hi->hi_key);
7718 if (di == NULL)
7719 break;
7720 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007721 {
7722 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7723 copyID) == FAIL)
7724 {
7725 vim_free(di);
7726 break;
7727 }
7728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 else
7730 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7731 if (dict_add(copy, di) == FAIL)
7732 {
7733 dictitem_free(di);
7734 break;
7735 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007736 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738
Bram Moolenaare9a41262005-01-15 22:18:47 +00007739 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007740 if (todo > 0)
7741 {
7742 dict_unref(copy);
7743 copy = NULL;
7744 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007745 }
7746
7747 return copy;
7748}
7749
7750/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007751 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007752 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007755dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756{
Bram Moolenaar33570922005-01-25 22:26:29 +00007757 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758}
7759
Bram Moolenaar8c711452005-01-14 21:53:12 +00007760/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007761 * Add a number or string entry to dictionary "d".
7762 * When "str" is NULL use number "nr", otherwise use "str".
7763 * Returns FAIL when out of memory and when key already exists.
7764 */
7765 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007766dict_add_nr_str(
7767 dict_T *d,
7768 char *key,
7769 long nr,
7770 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007771{
7772 dictitem_T *item;
7773
7774 item = dictitem_alloc((char_u *)key);
7775 if (item == NULL)
7776 return FAIL;
7777 item->di_tv.v_lock = 0;
7778 if (str == NULL)
7779 {
7780 item->di_tv.v_type = VAR_NUMBER;
7781 item->di_tv.vval.v_number = nr;
7782 }
7783 else
7784 {
7785 item->di_tv.v_type = VAR_STRING;
7786 item->di_tv.vval.v_string = vim_strsave(str);
7787 }
7788 if (dict_add(d, item) == FAIL)
7789 {
7790 dictitem_free(item);
7791 return FAIL;
7792 }
7793 return OK;
7794}
7795
7796/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007797 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007798 * Returns FAIL when out of memory and when key already exists.
7799 */
7800 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007801dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007802{
7803 dictitem_T *item;
7804
7805 item = dictitem_alloc((char_u *)key);
7806 if (item == NULL)
7807 return FAIL;
7808 item->di_tv.v_lock = 0;
7809 item->di_tv.v_type = VAR_LIST;
7810 item->di_tv.vval.v_list = list;
7811 if (dict_add(d, item) == FAIL)
7812 {
7813 dictitem_free(item);
7814 return FAIL;
7815 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007816 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007817 return OK;
7818}
7819
7820/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007821 * Get the number of items in a Dictionary.
7822 */
7823 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007825{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007826 if (d == NULL)
7827 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007828 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007829}
7830
7831/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007832 * Find item "key[len]" in Dictionary "d".
7833 * If "len" is negative use strlen(key).
7834 * Returns NULL when not found.
7835 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007836 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007837dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007838{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007839#define AKEYLEN 200
7840 char_u buf[AKEYLEN];
7841 char_u *akey;
7842 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007844
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +02007845 if (d == NULL)
7846 return NULL;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007847 if (len < 0)
7848 akey = key;
7849 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007850 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007851 tofree = akey = vim_strnsave(key, len);
7852 if (akey == NULL)
7853 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007854 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007855 else
7856 {
7857 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007858 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007859 akey = buf;
7860 }
7861
Bram Moolenaar33570922005-01-25 22:26:29 +00007862 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007863 vim_free(tofree);
7864 if (HASHITEM_EMPTY(hi))
7865 return NULL;
7866 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007867}
7868
7869/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007870 * Get a string item from a dictionary.
7871 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007872 * Returns NULL if the entry doesn't exist or out of memory.
7873 */
7874 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007875get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007876{
7877 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007878 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007879
7880 di = dict_find(d, key, -1);
7881 if (di == NULL)
7882 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007883 s = get_tv_string(&di->di_tv);
7884 if (save && s != NULL)
7885 s = vim_strsave(s);
7886 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007887}
7888
7889/*
7890 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007891 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007892 */
7893 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007894get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007895{
7896 dictitem_T *di;
7897
7898 di = dict_find(d, key, -1);
7899 if (di == NULL)
7900 return 0;
7901 return get_tv_number(&di->di_tv);
7902}
7903
7904/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007905 * Return an allocated string with the string representation of a Dictionary.
7906 * May return NULL.
7907 */
7908 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007909dict2string(typval_T *tv, int copyID, int restore_copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007910{
7911 garray_T ga;
7912 int first = TRUE;
7913 char_u *tofree;
7914 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007915 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007916 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007917 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007918 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007919
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007920 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007921 return NULL;
7922 ga_init2(&ga, (int)sizeof(char), 80);
7923 ga_append(&ga, '{');
7924
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007925 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007926 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007927 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007928 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007929 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007930 --todo;
7931
7932 if (first)
7933 first = FALSE;
7934 else
7935 ga_concat(&ga, (char_u *)", ");
7936
7937 tofree = string_quote(hi->hi_key, FALSE);
7938 if (tofree != NULL)
7939 {
7940 ga_concat(&ga, tofree);
7941 vim_free(tofree);
7942 }
7943 ga_concat(&ga, (char_u *)": ");
Bram Moolenaar18dfb442016-05-31 22:31:23 +02007944 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
7945 FALSE, restore_copyID, TRUE);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007946 if (s != NULL)
7947 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007948 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007949 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007950 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007951 line_breakcheck();
7952
Bram Moolenaar8c711452005-01-14 21:53:12 +00007953 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007954 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007955 if (todo > 0)
7956 {
7957 vim_free(ga.ga_data);
7958 return NULL;
7959 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007960
7961 ga_append(&ga, '}');
7962 ga_append(&ga, NUL);
7963 return (char_u *)ga.ga_data;
7964}
7965
7966/*
7967 * Allocate a variable for a Dictionary and fill it from "*arg".
7968 * Return OK or FAIL. Returns NOTDONE for {expr}.
7969 */
7970 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007971get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007972{
Bram Moolenaar33570922005-01-25 22:26:29 +00007973 dict_T *d = NULL;
7974 typval_T tvkey;
7975 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007976 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007977 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007978 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007979 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980
7981 /*
7982 * First check if it's not a curly-braces thing: {expr}.
7983 * Must do this without evaluating, otherwise a function may be called
7984 * twice. Unfortunately this means we need to call eval1() twice for the
7985 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007987 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007988 if (*start != '}')
7989 {
7990 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7991 return FAIL;
7992 if (*start == '}')
7993 return NOTDONE;
7994 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007995
7996 if (evaluate)
7997 {
7998 d = dict_alloc();
7999 if (d == NULL)
8000 return FAIL;
8001 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008002 tvkey.v_type = VAR_UNKNOWN;
8003 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008004
8005 *arg = skipwhite(*arg + 1);
8006 while (**arg != '}' && **arg != NUL)
8007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008008 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008009 goto failret;
8010 if (**arg != ':')
8011 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008012 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008013 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008014 goto failret;
8015 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00008016 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008017 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008018 key = get_tv_string_buf_chk(&tvkey, buf);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02008019 if (key == NULL)
Bram Moolenaar037cc642007-09-13 18:40:54 +00008020 {
8021 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
Bram Moolenaar037cc642007-09-13 18:40:54 +00008022 clear_tv(&tvkey);
8023 goto failret;
8024 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008025 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008026
8027 *arg = skipwhite(*arg + 1);
8028 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
8029 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00008030 if (evaluate)
8031 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032 goto failret;
8033 }
8034 if (evaluate)
8035 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008036 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 if (item != NULL)
8038 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00008039 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008040 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008041 clear_tv(&tv);
8042 goto failret;
8043 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008044 item = dictitem_alloc(key);
8045 clear_tv(&tvkey);
8046 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00008047 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008048 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008049 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008050 if (dict_add(d, item) == FAIL)
8051 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 }
8053 }
8054
8055 if (**arg == '}')
8056 break;
8057 if (**arg != ',')
8058 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008059 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 goto failret;
8061 }
8062 *arg = skipwhite(*arg + 1);
8063 }
8064
8065 if (**arg != '}')
8066 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008067 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068failret:
8069 if (evaluate)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008070 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008071 return FAIL;
8072 }
8073
8074 *arg = skipwhite(*arg + 1);
8075 if (evaluate)
8076 {
8077 rettv->v_type = VAR_DICT;
8078 rettv->vval.v_dict = d;
8079 ++d->dv_refcount;
8080 }
8081
8082 return OK;
8083}
8084
Bram Moolenaar17a13432016-01-24 14:22:10 +01008085 static char *
8086get_var_special_name(int nr)
8087{
8088 switch (nr)
8089 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01008090 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01008091 case VVAL_TRUE: return "v:true";
8092 case VVAL_NONE: return "v:none";
8093 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01008094 }
8095 EMSG2(_(e_intern2), "get_var_special_name()");
8096 return "42";
8097}
8098
Bram Moolenaar8c711452005-01-14 21:53:12 +00008099/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008100 * Return a string with the string representation of a variable.
8101 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008102 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008103 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008104 * When both "echo_style" and "dict_val" are FALSE, put quotes around stings as
8105 * "string()", otherwise does not put quotes around strings, as ":echo"
8106 * displays values.
8107 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
8108 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008109 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008110 */
8111 static char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008112echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01008113 typval_T *tv,
8114 char_u **tofree,
8115 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008116 int copyID,
8117 int echo_style,
8118 int restore_copyID,
8119 int dict_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008120{
Bram Moolenaare9a41262005-01-15 22:18:47 +00008121 static int recurse = 0;
8122 char_u *r = NULL;
8123
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008125 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02008126 if (!did_echo_string_emsg)
8127 {
8128 /* Only give this message once for a recursive call to avoid
8129 * flooding the user with errors. And stop iterating over lists
8130 * and dicts. */
8131 did_echo_string_emsg = TRUE;
8132 EMSG(_("E724: variable nested too deep for displaying"));
8133 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008134 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02008135 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00008136 }
8137 ++recurse;
8138
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008139 switch (tv->v_type)
8140 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008141 case VAR_STRING:
8142 if (echo_style && !dict_val)
8143 {
8144 *tofree = NULL;
8145 r = get_tv_string_buf(tv, numbuf);
8146 }
8147 else
8148 {
8149 *tofree = string_quote(tv->vval.v_string, FALSE);
8150 r = *tofree;
8151 }
8152 break;
8153
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008154 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008155 if (echo_style)
8156 {
8157 *tofree = NULL;
8158 r = tv->vval.v_string;
8159 }
8160 else
8161 {
8162 *tofree = string_quote(tv->vval.v_string, TRUE);
8163 r = *tofree;
8164 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008165 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008166
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008167 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008168 {
8169 partial_T *pt = tv->vval.v_partial;
8170 char_u *fname = string_quote(pt == NULL ? NULL
8171 : pt->pt_name, FALSE);
8172 garray_T ga;
8173 int i;
8174 char_u *tf;
8175
8176 ga_init2(&ga, 1, 100);
8177 ga_concat(&ga, (char_u *)"function(");
8178 if (fname != NULL)
8179 {
8180 ga_concat(&ga, fname);
8181 vim_free(fname);
8182 }
8183 if (pt != NULL && pt->pt_argc > 0)
8184 {
8185 ga_concat(&ga, (char_u *)", [");
8186 for (i = 0; i < pt->pt_argc; ++i)
8187 {
8188 if (i > 0)
8189 ga_concat(&ga, (char_u *)", ");
8190 ga_concat(&ga,
8191 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
8192 vim_free(tf);
8193 }
8194 ga_concat(&ga, (char_u *)"]");
8195 }
8196 if (pt != NULL && pt->pt_dict != NULL)
8197 {
8198 typval_T dtv;
8199
8200 ga_concat(&ga, (char_u *)", ");
8201 dtv.v_type = VAR_DICT;
8202 dtv.vval.v_dict = pt->pt_dict;
8203 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
8204 vim_free(tf);
8205 }
8206 ga_concat(&ga, (char_u *)")");
8207
8208 *tofree = ga.ga_data;
8209 r = *tofree;
8210 break;
8211 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008212
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008213 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008214 if (tv->vval.v_list == NULL)
8215 {
8216 *tofree = NULL;
8217 r = NULL;
8218 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008219 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
8220 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008221 {
8222 *tofree = NULL;
8223 r = (char_u *)"[...]";
8224 }
8225 else
8226 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008227 int old_copyID = tv->vval.v_list->lv_copyID;
8228
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008229 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008230 *tofree = list2string(tv, copyID, restore_copyID);
8231 if (restore_copyID)
8232 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008233 r = *tofree;
8234 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008235 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008236
Bram Moolenaar8c711452005-01-14 21:53:12 +00008237 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008238 if (tv->vval.v_dict == NULL)
8239 {
8240 *tofree = NULL;
8241 r = NULL;
8242 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008243 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
8244 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008245 {
8246 *tofree = NULL;
8247 r = (char_u *)"{...}";
8248 }
8249 else
8250 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008251 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008252 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008253 *tofree = dict2string(tv, copyID, restore_copyID);
8254 if (restore_copyID)
8255 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008256 r = *tofree;
8257 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008258 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008259
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008260 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008261 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008262 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008263 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008264 *tofree = NULL;
8265 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008266 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008267
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008268 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008269#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008270 *tofree = NULL;
8271 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
8272 r = numbuf;
8273 break;
8274#endif
8275
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008276 case VAR_SPECIAL:
8277 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01008278 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008279 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008281
Bram Moolenaar8502c702014-06-17 12:51:16 +02008282 if (--recurse == 0)
8283 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008284 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008285}
8286
8287/*
8288 * Return a string with the string representation of a variable.
8289 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8290 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008291 * Does not put quotes around strings, as ":echo" displays values.
8292 * When "copyID" is not NULL replace recursive lists and dicts with "...".
8293 * May return NULL.
8294 */
8295 static char_u *
8296echo_string(
8297 typval_T *tv,
8298 char_u **tofree,
8299 char_u *numbuf,
8300 int copyID)
8301{
8302 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
8303}
8304
8305/*
8306 * Return a string with the string representation of a variable.
8307 * If the memory is allocated "tofree" is set to it, otherwise NULL.
8308 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008309 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00008310 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008311 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02008312 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008313tv2string(
8314 typval_T *tv,
8315 char_u **tofree,
8316 char_u *numbuf,
8317 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008318{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02008319 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320}
8321
8322/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008323 * Return string "str" in ' quotes, doubling ' characters.
8324 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008325 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008326 */
8327 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008328string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008329{
Bram Moolenaar33570922005-01-25 22:26:29 +00008330 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008331 char_u *p, *r, *s;
8332
Bram Moolenaar33570922005-01-25 22:26:29 +00008333 len = (function ? 13 : 3);
8334 if (str != NULL)
8335 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008336 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008337 for (p = str; *p != NUL; mb_ptr_adv(p))
8338 if (*p == '\'')
8339 ++len;
8340 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008341 s = r = alloc(len);
8342 if (r != NULL)
8343 {
8344 if (function)
8345 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008347 r += 10;
8348 }
8349 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008350 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 if (str != NULL)
8352 for (p = str; *p != NUL; )
8353 {
8354 if (*p == '\'')
8355 *r++ = '\'';
8356 MB_COPY_CHAR(p, r);
8357 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008358 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008359 if (function)
8360 *r++ = ')';
8361 *r++ = NUL;
8362 }
8363 return s;
8364}
8365
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008366#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367/*
8368 * Convert the string "text" to a floating point number.
8369 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8370 * this always uses a decimal point.
8371 * Returns the length of the text that was consumed.
8372 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008373 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008374string2float(
8375 char_u *text,
8376 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008377{
8378 char *s = (char *)text;
8379 float_T f;
8380
8381 f = strtod(s, &s);
8382 *value = f;
8383 return (int)((char_u *)s - text);
8384}
8385#endif
8386
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 * Get the value of an environment variable.
8389 * "arg" is pointing to the '$'. It is advanced to after the name.
8390 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008391 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 */
8393 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008394get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395{
8396 char_u *string = NULL;
8397 int len;
8398 int cc;
8399 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008400 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401
8402 ++*arg;
8403 name = *arg;
8404 len = get_env_len(arg);
8405 if (evaluate)
8406 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008407 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008408 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008409
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008410 cc = name[len];
8411 name[len] = NUL;
8412 /* first try vim_getenv(), fast for normal environment vars */
8413 string = vim_getenv(name, &mustfree);
8414 if (string != NULL && *string != NUL)
8415 {
8416 if (!mustfree)
8417 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008419 else
8420 {
8421 if (mustfree)
8422 vim_free(string);
8423
8424 /* next try expanding things like $VIM and ${HOME} */
8425 string = expand_env_save(name - 1);
8426 if (string != NULL && *string == '$')
8427 {
8428 vim_free(string);
8429 string = NULL;
8430 }
8431 }
8432 name[len] = cc;
8433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 rettv->v_type = VAR_STRING;
8435 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 }
8437
8438 return OK;
8439}
8440
8441/*
8442 * Array with names and number of arguments of all internal functions
8443 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8444 */
8445static struct fst
8446{
8447 char *f_name; /* function name */
8448 char f_min_argc; /* minimal number of arguments */
8449 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008450 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008451 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452} functions[] =
8453{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008454#ifdef FEAT_FLOAT
8455 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008456 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008457#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008458 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008459 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 {"append", 2, 2, f_append},
8461 {"argc", 0, 0, f_argc},
8462 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008463 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008464 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008465#ifdef FEAT_FLOAT
8466 {"asin", 1, 1, f_asin}, /* WJMc */
8467#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008468 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008469 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008470 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008471 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008472 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02008473 {"assert_notequal", 2, 3, f_assert_notequal},
8474 {"assert_notmatch", 2, 3, f_assert_notmatch},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008475 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008476#ifdef FEAT_FLOAT
8477 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008478 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008481 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"bufexists", 1, 1, f_bufexists},
8483 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8484 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8485 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8486 {"buflisted", 1, 1, f_buflisted},
8487 {"bufloaded", 1, 1, f_bufloaded},
8488 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008489 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"bufwinnr", 1, 1, f_bufwinnr},
8491 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008492 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008493 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008495#ifdef FEAT_FLOAT
8496 {"ceil", 1, 1, f_ceil},
8497#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008499 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008500 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8501 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008502 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008503 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008504 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008505 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008506 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008507 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008508 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008509 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008510 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8511 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008512 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008513 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008514#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008515 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008516 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008518 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008520#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008521 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008522 {"complete_add", 1, 1, f_complete_add},
8523 {"complete_check", 0, 0, f_complete_check},
8524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008526 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008527#ifdef FEAT_FLOAT
8528 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008529 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008530#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008531 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008533 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008534 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008535 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008537 {"diff_filler", 1, 1, f_diff_filler},
8538 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008539 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008541 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 {"eventhandler", 0, 0, f_eventhandler},
8543 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008544 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008546#ifdef FEAT_FLOAT
8547 {"exp", 1, 1, f_exp},
8548#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008549 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008550 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008551 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8553 {"filereadable", 1, 1, f_filereadable},
8554 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008555 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008556 {"finddir", 1, 3, f_finddir},
8557 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008558#ifdef FEAT_FLOAT
8559 {"float2nr", 1, 1, f_float2nr},
8560 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008561 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008562#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008563 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {"fnamemodify", 2, 2, f_fnamemodify},
8565 {"foldclosed", 1, 1, f_foldclosed},
8566 {"foldclosedend", 1, 1, f_foldclosedend},
8567 {"foldlevel", 1, 1, f_foldlevel},
8568 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008569 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008571 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008572 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008573 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008574 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008575 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 {"getchar", 0, 1, f_getchar},
8577 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008578 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 {"getcmdline", 0, 0, f_getcmdline},
8580 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008581 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008582 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008583 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008584 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008585 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008586 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 {"getfsize", 1, 1, f_getfsize},
8588 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008589 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008590 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008591 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008592 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008593 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008594 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008595 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008596 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008598 {"gettabvar", 2, 3, f_gettabvar},
8599 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 {"getwinposx", 0, 0, f_getwinposx},
8601 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008602 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008603 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008604 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008605 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008607 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008608 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008609 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8611 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8612 {"histadd", 2, 2, f_histadd},
8613 {"histdel", 1, 2, f_histdel},
8614 {"histget", 1, 2, f_histget},
8615 {"histnr", 1, 1, f_histnr},
8616 {"hlID", 1, 1, f_hlID},
8617 {"hlexists", 1, 1, f_hlexists},
8618 {"hostname", 0, 0, f_hostname},
8619 {"iconv", 3, 3, f_iconv},
8620 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008621 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008622 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008624 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 {"inputrestore", 0, 0, f_inputrestore},
8626 {"inputsave", 0, 0, f_inputsave},
8627 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008628 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008629 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008631 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008632#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8633 {"isnan", 1, 1, f_isnan},
8634#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008635 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008636#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008637 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008638 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008639 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008640 {"job_start", 1, 2, f_job_start},
8641 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008642 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008643#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008644 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008645 {"js_decode", 1, 1, f_js_decode},
8646 {"js_encode", 1, 1, f_js_encode},
8647 {"json_decode", 1, 1, f_json_decode},
8648 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008649 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008651 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 {"libcall", 3, 3, f_libcall},
8653 {"libcallnr", 3, 3, f_libcallnr},
8654 {"line", 1, 1, f_line},
8655 {"line2byte", 1, 1, f_line2byte},
8656 {"lispindent", 1, 1, f_lispindent},
8657 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008658#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008659 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008660 {"log10", 1, 1, f_log10},
8661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008662#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008663 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008664#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008665 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008666 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008667 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008668 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008669 {"matchadd", 2, 5, f_matchadd},
8670 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008671 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008672 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008673 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008674 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008675 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008676 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008677 {"max", 1, 1, f_max},
8678 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008679#ifdef vim_mkdir
8680 {"mkdir", 1, 3, f_mkdir},
8681#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008682 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008683#ifdef FEAT_MZSCHEME
8684 {"mzeval", 1, 1, f_mzeval},
8685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008687 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008688 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008689 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008690#ifdef FEAT_PERL
8691 {"perleval", 1, 1, f_perleval},
8692#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008693#ifdef FEAT_FLOAT
8694 {"pow", 2, 2, f_pow},
8695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008697 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008698 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008699#ifdef FEAT_PYTHON3
8700 {"py3eval", 1, 1, f_py3eval},
8701#endif
8702#ifdef FEAT_PYTHON
8703 {"pyeval", 1, 1, f_pyeval},
8704#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008705 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008706 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008707 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008708#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008709 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008710#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008711 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {"remote_expr", 2, 3, f_remote_expr},
8713 {"remote_foreground", 1, 1, f_remote_foreground},
8714 {"remote_peek", 1, 2, f_remote_peek},
8715 {"remote_read", 1, 1, f_remote_read},
8716 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008717 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008719 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008721 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008722#ifdef FEAT_FLOAT
8723 {"round", 1, 1, f_round},
8724#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008725 {"screenattr", 2, 2, f_screenattr},
8726 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008727 {"screencol", 0, 0, f_screencol},
8728 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008729 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008730 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008731 {"searchpair", 3, 7, f_searchpair},
8732 {"searchpairpos", 3, 7, f_searchpairpos},
8733 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 {"server2client", 2, 2, f_server2client},
8735 {"serverlist", 0, 0, f_serverlist},
8736 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008737 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008739 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008741 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008742 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008743 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008744 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008746 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008747 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008749#ifdef FEAT_CRYPT
8750 {"sha256", 1, 1, f_sha256},
8751#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008752 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008753 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008755#ifdef FEAT_FLOAT
8756 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008757 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008758#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008759 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008760 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008761 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008762 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008763 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008764#ifdef FEAT_FLOAT
8765 {"sqrt", 1, 1, f_sqrt},
8766 {"str2float", 1, 1, f_str2float},
8767#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008768 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008769 {"strcharpart", 2, 3, f_strcharpart},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008770 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008771 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772#ifdef HAVE_STRFTIME
8773 {"strftime", 1, 2, f_strftime},
8774#endif
Bram Moolenaar58de0e22016-04-14 15:13:46 +02008775 {"strgetchar", 2, 2, f_strgetchar},
Bram Moolenaar33570922005-01-25 22:26:29 +00008776 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008777 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {"strlen", 1, 1, f_strlen},
8779 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008780 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008782 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008783 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 {"substitute", 4, 4, f_substitute},
8785 {"synID", 3, 3, f_synID},
8786 {"synIDattr", 2, 3, f_synIDattr},
8787 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008788 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008789 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008790 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008791 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008792 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008793 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008794 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008795 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008796 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008797#ifdef FEAT_FLOAT
8798 {"tan", 1, 1, f_tan},
8799 {"tanh", 1, 1, f_tanh},
8800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 {"tempname", 0, 0, f_tempname},
Bram Moolenaar8e8df252016-05-25 21:23:21 +02008802 {"test_alloc_fail", 3, 3, f_test_alloc_fail},
8803 {"test_disable_char_avail", 1, 1, f_test_disable_char_avail},
Bram Moolenaar574860b2016-05-24 17:33:34 +02008804 {"test_garbagecollect_now", 0, 0, f_test_garbagecollect_now},
8805#ifdef FEAT_JOB_CHANNEL
8806 {"test_null_channel", 0, 0, f_test_null_channel},
8807#endif
8808 {"test_null_dict", 0, 0, f_test_null_dict},
8809#ifdef FEAT_JOB_CHANNEL
8810 {"test_null_job", 0, 0, f_test_null_job},
8811#endif
8812 {"test_null_list", 0, 0, f_test_null_list},
8813 {"test_null_partial", 0, 0, f_test_null_partial},
8814 {"test_null_string", 0, 0, f_test_null_string},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008815#ifdef FEAT_TIMERS
8816 {"timer_start", 2, 3, f_timer_start},
8817 {"timer_stop", 1, 1, f_timer_stop},
8818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 {"tolower", 1, 1, f_tolower},
8820 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008821 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008822#ifdef FEAT_FLOAT
8823 {"trunc", 1, 1, f_trunc},
8824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008826 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008827 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008828 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008829 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 {"virtcol", 1, 1, f_virtcol},
8831 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008832 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008833 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008834 {"win_getid", 0, 2, f_win_getid},
8835 {"win_gotoid", 1, 1, f_win_gotoid},
8836 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8837 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {"winbufnr", 1, 1, f_winbufnr},
8839 {"wincol", 0, 0, f_wincol},
8840 {"winheight", 1, 1, f_winheight},
8841 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008842 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008844 {"winrestview", 1, 1, f_winrestview},
8845 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008847 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008848 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008849 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850};
8851
8852#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8853
8854/*
8855 * Function given to ExpandGeneric() to obtain the list of internal
8856 * or user defined function names.
8857 */
8858 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008859get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861 static int intidx = -1;
8862 char_u *name;
8863
8864 if (idx == 0)
8865 intidx = -1;
8866 if (intidx < 0)
8867 {
8868 name = get_user_func_name(xp, idx);
8869 if (name != NULL)
8870 return name;
8871 }
8872 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8873 {
8874 STRCPY(IObuff, functions[intidx].f_name);
8875 STRCAT(IObuff, "(");
8876 if (functions[intidx].f_max_argc == 0)
8877 STRCAT(IObuff, ")");
8878 return IObuff;
8879 }
8880
8881 return NULL;
8882}
8883
8884/*
8885 * Function given to ExpandGeneric() to obtain the list of internal or
8886 * user defined variable or function names.
8887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008889get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890{
8891 static int intidx = -1;
8892 char_u *name;
8893
8894 if (idx == 0)
8895 intidx = -1;
8896 if (intidx < 0)
8897 {
8898 name = get_function_name(xp, idx);
8899 if (name != NULL)
8900 return name;
8901 }
8902 return get_user_var_name(xp, ++intidx);
8903}
8904
8905#endif /* FEAT_CMDL_COMPL */
8906
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008907#if defined(EBCDIC) || defined(PROTO)
8908/*
8909 * Compare struct fst by function name.
8910 */
8911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008912compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008913{
8914 struct fst *p1 = (struct fst *)s1;
8915 struct fst *p2 = (struct fst *)s2;
8916
8917 return STRCMP(p1->f_name, p2->f_name);
8918}
8919
8920/*
8921 * Sort the function table by function name.
8922 * The sorting of the table above is ASCII dependant.
8923 * On machines using EBCDIC we have to sort it.
8924 */
8925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008926sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008927{
8928 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8929
8930 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8931}
8932#endif
8933
8934
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935/*
8936 * Find internal function in table above.
8937 * Return index, or -1 if not found
8938 */
8939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008940find_internal_func(
8941 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942{
8943 int first = 0;
8944 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8945 int cmp;
8946 int x;
8947
8948 /*
8949 * Find the function name in the table. Binary search.
8950 */
8951 while (first <= last)
8952 {
8953 x = first + ((unsigned)(last - first) >> 1);
8954 cmp = STRCMP(name, functions[x].f_name);
8955 if (cmp < 0)
8956 last = x - 1;
8957 else if (cmp > 0)
8958 first = x + 1;
8959 else
8960 return x;
8961 }
8962 return -1;
8963}
8964
8965/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008966 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8967 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008968 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8969 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008970 */
8971 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008972deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008973{
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008975 int cc;
8976
Bram Moolenaar65639032016-03-16 21:40:30 +01008977 if (partialp != NULL)
8978 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008979
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008980 cc = name[*lenp];
8981 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008982 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008984 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008985 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008986 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 {
8988 *lenp = 0;
8989 return (char_u *)""; /* just in case */
8990 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008991 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008992 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008993 }
8994
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008995 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8996 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008997 partial_T *pt = v->di_tv.vval.v_partial;
8998
8999 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009000 {
9001 *lenp = 0;
9002 return (char_u *)""; /* just in case */
9003 }
Bram Moolenaar65639032016-03-16 21:40:30 +01009004 if (partialp != NULL)
9005 *partialp = pt;
9006 *lenp = (int)STRLEN(pt->pt_name);
9007 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009008 }
9009
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009010 return name;
9011}
9012
9013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014 * Allocate a variable for the result of a function.
9015 * Return OK or FAIL.
9016 */
9017 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009018get_func_tv(
9019 char_u *name, /* name of the function */
9020 int len, /* length of "name" */
9021 typval_T *rettv,
9022 char_u **arg, /* argument, pointing to the '(' */
9023 linenr_T firstline, /* first line of range */
9024 linenr_T lastline, /* last line of range */
9025 int *doesrange, /* return: function handled range */
9026 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009027 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009028 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029{
9030 char_u *argp;
9031 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009032 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 int argcount = 0; /* number of arguments found */
9034
9035 /*
9036 * Get the arguments.
9037 */
9038 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009039 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 {
9041 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
9042 if (*argp == ')' || *argp == ',' || *argp == NUL)
9043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
9045 {
9046 ret = FAIL;
9047 break;
9048 }
9049 ++argcount;
9050 if (*argp != ',')
9051 break;
9052 }
9053 if (*argp == ')')
9054 ++argp;
9055 else
9056 ret = FAIL;
9057
9058 if (ret == OK)
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009059 {
9060 int i = 0;
9061
9062 if (get_vim_var_nr(VV_TESTING))
9063 {
Bram Moolenaar8e8df252016-05-25 21:23:21 +02009064 /* Prepare for calling test_garbagecollect_now(), need to know
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009065 * what variables are used on the call stack. */
9066 if (funcargs.ga_itemsize == 0)
9067 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
9068 for (i = 0; i < argcount; ++i)
9069 if (ga_grow(&funcargs, 1) == OK)
9070 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
9071 &argvars[i];
9072 }
9073
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009074 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009075 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02009076
9077 funcargs.ga_len -= i;
9078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00009080 {
9081 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009082 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009084 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00009085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086
9087 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090 *arg = skipwhite(argp);
9091 return ret;
9092}
9093
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009094#define ERROR_UNKNOWN 0
9095#define ERROR_TOOMANY 1
9096#define ERROR_TOOFEW 2
9097#define ERROR_SCRIPT 3
9098#define ERROR_DICT 4
9099#define ERROR_NONE 5
9100#define ERROR_OTHER 6
9101#define FLEN_FIXED 40
9102
9103/*
9104 * In a script change <SID>name() and s:name() to K_SNR 123_name().
9105 * Change <SNR>123_name() to K_SNR 123_name().
9106 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
9107 * (slow).
9108 */
9109 static char_u *
9110fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
9111{
9112 int llen;
9113 char_u *fname;
9114 int i;
9115
9116 llen = eval_fname_script(name);
9117 if (llen > 0)
9118 {
9119 fname_buf[0] = K_SPECIAL;
9120 fname_buf[1] = KS_EXTRA;
9121 fname_buf[2] = (int)KE_SNR;
9122 i = 3;
9123 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
9124 {
9125 if (current_SID <= 0)
9126 *error = ERROR_SCRIPT;
9127 else
9128 {
9129 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
9130 i = (int)STRLEN(fname_buf);
9131 }
9132 }
9133 if (i + STRLEN(name + llen) < FLEN_FIXED)
9134 {
9135 STRCPY(fname_buf + i, name + llen);
9136 fname = fname_buf;
9137 }
9138 else
9139 {
9140 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
9141 if (fname == NULL)
9142 *error = ERROR_OTHER;
9143 else
9144 {
9145 *tofree = fname;
9146 mch_memmove(fname, fname_buf, (size_t)i);
9147 STRCPY(fname + i, name + llen);
9148 }
9149 }
9150 }
9151 else
9152 fname = name;
9153 return fname;
9154}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155
9156/*
9157 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02009158 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00009159 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01009161 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009162call_func(
9163 char_u *funcname, /* name of the function */
9164 int len, /* length of "name" */
9165 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009166 int argcount_in, /* number of "argvars" */
9167 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009168 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01009169 linenr_T firstline, /* first line of range */
9170 linenr_T lastline, /* last line of range */
9171 int *doesrange, /* return: function handled range */
9172 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009173 partial_T *partial, /* optional, can be NULL */
9174 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 int error = ERROR_NONE;
9178 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009181 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009183 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009184 int argcount = argcount_in;
9185 typval_T *argvars = argvars_in;
9186 dict_T *selfdict = selfdict_in;
9187 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
9188 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009189
9190 /* Make a copy of the name, if it comes from a funcref variable it could
9191 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02009192 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009193 if (name == NULL)
9194 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009196 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197
9198 *doesrange = FALSE;
9199
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009200 if (partial != NULL)
9201 {
Bram Moolenaar1d429612016-05-24 15:44:17 +02009202 /* When the function has a partial with a dict and there is a dict
9203 * argument, use the dict argument. That is backwards compatible.
9204 * When the dict was bound explicitly use the one from the partial. */
9205 if (partial->pt_dict != NULL
9206 && (selfdict_in == NULL || !partial->pt_auto))
9207 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009208 if (error == ERROR_NONE && partial->pt_argc > 0)
9209 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009210 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
9211 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
9212 for (i = 0; i < argcount_in; ++i)
9213 argv[i + argv_clear] = argvars_in[i];
9214 argvars = argv;
9215 argcount = partial->pt_argc + argcount_in;
9216 }
9217 }
9218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219
9220 /* execute the function if no errors detected and executing */
9221 if (evaluate && error == ERROR_NONE)
9222 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009223 char_u *rfname = fname;
9224
9225 /* Ignore "g:" before a function name. */
9226 if (fname[0] == 'g' && fname[1] == ':')
9227 rfname = fname + 2;
9228
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009229 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
9230 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 error = ERROR_UNKNOWN;
9232
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009233 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 {
9235 /*
9236 * User defined function.
9237 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009238 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009239
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009241 /* Trigger FuncUndefined event, may load the function. */
9242 if (fp == NULL
9243 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009244 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009245 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009247 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009248 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 }
9250#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009251 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009252 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009253 {
9254 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02009255 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00009256 }
9257
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 if (fp != NULL)
9259 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009260 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009262 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009264 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009266 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009267 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268 else
9269 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009270 int did_save_redo = FALSE;
9271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 /*
9273 * Call the user function.
9274 * Save and restore search patterns, script variables and
9275 * redo buffer.
9276 */
9277 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009278#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009279 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01009280#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009281 {
9282 saveRedobuff();
9283 did_save_redo = TRUE;
9284 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009285 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009288 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
9289 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
9290 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00009291 /* Function was unreferenced while being used, free it
9292 * now. */
9293 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01009294 if (did_save_redo)
9295 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 restore_search_patterns();
9297 error = ERROR_NONE;
9298 }
9299 }
9300 }
9301 else
9302 {
9303 /*
9304 * Find the function name in the table, call its implementation.
9305 */
9306 i = find_internal_func(fname);
9307 if (i >= 0)
9308 {
9309 if (argcount < functions[i].f_min_argc)
9310 error = ERROR_TOOFEW;
9311 else if (argcount > functions[i].f_max_argc)
9312 error = ERROR_TOOMANY;
9313 else
9314 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009315 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009316 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317 error = ERROR_NONE;
9318 }
9319 }
9320 }
9321 /*
9322 * The function call (or "FuncUndefined" autocommand sequence) might
9323 * have been aborted by an error, an interrupt, or an explicitly thrown
9324 * exception that has not been caught so far. This situation can be
9325 * tested for by calling aborting(). For an error in an internal
9326 * function or for the "E132" error in call_user_func(), however, the
9327 * throw point at which the "force_abort" flag (temporarily reset by
9328 * emsg()) is normally updated has not been reached yet. We need to
9329 * update that flag first to make aborting() reliable.
9330 */
9331 update_force_abort();
9332 }
9333 if (error == ERROR_NONE)
9334 ret = OK;
9335
9336 /*
9337 * Report an error unless the argument evaluation or function call has been
9338 * cancelled due to an aborting error, an interrupt, or an exception.
9339 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009340 if (!aborting())
9341 {
9342 switch (error)
9343 {
9344 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009345 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009346 break;
9347 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009348 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009349 break;
9350 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009351 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009352 name);
9353 break;
9354 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009355 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009356 name);
9357 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009358 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009359 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009360 name);
9361 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009362 }
9363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009365 while (argv_clear > 0)
9366 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009367 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009368 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369
9370 return ret;
9371}
9372
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009373/*
9374 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009375 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009376 */
9377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009378emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009379{
9380 char_u *p;
9381
9382 if (*name == K_SPECIAL)
9383 p = concat_str((char_u *)"<SNR>", name + 3);
9384 else
9385 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009386 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009387 if (p != name)
9388 vim_free(p);
9389}
9390
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009391/*
9392 * Return TRUE for a non-zero Number and a non-empty String.
9393 */
9394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009395non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009396{
9397 return ((argvars[0].v_type == VAR_NUMBER
9398 && argvars[0].vval.v_number != 0)
9399 || (argvars[0].v_type == VAR_STRING
9400 && argvars[0].vval.v_string != NULL
9401 && *argvars[0].vval.v_string != NUL));
9402}
9403
Bram Moolenaar071d4272004-06-13 20:20:40 +00009404/*********************************************
9405 * Implementation of the built-in functions
9406 */
9407
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009408#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009409static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009410
9411/*
9412 * Get the float value of "argvars[0]" into "f".
9413 * Returns FAIL when the argument is not a Number or Float.
9414 */
9415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009416get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009417{
9418 if (argvars[0].v_type == VAR_FLOAT)
9419 {
9420 *f = argvars[0].vval.v_float;
9421 return OK;
9422 }
9423 if (argvars[0].v_type == VAR_NUMBER)
9424 {
9425 *f = (float_T)argvars[0].vval.v_number;
9426 return OK;
9427 }
9428 EMSG(_("E808: Number or Float required"));
9429 return FAIL;
9430}
9431
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009432/*
9433 * "abs(expr)" function
9434 */
9435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009436f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009437{
9438 if (argvars[0].v_type == VAR_FLOAT)
9439 {
9440 rettv->v_type = VAR_FLOAT;
9441 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9442 }
9443 else
9444 {
9445 varnumber_T n;
9446 int error = FALSE;
9447
9448 n = get_tv_number_chk(&argvars[0], &error);
9449 if (error)
9450 rettv->vval.v_number = -1;
9451 else if (n > 0)
9452 rettv->vval.v_number = n;
9453 else
9454 rettv->vval.v_number = -n;
9455 }
9456}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009457
9458/*
9459 * "acos()" function
9460 */
9461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009463{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009464 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009465
9466 rettv->v_type = VAR_FLOAT;
9467 if (get_float_arg(argvars, &f) == OK)
9468 rettv->vval.v_float = acos(f);
9469 else
9470 rettv->vval.v_float = 0.0;
9471}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009472#endif
9473
Bram Moolenaar071d4272004-06-13 20:20:40 +00009474/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009475 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 */
9477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009478f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009479{
Bram Moolenaar33570922005-01-25 22:26:29 +00009480 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009482 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009483 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009485 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009486 && !tv_check_lock(l->lv_lock,
9487 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009488 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009489 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009490 }
9491 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009492 EMSG(_(e_listreq));
9493}
9494
9495/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009496 * "and(expr, expr)" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009500{
9501 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9502 & get_tv_number_chk(&argvars[1], NULL);
9503}
9504
9505/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009506 * "append(lnum, string/list)" function
9507 */
9508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009509f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009510{
9511 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009513 list_T *l = NULL;
9514 listitem_T *li = NULL;
9515 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009516 long added = 0;
9517
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009518 /* When coming here from Insert mode, sync undo, so that this can be
9519 * undone separately from what was previously inserted. */
9520 if (u_sync_once == 2)
9521 {
9522 u_sync_once = 1; /* notify that u_sync() was called */
9523 u_sync(TRUE);
9524 }
9525
Bram Moolenaar0d660222005-01-07 21:51:51 +00009526 lnum = get_tv_lnum(argvars);
9527 if (lnum >= 0
9528 && lnum <= curbuf->b_ml.ml_line_count
9529 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009530 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009531 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009532 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009533 l = argvars[1].vval.v_list;
9534 if (l == NULL)
9535 return;
9536 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009537 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009538 for (;;)
9539 {
9540 if (l == NULL)
9541 tv = &argvars[1]; /* append a string */
9542 else if (li == NULL)
9543 break; /* end of list */
9544 else
9545 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 line = get_tv_string_chk(tv);
9547 if (line == NULL) /* type error */
9548 {
9549 rettv->vval.v_number = 1; /* Failed */
9550 break;
9551 }
9552 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009553 ++added;
9554 if (l == NULL)
9555 break;
9556 li = li->li_next;
9557 }
9558
9559 appended_lines_mark(lnum, added);
9560 if (curwin->w_cursor.lnum > lnum)
9561 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009563 else
9564 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
9568 * "argc()" function
9569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009571f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574}
9575
9576/*
9577 * "argidx()" function
9578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009580f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009582 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009583}
9584
9585/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009586 * "arglistid()" function
9587 */
9588 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009589f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009590{
9591 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009592
9593 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009594 wp = find_tabwin(&argvars[0], &argvars[1]);
9595 if (wp != NULL)
9596 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009597}
9598
9599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600 * "argv(nr)" function
9601 */
9602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009603f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 int idx;
9606
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009607 if (argvars[0].v_type != VAR_UNKNOWN)
9608 {
9609 idx = get_tv_number_chk(&argvars[0], NULL);
9610 if (idx >= 0 && idx < ARGCOUNT)
9611 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9612 else
9613 rettv->vval.v_string = NULL;
9614 rettv->v_type = VAR_STRING;
9615 }
9616 else if (rettv_list_alloc(rettv) == OK)
9617 for (idx = 0; idx < ARGCOUNT; ++idx)
9618 list_append_string(rettv->vval.v_list,
9619 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009620}
9621
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009622typedef enum
9623{
9624 ASSERT_EQUAL,
9625 ASSERT_NOTEQUAL,
9626 ASSERT_MATCH,
9627 ASSERT_NOTMATCH,
Bram Moolenaar3780bb92016-04-12 22:18:53 +02009628 ASSERT_OTHER
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009629} assert_type_T;
9630
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009631static void prepare_assert_error(garray_T*gap);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009632static 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 +01009633static void assert_error(garray_T *gap);
9634static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009635
9636/*
9637 * Prepare "gap" for an assert error and add the sourcing position.
9638 */
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009641{
9642 char buf[NUMBUFLEN];
9643
9644 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009645 if (sourcing_name != NULL)
9646 {
9647 ga_concat(gap, sourcing_name);
9648 if (sourcing_lnum > 0)
9649 ga_concat(gap, (char_u *)" ");
9650 }
9651 if (sourcing_lnum > 0)
9652 {
9653 sprintf(buf, "line %ld", (long)sourcing_lnum);
9654 ga_concat(gap, (char_u *)buf);
9655 }
9656 if (sourcing_name != NULL || sourcing_lnum > 0)
9657 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009658}
9659
9660/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009661 * Append "str" to "gap", escaping unprintable characters.
9662 * Changes NL to \n, CR to \r, etc.
9663 */
9664 static void
9665ga_concat_esc(garray_T *gap, char_u *str)
9666{
9667 char_u *p;
9668 char_u buf[NUMBUFLEN];
9669
Bram Moolenaarf1551962016-03-15 12:55:58 +01009670 if (str == NULL)
9671 {
9672 ga_concat(gap, (char_u *)"NULL");
9673 return;
9674 }
9675
Bram Moolenaar23689172016-02-15 22:37:37 +01009676 for (p = str; *p != NUL; ++p)
9677 switch (*p)
9678 {
9679 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9680 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9681 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9682 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9683 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9684 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9685 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9686 default:
9687 if (*p < ' ')
9688 {
9689 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9690 ga_concat(gap, buf);
9691 }
9692 else
9693 ga_append(gap, *p);
9694 break;
9695 }
9696}
9697
9698/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009699 * Fill "gap" with information about an assert error.
9700 */
9701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009702fill_assert_error(
9703 garray_T *gap,
9704 typval_T *opt_msg_tv,
9705 char_u *exp_str,
9706 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009707 typval_T *got_tv,
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009708 assert_type_T atype)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009709{
9710 char_u numbuf[NUMBUFLEN];
9711 char_u *tofree;
9712
9713 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9714 {
9715 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9716 vim_free(tofree);
9717 }
9718 else
9719 {
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009720 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009721 ga_concat(gap, (char_u *)"Pattern ");
9722 else
9723 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009724 if (exp_str == NULL)
9725 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009726 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009727 vim_free(tofree);
9728 }
9729 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009730 ga_concat_esc(gap, exp_str);
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009731 if (atype == ASSERT_MATCH)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009732 ga_concat(gap, (char_u *)" does not match ");
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009733 else if (atype == ASSERT_NOTMATCH)
9734 ga_concat(gap, (char_u *)" does match ");
9735 else if (atype == ASSERT_NOTEQUAL)
9736 ga_concat(gap, (char_u *)" differs from ");
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009737 else
9738 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009739 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009740 vim_free(tofree);
9741 }
9742}
Bram Moolenaar43345542015-11-29 17:35:35 +01009743
9744/*
9745 * Add an assert error to v:errors.
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009749{
9750 struct vimvar *vp = &vimvars[VV_ERRORS];
9751
9752 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9753 /* Make sure v:errors is a list. */
9754 set_vim_var_list(VV_ERRORS, list_alloc());
9755 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9756}
9757
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009758 static void
9759assert_equal_common(typval_T *argvars, assert_type_T atype)
9760{
9761 garray_T ga;
9762
9763 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9764 != (atype == ASSERT_EQUAL))
9765 {
9766 prepare_assert_error(&ga);
9767 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9768 atype);
9769 assert_error(&ga);
9770 ga_clear(&ga);
9771 }
9772}
9773
Bram Moolenaar43345542015-11-29 17:35:35 +01009774/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009775 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009776 */
9777 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009778f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009779{
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009780 assert_equal_common(argvars, ASSERT_EQUAL);
9781}
Bram Moolenaar43345542015-11-29 17:35:35 +01009782
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009783/*
9784 * "assert_notequal(expected, actual[, msg])" function
9785 */
9786 static void
9787f_assert_notequal(typval_T *argvars, typval_T *rettv UNUSED)
9788{
9789 assert_equal_common(argvars, ASSERT_NOTEQUAL);
Bram Moolenaar43345542015-11-29 17:35:35 +01009790}
9791
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009792/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009793 * "assert_exception(string[, msg])" function
9794 */
9795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009796f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009797{
9798 garray_T ga;
9799 char *error;
9800
9801 error = (char *)get_tv_string_chk(&argvars[0]);
9802 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9803 {
9804 prepare_assert_error(&ga);
9805 ga_concat(&ga, (char_u *)"v:exception is not set");
9806 assert_error(&ga);
9807 ga_clear(&ga);
9808 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009809 else if (error != NULL
9810 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009811 {
9812 prepare_assert_error(&ga);
9813 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009814 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009815 assert_error(&ga);
9816 ga_clear(&ga);
9817 }
9818}
9819
9820/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009821 * "assert_fails(cmd [, error])" function
9822 */
9823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009824f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009825{
9826 char_u *cmd = get_tv_string_chk(&argvars[0]);
9827 garray_T ga;
9828
9829 called_emsg = FALSE;
9830 suppress_errthrow = TRUE;
9831 emsg_silent = TRUE;
9832 do_cmdline_cmd(cmd);
9833 if (!called_emsg)
9834 {
9835 prepare_assert_error(&ga);
9836 ga_concat(&ga, (char_u *)"command did not fail: ");
9837 ga_concat(&ga, cmd);
9838 assert_error(&ga);
9839 ga_clear(&ga);
9840 }
9841 else if (argvars[1].v_type != VAR_UNKNOWN)
9842 {
9843 char_u buf[NUMBUFLEN];
9844 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9845
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009846 if (error == NULL
9847 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009848 {
9849 prepare_assert_error(&ga);
9850 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009851 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
Bram Moolenaara260b872016-01-15 20:48:22 +01009852 assert_error(&ga);
9853 ga_clear(&ga);
9854 }
9855 }
9856
9857 called_emsg = FALSE;
9858 suppress_errthrow = FALSE;
9859 emsg_silent = FALSE;
9860 emsg_on_display = FALSE;
9861 set_vim_var_string(VV_ERRMSG, NULL, 0);
9862}
9863
9864/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009865 * Common for assert_true() and assert_false().
9866 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009868assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009869{
9870 int error = FALSE;
9871 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009872
Bram Moolenaar37127922016-02-06 20:29:28 +01009873 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009874 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009875 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009876 if (argvars[0].v_type != VAR_NUMBER
9877 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9878 || error)
9879 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009880 prepare_assert_error(&ga);
9881 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009882 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009883 NULL, &argvars[0], ASSERT_OTHER);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009884 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009885 ga_clear(&ga);
9886 }
9887}
9888
9889/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009890 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009891 */
9892 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009893f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009894{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009895 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009896}
9897
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009898 static void
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009899assert_match_common(typval_T *argvars, assert_type_T atype)
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009900{
9901 garray_T ga;
9902 char_u buf1[NUMBUFLEN];
9903 char_u buf2[NUMBUFLEN];
9904 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9905 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9906
Bram Moolenaar72188e92016-03-28 22:48:29 +02009907 if (pat == NULL || text == NULL)
9908 EMSG(_(e_invarg));
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009909 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009910 {
9911 prepare_assert_error(&ga);
9912 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009913 atype);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009914 assert_error(&ga);
9915 ga_clear(&ga);
9916 }
9917}
9918
9919/*
Bram Moolenaarb50e5f52016-04-03 20:57:20 +02009920 * "assert_match(pattern, actual[, msg])" function
9921 */
9922 static void
9923f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9924{
9925 assert_match_common(argvars, ASSERT_MATCH);
9926}
9927
9928/*
9929 * "assert_notmatch(pattern, actual[, msg])" function
9930 */
9931 static void
9932f_assert_notmatch(typval_T *argvars, typval_T *rettv UNUSED)
9933{
9934 assert_match_common(argvars, ASSERT_NOTMATCH);
9935}
9936
9937/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009938 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009939 */
9940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009941f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009942{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009943 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009944}
9945
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009946#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009947/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009948 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009949 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009951f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009952{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009953 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009954
9955 rettv->v_type = VAR_FLOAT;
9956 if (get_float_arg(argvars, &f) == OK)
9957 rettv->vval.v_float = asin(f);
9958 else
9959 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009960}
9961
9962/*
9963 * "atan()" function
9964 */
9965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009966f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009967{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009968 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009969
9970 rettv->v_type = VAR_FLOAT;
9971 if (get_float_arg(argvars, &f) == OK)
9972 rettv->vval.v_float = atan(f);
9973 else
9974 rettv->vval.v_float = 0.0;
9975}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009976
9977/*
9978 * "atan2()" function
9979 */
9980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009981f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009982{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009983 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009984
9985 rettv->v_type = VAR_FLOAT;
9986 if (get_float_arg(argvars, &fx) == OK
9987 && get_float_arg(&argvars[1], &fy) == OK)
9988 rettv->vval.v_float = atan2(fx, fy);
9989 else
9990 rettv->vval.v_float = 0.0;
9991}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009992#endif
9993
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994/*
9995 * "browse(save, title, initdir, default)" function
9996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009998f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000#ifdef FEAT_BROWSE
10001 int save;
10002 char_u *title;
10003 char_u *initdir;
10004 char_u *defname;
10005 char_u buf[NUMBUFLEN];
10006 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010007 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010009 save = get_tv_number_chk(&argvars[0], &error);
10010 title = get_tv_string_chk(&argvars[1]);
10011 initdir = get_tv_string_buf_chk(&argvars[2], buf);
10012 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010013
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010014 if (error || title == NULL || initdir == NULL || defname == NULL)
10015 rettv->vval.v_string = NULL;
10016 else
10017 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010018 do_browse(save ? BROWSE_SAVE : 0,
10019 title, defname, NULL, initdir, NULL, curbuf);
10020#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010021 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010022#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010023 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010024}
10025
10026/*
10027 * "browsedir(title, initdir)" function
10028 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010030f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010031{
10032#ifdef FEAT_BROWSE
10033 char_u *title;
10034 char_u *initdir;
10035 char_u buf[NUMBUFLEN];
10036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010037 title = get_tv_string_chk(&argvars[0]);
10038 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010039
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010040 if (title == NULL || initdir == NULL)
10041 rettv->vval.v_string = NULL;
10042 else
10043 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010044 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010046 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010047#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010048 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049}
10050
Bram Moolenaar48e697e2016-01-23 22:17:30 +010010051static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010052
Bram Moolenaar071d4272004-06-13 20:20:40 +000010053/*
10054 * Find a buffer by number or exact name.
10055 */
10056 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010057find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058{
10059 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010061 if (avar->v_type == VAR_NUMBER)
10062 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000010063 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010065 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010066 if (buf == NULL)
10067 {
10068 /* No full path name match, try a match with a URL or a "nofile"
10069 * buffer, these don't use the full path. */
10070 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10071 if (buf->b_fname != NULL
10072 && (path_with_url(buf->b_fname)
10073#ifdef FEAT_QUICKFIX
10074 || bt_nofile(buf)
10075#endif
10076 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010077 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000010078 break;
10079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 }
10081 return buf;
10082}
10083
10084/*
10085 * "bufexists(expr)" function
10086 */
10087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010088f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010090 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091}
10092
10093/*
10094 * "buflisted(expr)" function
10095 */
10096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010097f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098{
10099 buf_T *buf;
10100
10101 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010102 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103}
10104
10105/*
10106 * "bufloaded(expr)" function
10107 */
10108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010109f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110{
10111 buf_T *buf;
10112
10113 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115}
10116
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010117 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +010010118buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 int save_magic;
10121 char_u *save_cpo;
10122 buf_T *buf;
10123
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
10125 save_magic = p_magic;
10126 p_magic = TRUE;
10127 save_cpo = p_cpo;
10128 p_cpo = (char_u *)"";
10129
10130 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010131 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 p_magic = save_magic;
10134 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +010010135 return buf;
10136}
10137
10138/*
10139 * Get buffer by number or pattern.
10140 */
10141 static buf_T *
10142get_buf_tv(typval_T *tv, int curtab_only)
10143{
10144 char_u *name = tv->vval.v_string;
10145 buf_T *buf;
10146
10147 if (tv->v_type == VAR_NUMBER)
10148 return buflist_findnr((int)tv->vval.v_number);
10149 if (tv->v_type != VAR_STRING)
10150 return NULL;
10151 if (name == NULL || *name == NUL)
10152 return curbuf;
10153 if (name[0] == '$' && name[1] == NUL)
10154 return lastbuf;
10155
10156 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157
10158 /* If not found, try expanding the name, like done for bufexists(). */
10159 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161
10162 return buf;
10163}
10164
10165/*
10166 * "bufname(expr)" function
10167 */
10168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010169f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170{
10171 buf_T *buf;
10172
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010173 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010175 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010176 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010178 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010180 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 --emsg_off;
10182}
10183
10184/*
10185 * "bufnr(expr)" function
10186 */
10187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010188f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189{
10190 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010191 int error = FALSE;
10192 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010194 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010196 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010197 --emsg_off;
10198
10199 /* If the buffer isn't found and the second argument is not zero create a
10200 * new buffer. */
10201 if (buf == NULL
10202 && argvars[1].v_type != VAR_UNKNOWN
10203 && get_tv_number_chk(&argvars[1], &error) != 0
10204 && !error
10205 && (name = get_tv_string_chk(&argvars[0])) != NULL
10206 && !error)
10207 buf = buflist_new(name, NULL, (linenr_T)1, 0);
10208
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010210 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213}
10214
10215/*
10216 * "bufwinnr(nr)" function
10217 */
10218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010219f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221#ifdef FEAT_WINDOWS
10222 win_T *wp;
10223 int winnr = 0;
10224#endif
10225 buf_T *buf;
10226
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010227 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010010229 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230#ifdef FEAT_WINDOWS
10231 for (wp = firstwin; wp; wp = wp->w_next)
10232 {
10233 ++winnr;
10234 if (wp->w_buffer == buf)
10235 break;
10236 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010237 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010239 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240#endif
10241 --emsg_off;
10242}
10243
10244/*
10245 * "byte2line(byte)" function
10246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010248f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010249{
10250#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010251 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252#else
10253 long boff = 0;
10254
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010255 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010259 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 (linenr_T)0, &boff);
10261#endif
10262}
10263
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010265byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010266{
10267#ifdef FEAT_MBYTE
10268 char_u *t;
10269#endif
10270 char_u *str;
10271 long idx;
10272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010273 str = get_tv_string_chk(&argvars[0]);
10274 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010277 return;
10278
10279#ifdef FEAT_MBYTE
10280 t = str;
10281 for ( ; idx > 0; idx--)
10282 {
10283 if (*t == NUL) /* EOL reached */
10284 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010285 if (enc_utf8 && comp)
10286 t += utf_ptr2len(t);
10287 else
10288 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010290 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010291#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010292 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010294#endif
10295}
10296
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010297/*
10298 * "byteidx()" function
10299 */
10300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010301f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010302{
10303 byteidx(argvars, rettv, FALSE);
10304}
10305
10306/*
10307 * "byteidxcomp()" function
10308 */
10309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010310f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +010010311{
10312 byteidx(argvars, rettv, TRUE);
10313}
10314
Bram Moolenaardb913952012-06-29 12:54:53 +020010315 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010316func_call(
10317 char_u *name,
10318 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010319 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320 dict_T *selfdict,
10321 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020010322{
10323 listitem_T *item;
10324 typval_T argv[MAX_FUNC_ARGS + 1];
10325 int argc = 0;
10326 int dummy;
10327 int r = 0;
10328
10329 for (item = args->vval.v_list->lv_first; item != NULL;
10330 item = item->li_next)
10331 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010332 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +020010333 {
10334 EMSG(_("E699: Too many arguments"));
10335 break;
10336 }
10337 /* Make a copy of each argument. This is needed to be able to set
10338 * v_lock to VAR_FIXED in the copy without changing the original list.
10339 */
10340 copy_tv(&item->li_tv, &argv[argc++]);
10341 }
10342
10343 if (item == NULL)
10344 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10345 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010346 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010347
10348 /* Free the arguments. */
10349 while (argc > 0)
10350 clear_tv(&argv[--argc]);
10351
10352 return r;
10353}
10354
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010355/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010356 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010357 */
10358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010359f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010360{
10361 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010362 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010363 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010364
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010365 if (argvars[1].v_type != VAR_LIST)
10366 {
10367 EMSG(_(e_listreq));
10368 return;
10369 }
10370 if (argvars[1].vval.v_list == NULL)
10371 return;
10372
10373 if (argvars[0].v_type == VAR_FUNC)
10374 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010375 else if (argvars[0].v_type == VAR_PARTIAL)
10376 {
10377 partial = argvars[0].vval.v_partial;
10378 func = partial->pt_name;
10379 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010380 else
10381 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010382 if (*func == NUL)
10383 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010384
Bram Moolenaare9a41262005-01-15 22:18:47 +000010385 if (argvars[2].v_type != VAR_UNKNOWN)
10386 {
10387 if (argvars[2].v_type != VAR_DICT)
10388 {
10389 EMSG(_(e_dictreq));
10390 return;
10391 }
10392 selfdict = argvars[2].vval.v_dict;
10393 }
10394
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010395 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010396}
10397
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010398#ifdef FEAT_FLOAT
10399/*
10400 * "ceil({float})" function
10401 */
10402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010403f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010404{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010405 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010406
10407 rettv->v_type = VAR_FLOAT;
10408 if (get_float_arg(argvars, &f) == OK)
10409 rettv->vval.v_float = ceil(f);
10410 else
10411 rettv->vval.v_float = 0.0;
10412}
10413#endif
10414
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010415#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010416/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010417 * "ch_close()" function
10418 */
10419 static void
10420f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10421{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010422 channel_T *channel = get_channel_arg(&argvars[0], TRUE, FALSE, 0);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010423
10424 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010425 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010426 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010427 channel_clear(channel);
10428 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010429}
10430
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010431/*
10432 * "ch_getbufnr()" function
10433 */
10434 static void
10435f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10436{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010437 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010438
10439 rettv->vval.v_number = -1;
10440 if (channel != NULL)
10441 {
10442 char_u *what = get_tv_string(&argvars[1]);
10443 int part;
10444
10445 if (STRCMP(what, "err") == 0)
10446 part = PART_ERR;
10447 else if (STRCMP(what, "out") == 0)
10448 part = PART_OUT;
10449 else if (STRCMP(what, "in") == 0)
10450 part = PART_IN;
10451 else
10452 part = PART_SOCK;
10453 if (channel->ch_part[part].ch_buffer != NULL)
10454 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10455 }
10456}
10457
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010458/*
10459 * "ch_getjob()" function
10460 */
10461 static void
10462f_ch_getjob(typval_T *argvars, typval_T *rettv)
10463{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010464 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010465
10466 if (channel != NULL)
10467 {
10468 rettv->v_type = VAR_JOB;
10469 rettv->vval.v_job = channel->ch_job;
10470 if (channel->ch_job != NULL)
10471 ++channel->ch_job->jv_refcount;
10472 }
10473}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010474
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010475/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010476 * "ch_info()" function
10477 */
10478 static void
10479f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10480{
Bram Moolenaar437905c2016-04-26 19:01:05 +020010481 channel_T *channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010482
10483 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10484 channel_info(channel, rettv->vval.v_dict);
10485}
10486
10487/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010488 * "ch_log()" function
10489 */
10490 static void
10491f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10492{
10493 char_u *msg = get_tv_string(&argvars[0]);
10494 channel_T *channel = NULL;
10495
10496 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar437905c2016-04-26 19:01:05 +020010497 channel = get_channel_arg(&argvars[1], FALSE, FALSE, 0);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010498
10499 ch_log(channel, (char *)msg);
10500}
10501
10502/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010503 * "ch_logfile()" function
10504 */
10505 static void
10506f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10507{
10508 char_u *fname;
10509 char_u *opt = (char_u *)"";
10510 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010511
10512 fname = get_tv_string(&argvars[0]);
10513 if (argvars[1].v_type == VAR_STRING)
10514 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010515 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010516}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010517
10518/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010519 * "ch_open()" function
10520 */
10521 static void
10522f_ch_open(typval_T *argvars, typval_T *rettv)
10523{
Bram Moolenaar77073442016-02-13 23:23:53 +010010524 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar38499922016-04-22 20:46:52 +020010525 if (check_restricted() || check_secure())
10526 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010527 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010528}
10529
10530/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010531 * "ch_read()" function
10532 */
10533 static void
10534f_ch_read(typval_T *argvars, typval_T *rettv)
10535{
10536 common_channel_read(argvars, rettv, FALSE);
10537}
10538
10539/*
10540 * "ch_readraw()" function
10541 */
10542 static void
10543f_ch_readraw(typval_T *argvars, typval_T *rettv)
10544{
10545 common_channel_read(argvars, rettv, TRUE);
10546}
10547
10548/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010549 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010550 */
10551 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010552f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10553{
10554 ch_expr_common(argvars, rettv, TRUE);
10555}
10556
10557/*
10558 * "ch_sendexpr()" function
10559 */
10560 static void
10561f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10562{
10563 ch_expr_common(argvars, rettv, FALSE);
10564}
10565
10566/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010567 * "ch_evalraw()" function
10568 */
10569 static void
10570f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10571{
10572 ch_raw_common(argvars, rettv, TRUE);
10573}
10574
10575/*
10576 * "ch_sendraw()" function
10577 */
10578 static void
10579f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10580{
10581 ch_raw_common(argvars, rettv, FALSE);
10582}
10583
10584/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010585 * "ch_setoptions()" function
10586 */
10587 static void
10588f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10589{
10590 channel_T *channel;
10591 jobopt_T opt;
10592
Bram Moolenaar437905c2016-04-26 19:01:05 +020010593 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010594 if (channel == NULL)
10595 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010596 clear_job_options(&opt);
10597 if (get_job_options(&argvars[1], &opt,
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020010598 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == OK)
10599 channel_set_options(channel, &opt);
10600 free_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010601}
10602
10603/*
10604 * "ch_status()" function
10605 */
10606 static void
10607f_ch_status(typval_T *argvars, typval_T *rettv)
10608{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010609 channel_T *channel;
10610
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010611 /* return an empty string by default */
10612 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010613 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010614
Bram Moolenaar437905c2016-04-26 19:01:05 +020010615 channel = get_channel_arg(&argvars[0], FALSE, FALSE, 0);
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010616 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010617}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010618#endif
10619
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010620/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010621 * "changenr()" function
10622 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010624f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010625{
10626 rettv->vval.v_number = curbuf->b_u_seq_cur;
10627}
10628
10629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 * "char2nr(string)" function
10631 */
10632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010633f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634{
10635#ifdef FEAT_MBYTE
10636 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010637 {
10638 int utf8 = 0;
10639
10640 if (argvars[1].v_type != VAR_UNKNOWN)
10641 utf8 = get_tv_number_chk(&argvars[1], NULL);
10642
10643 if (utf8)
10644 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10645 else
10646 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 else
10649#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010650 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651}
10652
10653/*
10654 * "cindent(lnum)" function
10655 */
10656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010657f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658{
10659#ifdef FEAT_CINDENT
10660 pos_T pos;
10661 linenr_T lnum;
10662
10663 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010664 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10666 {
10667 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669 curwin->w_cursor = pos;
10670 }
10671 else
10672#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010674}
10675
10676/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010677 * "clearmatches()" function
10678 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010680f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010681{
10682#ifdef FEAT_SEARCH_EXTRA
10683 clear_matches(curwin);
10684#endif
10685}
10686
10687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688 * "col(string)" function
10689 */
10690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010691f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
10693 colnr_T col = 0;
10694 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010695 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010696
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010697 fp = var2fpos(&argvars[0], FALSE, &fnum);
10698 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 {
10700 if (fp->col == MAXCOL)
10701 {
10702 /* '> can be MAXCOL, get the length of the line then */
10703 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010704 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 else
10706 col = MAXCOL;
10707 }
10708 else
10709 {
10710 col = fp->col + 1;
10711#ifdef FEAT_VIRTUALEDIT
10712 /* col(".") when the cursor is on the NUL at the end of the line
10713 * because of "coladd" can be seen as an extra column. */
10714 if (virtual_active() && fp == &curwin->w_cursor)
10715 {
10716 char_u *p = ml_get_cursor();
10717
10718 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10719 curwin->w_virtcol - curwin->w_cursor.coladd))
10720 {
10721# ifdef FEAT_MBYTE
10722 int l;
10723
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010724 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010725 col += l;
10726# else
10727 if (*p != NUL && p[1] == NUL)
10728 ++col;
10729# endif
10730 }
10731 }
10732#endif
10733 }
10734 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010735 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736}
10737
Bram Moolenaar572cb562005-08-05 21:35:02 +000010738#if defined(FEAT_INS_EXPAND)
10739/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010740 * "complete()" function
10741 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010743f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010744{
10745 int startcol;
10746
10747 if ((State & INSERT) == 0)
10748 {
10749 EMSG(_("E785: complete() can only be used in Insert mode"));
10750 return;
10751 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010752
10753 /* Check for undo allowed here, because if something was already inserted
10754 * the line was already saved for undo and this check isn't done. */
10755 if (!undo_allowed())
10756 return;
10757
Bram Moolenaarade00832006-03-10 21:46:58 +000010758 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10759 {
10760 EMSG(_(e_invarg));
10761 return;
10762 }
10763
10764 startcol = get_tv_number_chk(&argvars[0], NULL);
10765 if (startcol <= 0)
10766 return;
10767
10768 set_completion(startcol - 1, argvars[1].vval.v_list);
10769}
10770
10771/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010772 * "complete_add()" function
10773 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010775f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010776{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010777 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010778}
10779
10780/*
10781 * "complete_check()" function
10782 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010784f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010785{
10786 int saved = RedrawingDisabled;
10787
10788 RedrawingDisabled = 0;
10789 ins_compl_check_keys(0);
10790 rettv->vval.v_number = compl_interrupted;
10791 RedrawingDisabled = saved;
10792}
10793#endif
10794
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795/*
10796 * "confirm(message, buttons[, default [, type]])" function
10797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
10801#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10802 char_u *message;
10803 char_u *buttons = NULL;
10804 char_u buf[NUMBUFLEN];
10805 char_u buf2[NUMBUFLEN];
10806 int def = 1;
10807 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010808 char_u *typestr;
10809 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010811 message = get_tv_string_chk(&argvars[0]);
10812 if (message == NULL)
10813 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010814 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10817 if (buttons == NULL)
10818 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010819 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010822 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010824 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10825 if (typestr == NULL)
10826 error = TRUE;
10827 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010829 switch (TOUPPER_ASC(*typestr))
10830 {
10831 case 'E': type = VIM_ERROR; break;
10832 case 'Q': type = VIM_QUESTION; break;
10833 case 'I': type = VIM_INFO; break;
10834 case 'W': type = VIM_WARNING; break;
10835 case 'G': type = VIM_GENERIC; break;
10836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 }
10838 }
10839 }
10840 }
10841
10842 if (buttons == NULL || *buttons == NUL)
10843 buttons = (char_u *)_("&Ok");
10844
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010845 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010847 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848#endif
10849}
10850
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010851/*
10852 * "copy()" function
10853 */
10854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010855f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010856{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010857 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010858}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010860#ifdef FEAT_FLOAT
10861/*
10862 * "cos()" function
10863 */
10864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010865f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010866{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010867 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010868
10869 rettv->v_type = VAR_FLOAT;
10870 if (get_float_arg(argvars, &f) == OK)
10871 rettv->vval.v_float = cos(f);
10872 else
10873 rettv->vval.v_float = 0.0;
10874}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010875
10876/*
10877 * "cosh()" function
10878 */
10879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010880f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010881{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010882 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010883
10884 rettv->v_type = VAR_FLOAT;
10885 if (get_float_arg(argvars, &f) == OK)
10886 rettv->vval.v_float = cosh(f);
10887 else
10888 rettv->vval.v_float = 0.0;
10889}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010890#endif
10891
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010893 * "count()" function
10894 */
10895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010896f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010897{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010898 long n = 0;
10899 int ic = FALSE;
10900
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010902 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010903 listitem_T *li;
10904 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010906
Bram Moolenaare9a41262005-01-15 22:18:47 +000010907 if ((l = argvars[0].vval.v_list) != NULL)
10908 {
10909 li = l->lv_first;
10910 if (argvars[2].v_type != VAR_UNKNOWN)
10911 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010912 int error = FALSE;
10913
10914 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010915 if (argvars[3].v_type != VAR_UNKNOWN)
10916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010917 idx = get_tv_number_chk(&argvars[3], &error);
10918 if (!error)
10919 {
10920 li = list_find(l, idx);
10921 if (li == NULL)
10922 EMSGN(_(e_listidx), idx);
10923 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010925 if (error)
10926 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 }
10928
10929 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010930 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010931 ++n;
10932 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010933 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010934 else if (argvars[0].v_type == VAR_DICT)
10935 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010936 int todo;
10937 dict_T *d;
10938 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010939
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010940 if ((d = argvars[0].vval.v_dict) != NULL)
10941 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 int error = FALSE;
10943
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 if (argvars[2].v_type != VAR_UNKNOWN)
10945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010946 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010947 if (argvars[3].v_type != VAR_UNKNOWN)
10948 EMSG(_(e_invarg));
10949 }
10950
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010951 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010952 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010953 {
10954 if (!HASHITEM_EMPTY(hi))
10955 {
10956 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010957 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010958 ++n;
10959 }
10960 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 }
10962 }
10963 else
10964 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010965 rettv->vval.v_number = n;
10966}
10967
10968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10970 *
10971 * Checks the existence of a cscope connection.
10972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010974f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
10976#ifdef FEAT_CSCOPE
10977 int num = 0;
10978 char_u *dbpath = NULL;
10979 char_u *prepend = NULL;
10980 char_u buf[NUMBUFLEN];
10981
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010982 if (argvars[0].v_type != VAR_UNKNOWN
10983 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985 num = (int)get_tv_number(&argvars[0]);
10986 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010987 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 }
10990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010991 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992#endif
10993}
10994
10995/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010996 * "cursor(lnum, col)" function, or
10997 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010998 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010999 * Moves the cursor to the specified line and column.
11000 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011003f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004{
11005 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011006#ifdef FEAT_VIRTUALEDIT
11007 long coladd = 0;
11008#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011009 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011011 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011012 if (argvars[1].v_type == VAR_UNKNOWN)
11013 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011014 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011015 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011016
Bram Moolenaar493c1782014-05-28 14:34:46 +020011017 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011018 {
11019 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011020 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011021 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011022 line = pos.lnum;
11023 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011024#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011025 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011026#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011027 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011028 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011029 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011030 set_curswant = FALSE;
11031 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011032 }
11033 else
11034 {
11035 line = get_tv_lnum(argvars);
11036 col = get_tv_number_chk(&argvars[1], NULL);
11037#ifdef FEAT_VIRTUALEDIT
11038 if (argvars[2].v_type != VAR_UNKNOWN)
11039 coladd = get_tv_number_chk(&argvars[2], NULL);
11040#endif
11041 }
11042 if (line < 0 || col < 0
11043#ifdef FEAT_VIRTUALEDIT
11044 || coladd < 0
11045#endif
11046 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 if (line > 0)
11049 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011050 if (col > 0)
11051 curwin->w_cursor.col = col - 1;
11052#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011053 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054#endif
11055
11056 /* Make sure the cursor is in a valid position. */
11057 check_cursor();
11058#ifdef FEAT_MBYTE
11059 /* Correct cursor for multi-byte character. */
11060 if (has_mbyte)
11061 mb_adjust_cursor();
11062#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011063
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011064 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011065 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066}
11067
11068/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011069 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 */
11071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011072f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011074 int noref = 0;
11075
11076 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011077 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011078 if (noref < 0 || noref > 1)
11079 EMSG(_(e_invarg));
11080 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011081 {
11082 current_copyID += COPYID_INC;
11083 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011085}
11086
11087/*
11088 * "delete()" function
11089 */
11090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011091f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011093 char_u nbuf[NUMBUFLEN];
11094 char_u *name;
11095 char_u *flags;
11096
11097 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011098 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011099 return;
11100
11101 name = get_tv_string(&argvars[0]);
11102 if (name == NULL || *name == NUL)
11103 {
11104 EMSG(_(e_invarg));
11105 return;
11106 }
11107
11108 if (argvars[1].v_type != VAR_UNKNOWN)
11109 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011111 flags = (char_u *)"";
11112
11113 if (*flags == NUL)
11114 /* delete a file */
11115 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11116 else if (STRCMP(flags, "d") == 0)
11117 /* delete an empty directory */
11118 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11119 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011120 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011121 rettv->vval.v_number = delete_recursive(name);
11122 else
11123 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011124}
11125
11126/*
11127 * "did_filetype()" function
11128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011130f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131{
11132#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134#endif
11135}
11136
11137/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011138 * "diff_filler()" function
11139 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011141f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011142{
11143#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011144 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011145#endif
11146}
11147
11148/*
11149 * "diff_hlID()" function
11150 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011152f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011153{
11154#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011155 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011156 static linenr_T prev_lnum = 0;
11157 static int changedtick = 0;
11158 static int fnum = 0;
11159 static int change_start = 0;
11160 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011161 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011162 int filler_lines;
11163 int col;
11164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 if (lnum < 0) /* ignore type error in {lnum} arg */
11166 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011167 if (lnum != prev_lnum
11168 || changedtick != curbuf->b_changedtick
11169 || fnum != curbuf->b_fnum)
11170 {
11171 /* New line, buffer, change: need to get the values. */
11172 filler_lines = diff_check(curwin, lnum);
11173 if (filler_lines < 0)
11174 {
11175 if (filler_lines == -1)
11176 {
11177 change_start = MAXCOL;
11178 change_end = -1;
11179 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11180 hlID = HLF_ADD; /* added line */
11181 else
11182 hlID = HLF_CHD; /* changed line */
11183 }
11184 else
11185 hlID = HLF_ADD; /* added line */
11186 }
11187 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011188 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011189 prev_lnum = lnum;
11190 changedtick = curbuf->b_changedtick;
11191 fnum = curbuf->b_fnum;
11192 }
11193
11194 if (hlID == HLF_CHD || hlID == HLF_TXD)
11195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011196 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011197 if (col >= change_start && col <= change_end)
11198 hlID = HLF_TXD; /* changed text */
11199 else
11200 hlID = HLF_CHD; /* changed line */
11201 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011202 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011203#endif
11204}
11205
11206/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011207 * "empty({expr})" function
11208 */
11209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011210f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011211{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011212 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011213
11214 switch (argvars[0].v_type)
11215 {
11216 case VAR_STRING:
11217 case VAR_FUNC:
11218 n = argvars[0].vval.v_string == NULL
11219 || *argvars[0].vval.v_string == NUL;
11220 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011221 case VAR_PARTIAL:
11222 n = FALSE;
11223 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011224 case VAR_NUMBER:
11225 n = argvars[0].vval.v_number == 0;
11226 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011227 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011228#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011229 n = argvars[0].vval.v_float == 0.0;
11230 break;
11231#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011232 case VAR_LIST:
11233 n = argvars[0].vval.v_list == NULL
11234 || argvars[0].vval.v_list->lv_first == NULL;
11235 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 case VAR_DICT:
11237 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011238 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011239 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011240 case VAR_SPECIAL:
11241 n = argvars[0].vval.v_number != VVAL_TRUE;
11242 break;
11243
Bram Moolenaar835dc632016-02-07 14:27:38 +010011244 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011245#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011246 n = argvars[0].vval.v_job == NULL
11247 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11248 break;
11249#endif
11250 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010011251#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011252 n = argvars[0].vval.v_channel == NULL
11253 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011254 break;
11255#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011256 case VAR_UNKNOWN:
11257 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11258 n = TRUE;
11259 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011260 }
11261
11262 rettv->vval.v_number = n;
11263}
11264
11265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011266 * "escape({string}, {chars})" function
11267 */
11268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011269f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270{
11271 char_u buf[NUMBUFLEN];
11272
Bram Moolenaar758711c2005-02-02 23:11:38 +000011273 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11274 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276}
11277
11278/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279 * "eval()" function
11280 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011282f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011283{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011284 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011286 s = get_tv_string_chk(&argvars[0]);
11287 if (s != NULL)
11288 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011289
Bram Moolenaar615b9972015-01-14 17:15:05 +010011290 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11292 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011293 if (p != NULL && !aborting())
11294 EMSG2(_(e_invexpr2), p);
11295 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011296 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011297 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011298 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011299 else if (*s != NUL)
11300 EMSG(_(e_trailing));
11301}
11302
11303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 * "eventhandler()" function
11305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011307f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011309 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310}
11311
11312/*
11313 * "executable()" function
11314 */
11315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011316f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011317{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011318 char_u *name = get_tv_string(&argvars[0]);
11319
11320 /* Check in $PATH and also check directly if there is a directory name. */
11321 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11322 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011323}
11324
11325/*
11326 * "exepath()" function
11327 */
11328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011329f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011330{
11331 char_u *p = NULL;
11332
Bram Moolenaarb5971142015-03-21 17:32:19 +010011333 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011334 rettv->v_type = VAR_STRING;
11335 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336}
11337
11338/*
11339 * "exists()" function
11340 */
11341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011342f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011343{
11344 char_u *p;
11345 char_u *name;
11346 int n = FALSE;
11347 int len = 0;
11348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011350 if (*p == '$') /* environment variable */
11351 {
11352 /* first try "normal" environment variables (fast) */
11353 if (mch_getenv(p + 1) != NULL)
11354 n = TRUE;
11355 else
11356 {
11357 /* try expanding things like $VIM and ${HOME} */
11358 p = expand_env_save(p);
11359 if (p != NULL && *p != '$')
11360 n = TRUE;
11361 vim_free(p);
11362 }
11363 }
11364 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011366 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011367 if (*skipwhite(p) != NUL)
11368 n = FALSE; /* trailing garbage */
11369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 else if (*p == '*') /* internal or user defined function */
11371 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011372 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 }
11374 else if (*p == ':')
11375 {
11376 n = cmd_exists(p + 1);
11377 }
11378 else if (*p == '#')
11379 {
11380#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011381 if (p[1] == '#')
11382 n = autocmd_supported(p + 2);
11383 else
11384 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385#endif
11386 }
11387 else /* internal variable */
11388 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011389 char_u *tofree;
11390 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011392 /* get_name_len() takes care of expanding curly braces */
11393 name = p;
11394 len = get_name_len(&p, &tofree, TRUE, FALSE);
11395 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011397 if (tofree != NULL)
11398 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011399 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011400 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011402 /* handle d.key, l[idx], f(expr) */
11403 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11404 if (n)
11405 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 }
11407 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011408 if (*p != NUL)
11409 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011411 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412 }
11413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011417#ifdef FEAT_FLOAT
11418/*
11419 * "exp()" function
11420 */
11421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011422f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011423{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011424 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011425
11426 rettv->v_type = VAR_FLOAT;
11427 if (get_float_arg(argvars, &f) == OK)
11428 rettv->vval.v_float = exp(f);
11429 else
11430 rettv->vval.v_float = 0.0;
11431}
11432#endif
11433
Bram Moolenaar071d4272004-06-13 20:20:40 +000011434/*
11435 * "expand()" function
11436 */
11437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011438f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011439{
11440 char_u *s;
11441 int len;
11442 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011443 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011444 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011445 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011446 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011449 if (argvars[1].v_type != VAR_UNKNOWN
11450 && argvars[2].v_type != VAR_UNKNOWN
11451 && get_tv_number_chk(&argvars[2], &error)
11452 && !error)
11453 {
11454 rettv->v_type = VAR_LIST;
11455 rettv->vval.v_list = NULL;
11456 }
11457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011458 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 if (*s == '%' || *s == '#' || *s == '<')
11460 {
11461 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011462 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011464 if (rettv->v_type == VAR_LIST)
11465 {
11466 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11467 list_append_string(rettv->vval.v_list, result, -1);
11468 else
11469 vim_free(result);
11470 }
11471 else
11472 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474 else
11475 {
11476 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011477 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011478 if (argvars[1].v_type != VAR_UNKNOWN
11479 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011480 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011481 if (!error)
11482 {
11483 ExpandInit(&xpc);
11484 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011485 if (p_wic)
11486 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011487 if (rettv->v_type == VAR_STRING)
11488 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11489 options, WILD_ALL);
11490 else if (rettv_list_alloc(rettv) != FAIL)
11491 {
11492 int i;
11493
11494 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11495 for (i = 0; i < xpc.xp_numfiles; i++)
11496 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11497 ExpandCleanup(&xpc);
11498 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011499 }
11500 else
11501 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 }
11503}
11504
11505/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011506 * Go over all entries in "d2" and add them to "d1".
11507 * When "action" is "error" then a duplicate key is an error.
11508 * When "action" is "force" then a duplicate key is overwritten.
11509 * Otherwise duplicate keys are ignored ("action" is "keep").
11510 */
11511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011512dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011513{
11514 dictitem_T *di1;
11515 hashitem_T *hi2;
11516 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011517 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011518
11519 todo = (int)d2->dv_hashtab.ht_used;
11520 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11521 {
11522 if (!HASHITEM_EMPTY(hi2))
11523 {
11524 --todo;
11525 di1 = dict_find(d1, hi2->hi_key, -1);
11526 if (d1->dv_scope != 0)
11527 {
11528 /* Disallow replacing a builtin function in l: and g:.
11529 * Check the key to be valid when adding to any
11530 * scope. */
11531 if (d1->dv_scope == VAR_DEF_SCOPE
11532 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11533 && var_check_func_name(hi2->hi_key,
11534 di1 == NULL))
11535 break;
11536 if (!valid_varname(hi2->hi_key))
11537 break;
11538 }
11539 if (di1 == NULL)
11540 {
11541 di1 = dictitem_copy(HI2DI(hi2));
11542 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11543 dictitem_free(di1);
11544 }
11545 else if (*action == 'e')
11546 {
11547 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11548 break;
11549 }
11550 else if (*action == 'f' && HI2DI(hi2) != di1)
11551 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011552 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11553 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011554 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011555 clear_tv(&di1->di_tv);
11556 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11557 }
11558 }
11559 }
11560}
11561
11562/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011563 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011564 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011565 */
11566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011567f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011568{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011569 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011570
Bram Moolenaare9a41262005-01-15 22:18:47 +000011571 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011572 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 list_T *l1, *l2;
11574 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011575 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011576 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011577
Bram Moolenaare9a41262005-01-15 22:18:47 +000011578 l1 = argvars[0].vval.v_list;
11579 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011580 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011581 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011582 {
11583 if (argvars[2].v_type != VAR_UNKNOWN)
11584 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585 before = get_tv_number_chk(&argvars[2], &error);
11586 if (error)
11587 return; /* type error; errmsg already given */
11588
Bram Moolenaar758711c2005-02-02 23:11:38 +000011589 if (before == l1->lv_len)
11590 item = NULL;
11591 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011592 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011593 item = list_find(l1, before);
11594 if (item == NULL)
11595 {
11596 EMSGN(_(e_listidx), before);
11597 return;
11598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011599 }
11600 }
11601 else
11602 item = NULL;
11603 list_extend(l1, l2, item);
11604
Bram Moolenaare9a41262005-01-15 22:18:47 +000011605 copy_tv(&argvars[0], rettv);
11606 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011608 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11609 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011610 dict_T *d1, *d2;
11611 char_u *action;
11612 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011613
11614 d1 = argvars[0].vval.v_dict;
11615 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011616 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011617 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011618 {
11619 /* Check the third argument. */
11620 if (argvars[2].v_type != VAR_UNKNOWN)
11621 {
11622 static char *(av[]) = {"keep", "force", "error"};
11623
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 action = get_tv_string_chk(&argvars[2]);
11625 if (action == NULL)
11626 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011627 for (i = 0; i < 3; ++i)
11628 if (STRCMP(action, av[i]) == 0)
11629 break;
11630 if (i == 3)
11631 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011632 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011633 return;
11634 }
11635 }
11636 else
11637 action = (char_u *)"force";
11638
Bram Moolenaara9922d62013-05-30 13:01:18 +020011639 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011640
Bram Moolenaare9a41262005-01-15 22:18:47 +000011641 copy_tv(&argvars[0], rettv);
11642 }
11643 }
11644 else
11645 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011646}
11647
11648/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011649 * "feedkeys()" function
11650 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011652f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011653{
11654 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011655 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011656 char_u *keys, *flags;
11657 char_u nbuf[NUMBUFLEN];
11658 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011659 int execute = FALSE;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011660 int dangerous = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011661 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011662
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011663 /* This is not allowed in the sandbox. If the commands would still be
11664 * executed in the sandbox it would be OK, but it probably happens later,
11665 * when "sandbox" is no longer set. */
11666 if (check_secure())
11667 return;
11668
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011669 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011670
11671 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011672 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011673 flags = get_tv_string_buf(&argvars[1], nbuf);
11674 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011675 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011676 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011677 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011678 case 'n': remap = FALSE; break;
11679 case 'm': remap = TRUE; break;
11680 case 't': typed = TRUE; break;
11681 case 'i': insert = TRUE; break;
11682 case 'x': execute = TRUE; break;
Bram Moolenaar245c4102016-04-20 17:37:41 +020011683 case '!': dangerous = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011684 }
11685 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011686 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011687
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011688 if (*keys != NUL || execute)
11689 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011690 /* Need to escape K_SPECIAL and CSI before putting the string in the
11691 * typeahead buffer. */
11692 keys_esc = vim_strsave_escape_csi(keys);
11693 if (keys_esc != NULL)
11694 {
11695 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011696 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011697 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011698 if (vgetc_busy)
11699 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011700 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011701 {
11702 int save_msg_scroll = msg_scroll;
11703
11704 /* Avoid a 1 second delay when the keys start Insert mode. */
11705 msg_scroll = FALSE;
Bram Moolenaar9bd547a2016-04-01 21:00:48 +020011706
Bram Moolenaar245c4102016-04-20 17:37:41 +020011707 if (!dangerous)
11708 ++ex_normal_busy;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011709 exec_normal(TRUE);
Bram Moolenaar245c4102016-04-20 17:37:41 +020011710 if (!dangerous)
11711 --ex_normal_busy;
Bram Moolenaar9e496852016-03-11 19:31:47 +010011712 msg_scroll |= save_msg_scroll;
11713 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011714 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011715 }
11716}
11717
11718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 * "filereadable()" function
11720 */
11721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011722f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011723{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011724 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 char_u *p;
11726 int n;
11727
Bram Moolenaarc236c162008-07-13 17:41:49 +000011728#ifndef O_NONBLOCK
11729# define O_NONBLOCK 0
11730#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011732 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11733 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734 {
11735 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011736 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 }
11738 else
11739 n = FALSE;
11740
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011741 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742}
11743
11744/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011745 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 * rights to write into.
11747 */
11748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011749f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011751 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752}
11753
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011754 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011755findfilendir(
11756 typval_T *argvars UNUSED,
11757 typval_T *rettv,
11758 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011759{
11760#ifdef FEAT_SEARCHPATH
11761 char_u *fname;
11762 char_u *fresult = NULL;
11763 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11764 char_u *p;
11765 char_u pathbuf[NUMBUFLEN];
11766 int count = 1;
11767 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011768 int error = FALSE;
11769#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011770
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011771 rettv->vval.v_string = NULL;
11772 rettv->v_type = VAR_STRING;
11773
11774#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011775 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011776
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011777 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11780 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011781 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011782 else
11783 {
11784 if (*p != NUL)
11785 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011788 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011789 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011790 }
11791
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011792 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11793 error = TRUE;
11794
11795 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011796 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 do
11798 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011799 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011800 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 fresult = find_file_in_path_option(first ? fname : NULL,
11802 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011803 0, first, path,
11804 find_what,
11805 curbuf->b_ffname,
11806 find_what == FINDFILE_DIR
11807 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011809
11810 if (fresult != NULL && rettv->v_type == VAR_LIST)
11811 list_append_string(rettv->vval.v_list, fresult, -1);
11812
11813 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011814 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011815
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011816 if (rettv->v_type == VAR_STRING)
11817 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011818#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011819}
11820
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011821static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11822static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011823
11824/*
11825 * Implementation of map() and filter().
11826 */
11827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011828filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011829{
11830 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011831 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011832 listitem_T *li, *nli;
11833 list_T *l = NULL;
11834 dictitem_T *di;
11835 hashtab_T *ht;
11836 hashitem_T *hi;
11837 dict_T *d = NULL;
11838 typval_T save_val;
11839 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011840 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011841 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011842 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011843 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011844 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011845 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011846 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011847
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011849 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011850 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011851 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011852 return;
11853 }
11854 else if (argvars[0].v_type == VAR_DICT)
11855 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011856 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011857 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 return;
11859 }
11860 else
11861 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011862 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011863 return;
11864 }
11865
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011866 expr = get_tv_string_buf_chk(&argvars[1], buf);
11867 /* On type errors, the preceding call has already displayed an error
11868 * message. Avoid a misleading error message for an empty string that
11869 * was not passed as argument. */
11870 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011872 prepare_vimvar(VV_VAL, &save_val);
11873 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011874
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011875 /* We reset "did_emsg" to be able to detect whether an error
11876 * occurred during evaluation of the expression. */
11877 save_did_emsg = did_emsg;
11878 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011879
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011880 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011881 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011882 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011883 vimvars[VV_KEY].vv_type = VAR_STRING;
11884
11885 ht = &d->dv_hashtab;
11886 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011887 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011888 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011890 if (!HASHITEM_EMPTY(hi))
11891 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011892 int r;
11893
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011894 --todo;
11895 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011896 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011897 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11898 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011899 break;
11900 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011901 r = filter_map_one(&di->di_tv, expr, map, &rem);
11902 clear_tv(&vimvars[VV_KEY].vv_tv);
11903 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 break;
11905 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011906 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011907 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11908 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011909 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011910 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011911 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912 }
11913 }
11914 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011915 }
11916 else
11917 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011918 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920 for (li = l->lv_first; li != NULL; li = nli)
11921 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011922 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011923 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011924 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011925 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011926 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011927 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011928 break;
11929 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011931 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011932 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011933 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011934
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011935 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011936 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011937
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011938 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011939 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011940
11941 copy_tv(&argvars[0], rettv);
11942}
11943
11944 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011945filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011946{
Bram Moolenaar33570922005-01-25 22:26:29 +000011947 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011948 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011949 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011950
Bram Moolenaar33570922005-01-25 22:26:29 +000011951 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011952 s = expr;
11953 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011954 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011955 if (*s != NUL) /* check for trailing chars after expr */
11956 {
11957 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011958 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011959 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011960 }
11961 if (map)
11962 {
11963 /* map(): replace the list item value */
11964 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011965 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011966 *tv = rettv;
11967 }
11968 else
11969 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011970 int error = FALSE;
11971
Bram Moolenaare9a41262005-01-15 22:18:47 +000011972 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011975 /* On type error, nothing has been removed; return FAIL to stop the
11976 * loop. The error message was given by get_tv_number_chk(). */
11977 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011978 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011979 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011980 retval = OK;
11981theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011982 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011983 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011984}
11985
11986/*
11987 * "filter()" function
11988 */
11989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011990f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011991{
11992 filter_map(argvars, rettv, FALSE);
11993}
11994
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011996 * "finddir({fname}[, {path}[, {count}]])" function
11997 */
11998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011999f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012000{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012001 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002}
12003
12004/*
12005 * "findfile({fname}[, {path}[, {count}]])" function
12006 */
12007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012008f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012009{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012010 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012011}
12012
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012013#ifdef FEAT_FLOAT
12014/*
12015 * "float2nr({float})" function
12016 */
12017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012018f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012019{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012020 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012021
12022 if (get_float_arg(argvars, &f) == OK)
12023 {
12024 if (f < -0x7fffffff)
12025 rettv->vval.v_number = -0x7fffffff;
12026 else if (f > 0x7fffffff)
12027 rettv->vval.v_number = 0x7fffffff;
12028 else
12029 rettv->vval.v_number = (varnumber_T)f;
12030 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012031}
12032
12033/*
12034 * "floor({float})" function
12035 */
12036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012037f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012038{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012039 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012040
12041 rettv->v_type = VAR_FLOAT;
12042 if (get_float_arg(argvars, &f) == OK)
12043 rettv->vval.v_float = floor(f);
12044 else
12045 rettv->vval.v_float = 0.0;
12046}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012047
12048/*
12049 * "fmod()" function
12050 */
12051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012052f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012053{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012054 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012055
12056 rettv->v_type = VAR_FLOAT;
12057 if (get_float_arg(argvars, &fx) == OK
12058 && get_float_arg(&argvars[1], &fy) == OK)
12059 rettv->vval.v_float = fmod(fx, fy);
12060 else
12061 rettv->vval.v_float = 0.0;
12062}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012063#endif
12064
Bram Moolenaar0d660222005-01-07 21:51:51 +000012065/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012066 * "fnameescape({string})" function
12067 */
12068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012069f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012070{
12071 rettv->vval.v_string = vim_strsave_fnameescape(
12072 get_tv_string(&argvars[0]), FALSE);
12073 rettv->v_type = VAR_STRING;
12074}
12075
12076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012077 * "fnamemodify({fname}, {mods})" function
12078 */
12079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012080f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
12082 char_u *fname;
12083 char_u *mods;
12084 int usedlen = 0;
12085 int len;
12086 char_u *fbuf = NULL;
12087 char_u buf[NUMBUFLEN];
12088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 fname = get_tv_string_chk(&argvars[0]);
12090 mods = get_tv_string_buf_chk(&argvars[1], buf);
12091 if (fname == NULL || mods == NULL)
12092 fname = NULL;
12093 else
12094 {
12095 len = (int)STRLEN(fname);
12096 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012099 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012100 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012103 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 vim_free(fbuf);
12105}
12106
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012107static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108
12109/*
12110 * "foldclosed()" function
12111 */
12112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012113foldclosed_both(
12114 typval_T *argvars UNUSED,
12115 typval_T *rettv,
12116 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117{
12118#ifdef FEAT_FOLDING
12119 linenr_T lnum;
12120 linenr_T first, last;
12121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012122 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012123 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12124 {
12125 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12126 {
12127 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012128 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 return;
12132 }
12133 }
12134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012135 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012136}
12137
12138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012139 * "foldclosed()" function
12140 */
12141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012142f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143{
12144 foldclosed_both(argvars, rettv, FALSE);
12145}
12146
12147/*
12148 * "foldclosedend()" function
12149 */
12150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012151f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152{
12153 foldclosed_both(argvars, rettv, TRUE);
12154}
12155
12156/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 * "foldlevel()" function
12158 */
12159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012160f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161{
12162#ifdef FEAT_FOLDING
12163 linenr_T lnum;
12164
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169}
12170
12171/*
12172 * "foldtext()" function
12173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012175f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176{
12177#ifdef FEAT_FOLDING
12178 linenr_T lnum;
12179 char_u *s;
12180 char_u *r;
12181 int len;
12182 char *txt;
12183#endif
12184
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012185 rettv->v_type = VAR_STRING;
12186 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012188 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12189 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12190 <= curbuf->b_ml.ml_line_count
12191 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 {
12193 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012194 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12195 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012196 {
12197 if (!linewhite(lnum))
12198 break;
12199 ++lnum;
12200 }
12201
12202 /* Find interesting text in this line. */
12203 s = skipwhite(ml_get(lnum));
12204 /* skip C comment-start */
12205 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012208 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012209 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012210 {
12211 s = skipwhite(ml_get(lnum + 1));
12212 if (*s == '*')
12213 s = skipwhite(s + 1);
12214 }
12215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012216 txt = _("+-%s%3ld lines: ");
12217 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012218 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 + 20 /* for %3ld */
12220 + STRLEN(s))); /* concatenated */
12221 if (r != NULL)
12222 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012223 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12224 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12225 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226 len = (int)STRLEN(r);
12227 STRCAT(r, s);
12228 /* remove 'foldmarker' and 'commentstring' */
12229 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012230 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012231 }
12232 }
12233#endif
12234}
12235
12236/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012237 * "foldtextresult(lnum)" function
12238 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012240f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012241{
12242#ifdef FEAT_FOLDING
12243 linenr_T lnum;
12244 char_u *text;
12245 char_u buf[51];
12246 foldinfo_T foldinfo;
12247 int fold_count;
12248#endif
12249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012250 rettv->v_type = VAR_STRING;
12251 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012252#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012253 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012254 /* treat illegal types and illegal string values for {lnum} the same */
12255 if (lnum < 0)
12256 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012257 fold_count = foldedCount(curwin, lnum, &foldinfo);
12258 if (fold_count > 0)
12259 {
12260 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12261 &foldinfo, buf);
12262 if (text == buf)
12263 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012264 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012265 }
12266#endif
12267}
12268
12269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270 * "foreground()" function
12271 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012273f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012274{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275#ifdef FEAT_GUI
12276 if (gui.in_use)
12277 gui_mch_set_foreground();
12278#else
12279# ifdef WIN32
12280 win32_set_foreground();
12281# endif
12282#endif
12283}
12284
12285/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012286 * "function()" function
12287 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012289f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012290{
12291 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012292 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012293 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012294 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012295
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012296 if (argvars[0].v_type == VAR_FUNC)
12297 {
12298 /* function(MyFunc, [arg], dict) */
12299 s = argvars[0].vval.v_string;
12300 }
12301 else if (argvars[0].v_type == VAR_PARTIAL
12302 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012303 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012304 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012305 arg_pt = argvars[0].vval.v_partial;
12306 s = arg_pt->pt_name;
12307 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012308 else
12309 {
12310 /* function('MyFunc', [arg], dict) */
12311 s = get_tv_string(&argvars[0]);
12312 use_string = TRUE;
12313 }
12314
12315 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012316 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012317 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010012318 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
12319 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012320 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012321 else
12322 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012323 int dict_idx = 0;
12324 int arg_idx = 0;
12325 list_T *list = NULL;
12326
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012327 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012328 {
12329 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012330 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012331
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012332 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12333 * also be called from another script. Using trans_function_name()
12334 * would also work, but some plugins depend on the name being
12335 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012336 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012337 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
12338 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012339 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012340 STRCPY(name, sid_buf);
12341 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012342 }
12343 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012344 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012345 name = vim_strsave(s);
12346
12347 if (argvars[1].v_type != VAR_UNKNOWN)
12348 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012349 if (argvars[2].v_type != VAR_UNKNOWN)
12350 {
12351 /* function(name, [args], dict) */
12352 arg_idx = 1;
12353 dict_idx = 2;
12354 }
12355 else if (argvars[1].v_type == VAR_DICT)
12356 /* function(name, dict) */
12357 dict_idx = 1;
12358 else
12359 /* function(name, [args]) */
12360 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012361 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012362 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012363 if (argvars[dict_idx].v_type != VAR_DICT)
12364 {
12365 EMSG(_("E922: expected a dict"));
12366 vim_free(name);
12367 return;
12368 }
12369 if (argvars[dict_idx].vval.v_dict == NULL)
12370 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012371 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012372 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012373 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012374 if (argvars[arg_idx].v_type != VAR_LIST)
12375 {
12376 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12377 vim_free(name);
12378 return;
12379 }
12380 list = argvars[arg_idx].vval.v_list;
12381 if (list == NULL || list->lv_len == 0)
12382 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012383 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012384 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012385 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012386 {
12387 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012388
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012389 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012390 if (pt == NULL)
12391 vim_free(name);
12392 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012393 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012394 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012395 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012396 listitem_T *li;
12397 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012398 int arg_len = 0;
12399 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012400
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012401 if (arg_pt != NULL)
12402 arg_len = arg_pt->pt_argc;
12403 if (list != NULL)
12404 lv_len = list->lv_len;
12405 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012406 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012407 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012408 if (pt->pt_argv == NULL)
12409 {
12410 vim_free(pt);
12411 vim_free(name);
12412 return;
12413 }
12414 else
12415 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012416 for (i = 0; i < arg_len; i++)
12417 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12418 if (lv_len > 0)
12419 for (li = list->lv_first; li != NULL;
12420 li = li->li_next)
12421 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012422 }
12423 }
12424
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012425 /* For "function(dict.func, [], dict)" and "func" is a partial
12426 * use "dict". That is backwards compatible. */
12427 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012428 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012429 /* The dict is bound explicitly, pt_auto is FALSE. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012430 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12431 ++pt->pt_dict->dv_refcount;
12432 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012433 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012434 {
Bram Moolenaar1d429612016-05-24 15:44:17 +020012435 /* If the dict was bound automatically the result is also
12436 * bound automatically. */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012437 pt->pt_dict = arg_pt->pt_dict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020012438 pt->pt_auto = arg_pt->pt_auto;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012439 if (pt->pt_dict != NULL)
12440 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012441 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012442
12443 pt->pt_refcount = 1;
12444 pt->pt_name = name;
12445 func_ref(pt->pt_name);
12446 }
12447 rettv->v_type = VAR_PARTIAL;
12448 rettv->vval.v_partial = pt;
12449 }
12450 else
12451 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012452 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012453 rettv->v_type = VAR_FUNC;
12454 rettv->vval.v_string = name;
12455 func_ref(name);
12456 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012457 }
12458}
12459
12460/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012461 * "garbagecollect()" function
12462 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012464f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012465{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012466 /* This is postponed until we are back at the toplevel, because we may be
12467 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12468 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012469
12470 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12471 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012472}
12473
12474/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012475 * "get()" function
12476 */
12477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012478f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479{
Bram Moolenaar33570922005-01-25 22:26:29 +000012480 listitem_T *li;
12481 list_T *l;
12482 dictitem_T *di;
12483 dict_T *d;
12484 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485
Bram Moolenaare9a41262005-01-15 22:18:47 +000012486 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012488 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012490 int error = FALSE;
12491
12492 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12493 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012494 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012495 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012496 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012497 else if (argvars[0].v_type == VAR_DICT)
12498 {
12499 if ((d = argvars[0].vval.v_dict) != NULL)
12500 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012501 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012502 if (di != NULL)
12503 tv = &di->di_tv;
12504 }
12505 }
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012506 else if (argvars[0].v_type == VAR_PARTIAL || argvars[0].v_type == VAR_FUNC)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012507 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012508 partial_T *pt;
12509 partial_T fref_pt;
12510
12511 if (argvars[0].v_type == VAR_PARTIAL)
12512 pt = argvars[0].vval.v_partial;
12513 else
12514 {
12515 vim_memset(&fref_pt, 0, sizeof(fref_pt));
12516 fref_pt.pt_name = argvars[0].vval.v_string;
12517 pt = &fref_pt;
12518 }
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012519
12520 if (pt != NULL)
12521 {
12522 char_u *what = get_tv_string(&argvars[1]);
12523
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012524 if (STRCMP(what, "func") == 0 || STRCMP(what, "name") == 0)
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012525 {
Bram Moolenaar03e19a02016-05-24 22:29:49 +020012526 rettv->v_type = (*what == 'f' ? VAR_FUNC : VAR_STRING);
Bram Moolenaar2bbf8ef2016-05-24 18:37:12 +020012527 if (pt->pt_name == NULL)
12528 rettv->vval.v_string = NULL;
12529 else
12530 rettv->vval.v_string = vim_strsave(pt->pt_name);
12531 }
12532 else if (STRCMP(what, "dict") == 0)
12533 {
12534 rettv->v_type = VAR_DICT;
12535 rettv->vval.v_dict = pt->pt_dict;
12536 if (pt->pt_dict != NULL)
12537 ++pt->pt_dict->dv_refcount;
12538 }
12539 else if (STRCMP(what, "args") == 0)
12540 {
12541 rettv->v_type = VAR_LIST;
12542 if (rettv_list_alloc(rettv) == OK)
12543 {
12544 int i;
12545
12546 for (i = 0; i < pt->pt_argc; ++i)
12547 list_append_tv(rettv->vval.v_list, &pt->pt_argv[i]);
12548 }
12549 }
12550 else
12551 EMSG2(_(e_invarg2), what);
12552 return;
12553 }
12554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012555 else
12556 EMSG2(_(e_listdictarg), "get()");
12557
12558 if (tv == NULL)
12559 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012560 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012561 copy_tv(&argvars[2], rettv);
12562 }
12563 else
12564 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012565}
12566
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012567static 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 +000012568
12569/*
12570 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012571 * Return a range (from start to end) of lines in rettv from the specified
12572 * buffer.
12573 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012574 */
12575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012576get_buffer_lines(
12577 buf_T *buf,
12578 linenr_T start,
12579 linenr_T end,
12580 int retlist,
12581 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012582{
12583 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012584
Bram Moolenaar959a1432013-12-14 12:17:38 +010012585 rettv->v_type = VAR_STRING;
12586 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012587 if (retlist && rettv_list_alloc(rettv) == FAIL)
12588 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012589
12590 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12591 return;
12592
12593 if (!retlist)
12594 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012595 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12596 p = ml_get_buf(buf, start, FALSE);
12597 else
12598 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012599 rettv->vval.v_string = vim_strsave(p);
12600 }
12601 else
12602 {
12603 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012604 return;
12605
12606 if (start < 1)
12607 start = 1;
12608 if (end > buf->b_ml.ml_line_count)
12609 end = buf->b_ml.ml_line_count;
12610 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012611 if (list_append_string(rettv->vval.v_list,
12612 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012613 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012614 }
12615}
12616
12617/*
12618 * "getbufline()" function
12619 */
12620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012621f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012622{
12623 linenr_T lnum;
12624 linenr_T end;
12625 buf_T *buf;
12626
12627 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12628 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012629 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012630 --emsg_off;
12631
Bram Moolenaar661b1822005-07-28 22:36:45 +000012632 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012633 if (argvars[2].v_type == VAR_UNKNOWN)
12634 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012635 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012636 end = get_tv_lnum_buf(&argvars[2], buf);
12637
Bram Moolenaar342337a2005-07-21 21:11:17 +000012638 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012639}
12640
Bram Moolenaar0d660222005-01-07 21:51:51 +000012641/*
12642 * "getbufvar()" function
12643 */
12644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012645f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012646{
12647 buf_T *buf;
12648 buf_T *save_curbuf;
12649 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012650 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012651 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012653 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12654 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012655 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012656 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012657
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012658 rettv->v_type = VAR_STRING;
12659 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012660
12661 if (buf != NULL && varname != NULL)
12662 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012663 /* set curbuf to be our buf, temporarily */
12664 save_curbuf = curbuf;
12665 curbuf = buf;
12666
Bram Moolenaar0d660222005-01-07 21:51:51 +000012667 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012668 {
12669 if (get_option_tv(&varname, rettv, TRUE) == OK)
12670 done = TRUE;
12671 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012672 else if (STRCMP(varname, "changedtick") == 0)
12673 {
12674 rettv->v_type = VAR_NUMBER;
12675 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012676 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012677 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012678 else
12679 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012680 /* Look up the variable. */
12681 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12682 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12683 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012684 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012685 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012686 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012687 done = TRUE;
12688 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012689 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012690
12691 /* restore previous notion of curbuf */
12692 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012693 }
12694
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012695 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12696 /* use the default value */
12697 copy_tv(&argvars[2], rettv);
12698
Bram Moolenaar0d660222005-01-07 21:51:51 +000012699 --emsg_off;
12700}
12701
12702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 * "getchar()" function
12704 */
12705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012706f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707{
12708 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012709 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012711 /* Position the cursor. Needed after a message that ends in a space. */
12712 windgoto(msg_row, msg_col);
12713
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714 ++no_mapping;
12715 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012716 for (;;)
12717 {
12718 if (argvars[0].v_type == VAR_UNKNOWN)
12719 /* getchar(): blocking wait. */
12720 n = safe_vgetc();
12721 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12722 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012723 n = vpeekc_any();
12724 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012725 /* illegal argument or getchar(0) and no char avail: return zero */
12726 n = 0;
12727 else
12728 /* getchar(0) and char avail: return char */
12729 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012730
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012731 if (n == K_IGNORE)
12732 continue;
12733 break;
12734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 --no_mapping;
12736 --allow_keys;
12737
Bram Moolenaar219b8702006-11-01 14:32:36 +000012738 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12739 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12740 vimvars[VV_MOUSE_COL].vv_nr = 0;
12741
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012742 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 if (IS_SPECIAL(n) || mod_mask != 0)
12744 {
12745 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12746 int i = 0;
12747
12748 /* Turn a special key into three bytes, plus modifier. */
12749 if (mod_mask != 0)
12750 {
12751 temp[i++] = K_SPECIAL;
12752 temp[i++] = KS_MODIFIER;
12753 temp[i++] = mod_mask;
12754 }
12755 if (IS_SPECIAL(n))
12756 {
12757 temp[i++] = K_SPECIAL;
12758 temp[i++] = K_SECOND(n);
12759 temp[i++] = K_THIRD(n);
12760 }
12761#ifdef FEAT_MBYTE
12762 else if (has_mbyte)
12763 i += (*mb_char2bytes)(n, temp + i);
12764#endif
12765 else
12766 temp[i++] = n;
12767 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->v_type = VAR_STRING;
12769 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012770
12771#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012772 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012773 {
12774 int row = mouse_row;
12775 int col = mouse_col;
12776 win_T *win;
12777 linenr_T lnum;
12778# ifdef FEAT_WINDOWS
12779 win_T *wp;
12780# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012781 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012782
12783 if (row >= 0 && col >= 0)
12784 {
12785 /* Find the window at the mouse coordinates and compute the
12786 * text position. */
12787 win = mouse_find_win(&row, &col);
12788 (void)mouse_comp_pos(win, &row, &col, &lnum);
12789# ifdef FEAT_WINDOWS
12790 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012791 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012792# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012793 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012794 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12795 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12796 }
12797 }
12798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 }
12800}
12801
12802/*
12803 * "getcharmod()" function
12804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012806f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809}
12810
12811/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012812 * "getcharsearch()" function
12813 */
12814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012815f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012816{
12817 if (rettv_dict_alloc(rettv) != FAIL)
12818 {
12819 dict_T *dict = rettv->vval.v_dict;
12820
12821 dict_add_nr_str(dict, "char", 0L, last_csearch());
12822 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12823 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12824 }
12825}
12826
12827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 * "getcmdline()" function
12829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012831f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012832{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->v_type = VAR_STRING;
12834 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012835}
12836
12837/*
12838 * "getcmdpos()" function
12839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012841f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844}
12845
12846/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012847 * "getcmdtype()" function
12848 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012850f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012851{
12852 rettv->v_type = VAR_STRING;
12853 rettv->vval.v_string = alloc(2);
12854 if (rettv->vval.v_string != NULL)
12855 {
12856 rettv->vval.v_string[0] = get_cmdline_type();
12857 rettv->vval.v_string[1] = NUL;
12858 }
12859}
12860
12861/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012862 * "getcmdwintype()" function
12863 */
12864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012865f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012866{
12867 rettv->v_type = VAR_STRING;
12868 rettv->vval.v_string = NULL;
12869#ifdef FEAT_CMDWIN
12870 rettv->vval.v_string = alloc(2);
12871 if (rettv->vval.v_string != NULL)
12872 {
12873 rettv->vval.v_string[0] = cmdwin_type;
12874 rettv->vval.v_string[1] = NUL;
12875 }
12876#endif
12877}
12878
12879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 * "getcwd()" function
12881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012883f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012885 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012886 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012889 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012890
12891 wp = find_tabwin(&argvars[0], &argvars[1]);
12892 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012894 if (wp->w_localdir != NULL)
12895 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12896 else if(globaldir != NULL)
12897 rettv->vval.v_string = vim_strsave(globaldir);
12898 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012899 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012900 cwd = alloc(MAXPATHL);
12901 if (cwd != NULL)
12902 {
12903 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12904 rettv->vval.v_string = vim_strsave(cwd);
12905 vim_free(cwd);
12906 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012907 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012908#ifdef BACKSLASH_IN_FILENAME
12909 if (rettv->vval.v_string != NULL)
12910 slash_adjust(rettv->vval.v_string);
12911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 }
12913}
12914
12915/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012916 * "getfontname()" function
12917 */
12918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012919f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012920{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 rettv->v_type = VAR_STRING;
12922 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012923#ifdef FEAT_GUI
12924 if (gui.in_use)
12925 {
12926 GuiFont font;
12927 char_u *name = NULL;
12928
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012929 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012930 {
12931 /* Get the "Normal" font. Either the name saved by
12932 * hl_set_font_name() or from the font ID. */
12933 font = gui.norm_font;
12934 name = hl_get_font_name();
12935 }
12936 else
12937 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012939 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12940 return;
12941 font = gui_mch_get_font(name, FALSE);
12942 if (font == NOFONT)
12943 return; /* Invalid font name, return empty string. */
12944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012946 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012947 gui_mch_free_font(font);
12948 }
12949#endif
12950}
12951
12952/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012953 * "getfperm({fname})" function
12954 */
12955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012956f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012957{
12958 char_u *fname;
12959 struct stat st;
12960 char_u *perm = NULL;
12961 char_u flags[] = "rwx";
12962 int i;
12963
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012964 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012965
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012966 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012967 if (mch_stat((char *)fname, &st) >= 0)
12968 {
12969 perm = vim_strsave((char_u *)"---------");
12970 if (perm != NULL)
12971 {
12972 for (i = 0; i < 9; i++)
12973 {
12974 if (st.st_mode & (1 << (8 - i)))
12975 perm[i] = flags[i % 3];
12976 }
12977 }
12978 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012979 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012980}
12981
12982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 * "getfsize({fname})" function
12984 */
12985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012986f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987{
12988 char_u *fname;
12989 struct stat st;
12990
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012993 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994
12995 if (mch_stat((char *)fname, &st) >= 0)
12996 {
12997 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000013000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013001 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000013002
13003 /* non-perfect check for overflow */
13004 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
13005 rettv->vval.v_number = -2;
13006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013007 }
13008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013009 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013010}
13011
13012/*
13013 * "getftime({fname})" function
13014 */
13015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013016f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017{
13018 char_u *fname;
13019 struct stat st;
13020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013021 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013022
13023 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027}
13028
13029/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013030 * "getftype({fname})" function
13031 */
13032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013033f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013034{
13035 char_u *fname;
13036 struct stat st;
13037 char_u *type = NULL;
13038 char *t;
13039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013043 if (mch_lstat((char *)fname, &st) >= 0)
13044 {
13045#ifdef S_ISREG
13046 if (S_ISREG(st.st_mode))
13047 t = "file";
13048 else if (S_ISDIR(st.st_mode))
13049 t = "dir";
13050# ifdef S_ISLNK
13051 else if (S_ISLNK(st.st_mode))
13052 t = "link";
13053# endif
13054# ifdef S_ISBLK
13055 else if (S_ISBLK(st.st_mode))
13056 t = "bdev";
13057# endif
13058# ifdef S_ISCHR
13059 else if (S_ISCHR(st.st_mode))
13060 t = "cdev";
13061# endif
13062# ifdef S_ISFIFO
13063 else if (S_ISFIFO(st.st_mode))
13064 t = "fifo";
13065# endif
13066# ifdef S_ISSOCK
13067 else if (S_ISSOCK(st.st_mode))
13068 t = "fifo";
13069# endif
13070 else
13071 t = "other";
13072#else
13073# ifdef S_IFMT
13074 switch (st.st_mode & S_IFMT)
13075 {
13076 case S_IFREG: t = "file"; break;
13077 case S_IFDIR: t = "dir"; break;
13078# ifdef S_IFLNK
13079 case S_IFLNK: t = "link"; break;
13080# endif
13081# ifdef S_IFBLK
13082 case S_IFBLK: t = "bdev"; break;
13083# endif
13084# ifdef S_IFCHR
13085 case S_IFCHR: t = "cdev"; break;
13086# endif
13087# ifdef S_IFIFO
13088 case S_IFIFO: t = "fifo"; break;
13089# endif
13090# ifdef S_IFSOCK
13091 case S_IFSOCK: t = "socket"; break;
13092# endif
13093 default: t = "other";
13094 }
13095# else
13096 if (mch_isdir(fname))
13097 t = "dir";
13098 else
13099 t = "file";
13100# endif
13101#endif
13102 type = vim_strsave((char_u *)t);
13103 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013105}
13106
13107/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013108 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013109 */
13110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013111f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013112{
13113 linenr_T lnum;
13114 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013115 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013116
13117 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013118 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013119 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013120 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013121 retlist = FALSE;
13122 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013123 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013124 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013125 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013126 retlist = TRUE;
13127 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013128
Bram Moolenaar342337a2005-07-21 21:11:17 +000013129 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013130}
13131
13132/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013133 * "getmatches()" function
13134 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013136f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013137{
13138#ifdef FEAT_SEARCH_EXTRA
13139 dict_T *dict;
13140 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013141 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013142
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013143 if (rettv_list_alloc(rettv) == OK)
13144 {
13145 while (cur != NULL)
13146 {
13147 dict = dict_alloc();
13148 if (dict == NULL)
13149 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013150 if (cur->match.regprog == NULL)
13151 {
13152 /* match added with matchaddpos() */
13153 for (i = 0; i < MAXPOSMATCH; ++i)
13154 {
13155 llpos_T *llpos;
13156 char buf[6];
13157 list_T *l;
13158
13159 llpos = &cur->pos.pos[i];
13160 if (llpos->lnum == 0)
13161 break;
13162 l = list_alloc();
13163 if (l == NULL)
13164 break;
13165 list_append_number(l, (varnumber_T)llpos->lnum);
13166 if (llpos->col > 0)
13167 {
13168 list_append_number(l, (varnumber_T)llpos->col);
13169 list_append_number(l, (varnumber_T)llpos->len);
13170 }
13171 sprintf(buf, "pos%d", i + 1);
13172 dict_add_list(dict, buf, l);
13173 }
13174 }
13175 else
13176 {
13177 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13178 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013179 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013180 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13181 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar42356152016-03-31 22:27:40 +020013182# if defined(FEAT_CONCEAL) && defined(FEAT_MBYTE)
Bram Moolenaar6561d522015-07-21 15:48:27 +020013183 if (cur->conceal_char)
13184 {
13185 char_u buf[MB_MAXBYTES + 1];
13186
13187 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13188 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13189 }
13190# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013191 list_append_dict(rettv->vval.v_list, dict);
13192 cur = cur->next;
13193 }
13194 }
13195#endif
13196}
13197
13198/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013199 * "getpid()" function
13200 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013202f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013203{
13204 rettv->vval.v_number = mch_get_pid();
13205}
13206
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013208getpos_both(
13209 typval_T *argvars,
13210 typval_T *rettv,
13211 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013212{
Bram Moolenaara5525202006-03-02 22:52:09 +000013213 pos_T *fp;
13214 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013215 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013216
13217 if (rettv_list_alloc(rettv) == OK)
13218 {
13219 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013220 if (getcurpos)
13221 fp = &curwin->w_cursor;
13222 else
13223 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013224 if (fnum != -1)
13225 list_append_number(l, (varnumber_T)fnum);
13226 else
13227 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013228 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13229 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013230 list_append_number(l, (fp != NULL)
13231 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013232 : (varnumber_T)0);
13233 list_append_number(l,
13234#ifdef FEAT_VIRTUALEDIT
13235 (fp != NULL) ? (varnumber_T)fp->coladd :
13236#endif
13237 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013238 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013239 {
13240 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013241 list_append_number(l, curwin->w_curswant == MAXCOL ?
13242 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013243 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013244 }
13245 else
13246 rettv->vval.v_number = FALSE;
13247}
13248
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020013249
13250/*
13251 * "getcurpos()" function
13252 */
13253 static void
13254f_getcurpos(typval_T *argvars, typval_T *rettv)
13255{
13256 getpos_both(argvars, rettv, TRUE);
13257}
13258
13259/*
13260 * "getpos(string)" function
13261 */
13262 static void
13263f_getpos(typval_T *argvars, typval_T *rettv)
13264{
13265 getpos_both(argvars, rettv, FALSE);
13266}
13267
Bram Moolenaara5525202006-03-02 22:52:09 +000013268/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013269 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013270 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013272f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013273{
13274#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013275 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013276#endif
13277
Bram Moolenaar2641f772005-03-25 21:58:17 +000013278#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013279 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013280 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013281 wp = NULL;
13282 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13283 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013284 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013285 if (wp == NULL)
13286 return;
13287 }
13288
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013289 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013290 }
13291#endif
13292}
13293
13294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 * "getreg()" function
13296 */
13297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013298f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299{
13300 char_u *strregname;
13301 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013302 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013303 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013305
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013306 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013307 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013308 strregname = get_tv_string_chk(&argvars[0]);
13309 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013310 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013312 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013313 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13314 return_list = get_tv_number_chk(&argvars[2], &error);
13315 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013318 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013319
13320 if (error)
13321 return;
13322
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 regname = (strregname == NULL ? '"' : *strregname);
13324 if (regname == 0)
13325 regname = '"';
13326
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013327 if (return_list)
13328 {
13329 rettv->v_type = VAR_LIST;
13330 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13331 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013332 if (rettv->vval.v_list == NULL)
Bram Moolenaar8ed43912016-04-21 08:56:12 +020013333 (void)rettv_list_alloc(rettv);
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020013334 else
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013335 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013336 }
13337 else
13338 {
13339 rettv->v_type = VAR_STRING;
13340 rettv->vval.v_string = get_reg_contents(regname,
13341 arg2 ? GREG_EXPR_SRC : 0);
13342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343}
13344
13345/*
13346 * "getregtype()" function
13347 */
13348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013349f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013350{
13351 char_u *strregname;
13352 int regname;
13353 char_u buf[NUMBUFLEN + 2];
13354 long reglen = 0;
13355
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013356 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013357 {
13358 strregname = get_tv_string_chk(&argvars[0]);
13359 if (strregname == NULL) /* type error; errmsg already given */
13360 {
13361 rettv->v_type = VAR_STRING;
13362 rettv->vval.v_string = NULL;
13363 return;
13364 }
13365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 else
13367 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013368 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369
13370 regname = (strregname == NULL ? '"' : *strregname);
13371 if (regname == 0)
13372 regname = '"';
13373
13374 buf[0] = NUL;
13375 buf[1] = NUL;
13376 switch (get_reg_type(regname, &reglen))
13377 {
13378 case MLINE: buf[0] = 'V'; break;
13379 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013380 case MBLOCK:
13381 buf[0] = Ctrl_V;
13382 sprintf((char *)buf + 1, "%ld", reglen + 1);
13383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013385 rettv->v_type = VAR_STRING;
13386 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387}
13388
13389/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013390 * "gettabvar()" function
13391 */
13392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013393f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013394{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013395 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013396 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013397 dictitem_T *v;
13398 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013399 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013400
13401 rettv->v_type = VAR_STRING;
13402 rettv->vval.v_string = NULL;
13403
13404 varname = get_tv_string_chk(&argvars[1]);
13405 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13406 if (tp != NULL && varname != NULL)
13407 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013408 /* Set tp to be our tabpage, temporarily. Also set the window to the
13409 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013410 if (switch_win(&oldcurwin, &oldtabpage,
13411 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013412 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013413 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013414 /* look up the variable */
13415 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13416 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13417 if (v != NULL)
13418 {
13419 copy_tv(&v->di_tv, rettv);
13420 done = TRUE;
13421 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013422 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013423
13424 /* restore previous notion of curwin */
13425 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013426 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013427
13428 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013429 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013430}
13431
13432/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013433 * "gettabwinvar()" function
13434 */
13435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013436f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013437{
13438 getwinvar(argvars, rettv, 1);
13439}
13440
13441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 * "getwinposx()" function
13443 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013445f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013446{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013447 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448#ifdef FEAT_GUI
13449 if (gui.in_use)
13450 {
13451 int x, y;
13452
13453 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013454 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013455 }
13456#endif
13457}
13458
13459/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013460 * "win_findbuf()" function
13461 */
13462 static void
13463f_win_findbuf(typval_T *argvars, typval_T *rettv)
13464{
13465 if (rettv_list_alloc(rettv) != FAIL)
13466 win_findbuf(argvars, rettv->vval.v_list);
13467}
13468
13469/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013470 * "win_getid()" function
13471 */
13472 static void
13473f_win_getid(typval_T *argvars, typval_T *rettv)
13474{
13475 rettv->vval.v_number = win_getid(argvars);
13476}
13477
13478/*
13479 * "win_gotoid()" function
13480 */
13481 static void
13482f_win_gotoid(typval_T *argvars, typval_T *rettv)
13483{
13484 rettv->vval.v_number = win_gotoid(argvars);
13485}
13486
13487/*
13488 * "win_id2tabwin()" function
13489 */
13490 static void
13491f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13492{
13493 if (rettv_list_alloc(rettv) != FAIL)
13494 win_id2tabwin(argvars, rettv->vval.v_list);
13495}
13496
13497/*
13498 * "win_id2win()" function
13499 */
13500 static void
13501f_win_id2win(typval_T *argvars, typval_T *rettv)
13502{
13503 rettv->vval.v_number = win_id2win(argvars);
13504}
13505
13506/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 * "getwinposy()" function
13508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013510f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013513#ifdef FEAT_GUI
13514 if (gui.in_use)
13515 {
13516 int x, y;
13517
13518 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520 }
13521#endif
13522}
13523
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013524/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013525 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013526 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013527 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013528find_win_by_nr(
13529 typval_T *vp,
13530 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013531{
13532#ifdef FEAT_WINDOWS
13533 win_T *wp;
13534#endif
13535 int nr;
13536
13537 nr = get_tv_number_chk(vp, NULL);
13538
13539#ifdef FEAT_WINDOWS
13540 if (nr < 0)
13541 return NULL;
13542 if (nr == 0)
13543 return curwin;
13544
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013545 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13546 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013547 if (--nr <= 0)
13548 break;
13549 return wp;
13550#else
13551 if (nr == 0 || nr == 1)
13552 return curwin;
13553 return NULL;
13554#endif
13555}
13556
Bram Moolenaar071d4272004-06-13 20:20:40 +000013557/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013558 * Find window specified by "wvp" in tabpage "tvp".
13559 */
13560 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013561find_tabwin(
13562 typval_T *wvp, /* VAR_UNKNOWN for current window */
13563 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013564{
13565 win_T *wp = NULL;
13566 tabpage_T *tp = NULL;
13567 long n;
13568
13569 if (wvp->v_type != VAR_UNKNOWN)
13570 {
13571 if (tvp->v_type != VAR_UNKNOWN)
13572 {
13573 n = get_tv_number(tvp);
13574 if (n >= 0)
13575 tp = find_tabpage(n);
13576 }
13577 else
13578 tp = curtab;
13579
13580 if (tp != NULL)
13581 wp = find_win_by_nr(wvp, tp);
13582 }
13583 else
13584 wp = curwin;
13585
13586 return wp;
13587}
13588
13589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013590 * "getwinvar()" function
13591 */
13592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013593f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013595 getwinvar(argvars, rettv, 0);
13596}
13597
13598/*
13599 * getwinvar() and gettabwinvar()
13600 */
13601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013602getwinvar(
13603 typval_T *argvars,
13604 typval_T *rettv,
13605 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013606{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013607 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013609 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013610 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013611 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013612#ifdef FEAT_WINDOWS
13613 win_T *oldcurwin;
13614 tabpage_T *oldtabpage;
13615 int need_switch_win;
13616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013618#ifdef FEAT_WINDOWS
13619 if (off == 1)
13620 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13621 else
13622 tp = curtab;
13623#endif
13624 win = find_win_by_nr(&argvars[off], tp);
13625 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013628 rettv->v_type = VAR_STRING;
13629 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630
13631 if (win != NULL && varname != NULL)
13632 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013633#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013634 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013635 * otherwise the window is not valid. Only do this when needed,
13636 * autocommands get blocked. */
13637 need_switch_win = !(tp == curtab && win == curwin);
13638 if (!need_switch_win
13639 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13640#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013641 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013642 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013643 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013644 if (get_option_tv(&varname, rettv, 1) == OK)
13645 done = TRUE;
13646 }
13647 else
13648 {
13649 /* Look up the variable. */
13650 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13651 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13652 varname, FALSE);
13653 if (v != NULL)
13654 {
13655 copy_tv(&v->di_tv, rettv);
13656 done = TRUE;
13657 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013660
Bram Moolenaarba117c22015-09-29 16:53:22 +020013661#ifdef FEAT_WINDOWS
13662 if (need_switch_win)
13663 /* restore previous notion of curwin */
13664 restore_win(oldcurwin, oldtabpage, TRUE);
13665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013666 }
13667
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013668 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13669 /* use the default return value */
13670 copy_tv(&argvars[off + 2], rettv);
13671
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672 --emsg_off;
13673}
13674
13675/*
13676 * "glob()" function
13677 */
13678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013679f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013681 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013683 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013685 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013686 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013687 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013688 if (argvars[1].v_type != VAR_UNKNOWN)
13689 {
13690 if (get_tv_number_chk(&argvars[1], &error))
13691 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013692 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013693 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013694 if (get_tv_number_chk(&argvars[2], &error))
13695 {
13696 rettv->v_type = VAR_LIST;
13697 rettv->vval.v_list = NULL;
13698 }
13699 if (argvars[3].v_type != VAR_UNKNOWN
13700 && get_tv_number_chk(&argvars[3], &error))
13701 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013702 }
13703 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013704 if (!error)
13705 {
13706 ExpandInit(&xpc);
13707 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013708 if (p_wic)
13709 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013710 if (rettv->v_type == VAR_STRING)
13711 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013712 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013713 else if (rettv_list_alloc(rettv) != FAIL)
13714 {
13715 int i;
13716
13717 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13718 NULL, options, WILD_ALL_KEEP);
13719 for (i = 0; i < xpc.xp_numfiles; i++)
13720 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13721
13722 ExpandCleanup(&xpc);
13723 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013724 }
13725 else
13726 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727}
13728
13729/*
13730 * "globpath()" function
13731 */
13732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013733f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013735 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013736 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013737 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013738 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013739 garray_T ga;
13740 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013742 /* When the optional second argument is non-zero, don't remove matches
13743 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013745 if (argvars[2].v_type != VAR_UNKNOWN)
13746 {
13747 if (get_tv_number_chk(&argvars[2], &error))
13748 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013749 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013750 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013751 if (get_tv_number_chk(&argvars[3], &error))
13752 {
13753 rettv->v_type = VAR_LIST;
13754 rettv->vval.v_list = NULL;
13755 }
13756 if (argvars[4].v_type != VAR_UNKNOWN
13757 && get_tv_number_chk(&argvars[4], &error))
13758 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013759 }
13760 }
13761 if (file != NULL && !error)
13762 {
13763 ga_init2(&ga, (int)sizeof(char_u *), 10);
13764 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13765 if (rettv->v_type == VAR_STRING)
13766 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13767 else if (rettv_list_alloc(rettv) != FAIL)
13768 for (i = 0; i < ga.ga_len; ++i)
13769 list_append_string(rettv->vval.v_list,
13770 ((char_u **)(ga.ga_data))[i], -1);
13771 ga_clear_strings(&ga);
13772 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013773 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013774 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775}
13776
13777/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013778 * "glob2regpat()" function
13779 */
13780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013781f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013782{
13783 char_u *pat = get_tv_string_chk(&argvars[0]);
13784
13785 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013786 rettv->vval.v_string = (pat == NULL)
13787 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013788}
13789
13790/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791 * "has()" function
13792 */
13793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013794f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795{
13796 int i;
13797 char_u *name;
13798 int n = FALSE;
13799 static char *(has_list[]) =
13800 {
13801#ifdef AMIGA
13802 "amiga",
13803# ifdef FEAT_ARP
13804 "arp",
13805# endif
13806#endif
13807#ifdef __BEOS__
13808 "beos",
13809#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013810#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 "mac",
13812#endif
13813#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013814 "macunix", /* built with 'darwin' enabled */
13815#endif
13816#if defined(__APPLE__) && __APPLE__ == 1
13817 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#ifdef __QNX__
13820 "qnx",
13821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822#ifdef UNIX
13823 "unix",
13824#endif
13825#ifdef VMS
13826 "vms",
13827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828#ifdef WIN32
13829 "win32",
13830#endif
13831#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13832 "win32unix",
13833#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013834#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 "win64",
13836#endif
13837#ifdef EBCDIC
13838 "ebcdic",
13839#endif
13840#ifndef CASE_INSENSITIVE_FILENAME
13841 "fname_case",
13842#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013843#ifdef HAVE_ACL
13844 "acl",
13845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846#ifdef FEAT_ARABIC
13847 "arabic",
13848#endif
13849#ifdef FEAT_AUTOCMD
13850 "autocmd",
13851#endif
13852#ifdef FEAT_BEVAL
13853 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013854# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13855 "balloon_multiline",
13856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013857#endif
13858#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13859 "builtin_terms",
13860# ifdef ALL_BUILTIN_TCAPS
13861 "all_builtin_terms",
13862# endif
13863#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013864#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13865 || defined(FEAT_GUI_W32) \
13866 || defined(FEAT_GUI_MOTIF))
13867 "browsefilter",
13868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869#ifdef FEAT_BYTEOFF
13870 "byte_offset",
13871#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013872#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013873 "channel",
13874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875#ifdef FEAT_CINDENT
13876 "cindent",
13877#endif
13878#ifdef FEAT_CLIENTSERVER
13879 "clientserver",
13880#endif
13881#ifdef FEAT_CLIPBOARD
13882 "clipboard",
13883#endif
13884#ifdef FEAT_CMDL_COMPL
13885 "cmdline_compl",
13886#endif
13887#ifdef FEAT_CMDHIST
13888 "cmdline_hist",
13889#endif
13890#ifdef FEAT_COMMENTS
13891 "comments",
13892#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013893#ifdef FEAT_CONCEAL
13894 "conceal",
13895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013896#ifdef FEAT_CRYPT
13897 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013898 "crypt-blowfish",
13899 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013900#endif
13901#ifdef FEAT_CSCOPE
13902 "cscope",
13903#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013904#ifdef FEAT_CURSORBIND
13905 "cursorbind",
13906#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013907#ifdef CURSOR_SHAPE
13908 "cursorshape",
13909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910#ifdef DEBUG
13911 "debug",
13912#endif
13913#ifdef FEAT_CON_DIALOG
13914 "dialog_con",
13915#endif
13916#ifdef FEAT_GUI_DIALOG
13917 "dialog_gui",
13918#endif
13919#ifdef FEAT_DIFF
13920 "diff",
13921#endif
13922#ifdef FEAT_DIGRAPHS
13923 "digraphs",
13924#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013925#ifdef FEAT_DIRECTX
13926 "directx",
13927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013928#ifdef FEAT_DND
13929 "dnd",
13930#endif
13931#ifdef FEAT_EMACS_TAGS
13932 "emacs_tags",
13933#endif
13934 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013935 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936#ifdef FEAT_SEARCH_EXTRA
13937 "extra_search",
13938#endif
13939#ifdef FEAT_FKMAP
13940 "farsi",
13941#endif
13942#ifdef FEAT_SEARCHPATH
13943 "file_in_path",
13944#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013945#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013946 "filterpipe",
13947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013948#ifdef FEAT_FIND_ID
13949 "find_in_path",
13950#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013951#ifdef FEAT_FLOAT
13952 "float",
13953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954#ifdef FEAT_FOLDING
13955 "folding",
13956#endif
13957#ifdef FEAT_FOOTER
13958 "footer",
13959#endif
13960#if !defined(USE_SYSTEM) && defined(UNIX)
13961 "fork",
13962#endif
13963#ifdef FEAT_GETTEXT
13964 "gettext",
13965#endif
13966#ifdef FEAT_GUI
13967 "gui",
13968#endif
13969#ifdef FEAT_GUI_ATHENA
13970# ifdef FEAT_GUI_NEXTAW
13971 "gui_neXtaw",
13972# else
13973 "gui_athena",
13974# endif
13975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976#ifdef FEAT_GUI_GTK
13977 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013978# ifdef USE_GTK3
13979 "gui_gtk3",
13980# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013984#ifdef FEAT_GUI_GNOME
13985 "gui_gnome",
13986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987#ifdef FEAT_GUI_MAC
13988 "gui_mac",
13989#endif
13990#ifdef FEAT_GUI_MOTIF
13991 "gui_motif",
13992#endif
13993#ifdef FEAT_GUI_PHOTON
13994 "gui_photon",
13995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013996#ifdef FEAT_GUI_W32
13997 "gui_win32",
13998#endif
13999#ifdef FEAT_HANGULIN
14000 "hangul_input",
14001#endif
14002#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
14003 "iconv",
14004#endif
14005#ifdef FEAT_INS_EXPAND
14006 "insert_expand",
14007#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014008#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014009 "job",
14010#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011#ifdef FEAT_JUMPLIST
14012 "jumplist",
14013#endif
14014#ifdef FEAT_KEYMAP
14015 "keymap",
14016#endif
14017#ifdef FEAT_LANGMAP
14018 "langmap",
14019#endif
14020#ifdef FEAT_LIBCALL
14021 "libcall",
14022#endif
14023#ifdef FEAT_LINEBREAK
14024 "linebreak",
14025#endif
14026#ifdef FEAT_LISP
14027 "lispindent",
14028#endif
14029#ifdef FEAT_LISTCMDS
14030 "listcmds",
14031#endif
14032#ifdef FEAT_LOCALMAP
14033 "localmap",
14034#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014035#ifdef FEAT_LUA
14036# ifndef DYNAMIC_LUA
14037 "lua",
14038# endif
14039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040#ifdef FEAT_MENU
14041 "menu",
14042#endif
14043#ifdef FEAT_SESSION
14044 "mksession",
14045#endif
14046#ifdef FEAT_MODIFY_FNAME
14047 "modify_fname",
14048#endif
14049#ifdef FEAT_MOUSE
14050 "mouse",
14051#endif
14052#ifdef FEAT_MOUSESHAPE
14053 "mouseshape",
14054#endif
14055#if defined(UNIX) || defined(VMS)
14056# ifdef FEAT_MOUSE_DEC
14057 "mouse_dec",
14058# endif
14059# ifdef FEAT_MOUSE_GPM
14060 "mouse_gpm",
14061# endif
14062# ifdef FEAT_MOUSE_JSB
14063 "mouse_jsbterm",
14064# endif
14065# ifdef FEAT_MOUSE_NET
14066 "mouse_netterm",
14067# endif
14068# ifdef FEAT_MOUSE_PTERM
14069 "mouse_pterm",
14070# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014071# ifdef FEAT_MOUSE_SGR
14072 "mouse_sgr",
14073# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014074# ifdef FEAT_SYSMOUSE
14075 "mouse_sysmouse",
14076# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020014077# ifdef FEAT_MOUSE_URXVT
14078 "mouse_urxvt",
14079# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080# ifdef FEAT_MOUSE_XTERM
14081 "mouse_xterm",
14082# endif
14083#endif
14084#ifdef FEAT_MBYTE
14085 "multi_byte",
14086#endif
14087#ifdef FEAT_MBYTE_IME
14088 "multi_byte_ime",
14089#endif
14090#ifdef FEAT_MULTI_LANG
14091 "multi_lang",
14092#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014093#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000014094#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000014095 "mzscheme",
14096#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014098#ifdef FEAT_OLE
14099 "ole",
14100#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010014101 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102#ifdef FEAT_PATH_EXTRA
14103 "path_extra",
14104#endif
14105#ifdef FEAT_PERL
14106#ifndef DYNAMIC_PERL
14107 "perl",
14108#endif
14109#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020014110#ifdef FEAT_PERSISTENT_UNDO
14111 "persistent_undo",
14112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113#ifdef FEAT_PYTHON
14114#ifndef DYNAMIC_PYTHON
14115 "python",
14116#endif
14117#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014118#ifdef FEAT_PYTHON3
14119#ifndef DYNAMIC_PYTHON3
14120 "python3",
14121#endif
14122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123#ifdef FEAT_POSTSCRIPT
14124 "postscript",
14125#endif
14126#ifdef FEAT_PRINTER
14127 "printer",
14128#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000014129#ifdef FEAT_PROFILE
14130 "profile",
14131#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014132#ifdef FEAT_RELTIME
14133 "reltime",
14134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135#ifdef FEAT_QUICKFIX
14136 "quickfix",
14137#endif
14138#ifdef FEAT_RIGHTLEFT
14139 "rightleft",
14140#endif
14141#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14142 "ruby",
14143#endif
14144#ifdef FEAT_SCROLLBIND
14145 "scrollbind",
14146#endif
14147#ifdef FEAT_CMDL_INFO
14148 "showcmd",
14149 "cmdline_info",
14150#endif
14151#ifdef FEAT_SIGNS
14152 "signs",
14153#endif
14154#ifdef FEAT_SMARTINDENT
14155 "smartindent",
14156#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014157#ifdef STARTUPTIME
14158 "startuptime",
14159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160#ifdef FEAT_STL_OPT
14161 "statusline",
14162#endif
14163#ifdef FEAT_SUN_WORKSHOP
14164 "sun_workshop",
14165#endif
14166#ifdef FEAT_NETBEANS_INTG
14167 "netbeans_intg",
14168#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014169#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014170 "spell",
14171#endif
14172#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 "syntax",
14174#endif
14175#if defined(USE_SYSTEM) || !defined(UNIX)
14176 "system",
14177#endif
14178#ifdef FEAT_TAG_BINS
14179 "tag_binary",
14180#endif
14181#ifdef FEAT_TAG_OLDSTATIC
14182 "tag_old_static",
14183#endif
14184#ifdef FEAT_TAG_ANYWHITE
14185 "tag_any_white",
14186#endif
14187#ifdef FEAT_TCL
14188# ifndef DYNAMIC_TCL
14189 "tcl",
14190# endif
14191#endif
Bram Moolenaar61be73b2016-04-29 22:59:22 +020014192#ifdef FEAT_TERMGUICOLORS
14193 "termguicolors",
14194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195#ifdef TERMINFO
14196 "terminfo",
14197#endif
14198#ifdef FEAT_TERMRESPONSE
14199 "termresponse",
14200#endif
14201#ifdef FEAT_TEXTOBJ
14202 "textobjects",
14203#endif
14204#ifdef HAVE_TGETENT
14205 "tgetent",
14206#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010014207#ifdef FEAT_TIMERS
14208 "timers",
14209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014210#ifdef FEAT_TITLE
14211 "title",
14212#endif
14213#ifdef FEAT_TOOLBAR
14214 "toolbar",
14215#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014216#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14217 "unnamedplus",
14218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014219#ifdef FEAT_USR_CMDS
14220 "user-commands", /* was accidentally included in 5.4 */
14221 "user_commands",
14222#endif
14223#ifdef FEAT_VIMINFO
14224 "viminfo",
14225#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010014226#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 "vertsplit",
14228#endif
14229#ifdef FEAT_VIRTUALEDIT
14230 "virtualedit",
14231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233#ifdef FEAT_VISUALEXTRA
14234 "visualextra",
14235#endif
14236#ifdef FEAT_VREPLACE
14237 "vreplace",
14238#endif
14239#ifdef FEAT_WILDIGN
14240 "wildignore",
14241#endif
14242#ifdef FEAT_WILDMENU
14243 "wildmenu",
14244#endif
14245#ifdef FEAT_WINDOWS
14246 "windows",
14247#endif
14248#ifdef FEAT_WAK
14249 "winaltkeys",
14250#endif
14251#ifdef FEAT_WRITEBACKUP
14252 "writebackup",
14253#endif
14254#ifdef FEAT_XIM
14255 "xim",
14256#endif
14257#ifdef FEAT_XFONTSET
14258 "xfontset",
14259#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014260#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014261 "xpm",
14262 "xpm_w32", /* for backward compatibility */
14263#else
14264# if defined(HAVE_XPM)
14265 "xpm",
14266# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268#ifdef USE_XSMP
14269 "xsmp",
14270#endif
14271#ifdef USE_XSMP_INTERACT
14272 "xsmp_interact",
14273#endif
14274#ifdef FEAT_XCLIPBOARD
14275 "xterm_clipboard",
14276#endif
14277#ifdef FEAT_XTERM_SAVE
14278 "xterm_save",
14279#endif
14280#if defined(UNIX) && defined(FEAT_X11)
14281 "X11",
14282#endif
14283 NULL
14284 };
14285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014286 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 for (i = 0; has_list[i] != NULL; ++i)
14288 if (STRICMP(name, has_list[i]) == 0)
14289 {
14290 n = TRUE;
14291 break;
14292 }
14293
14294 if (n == FALSE)
14295 {
14296 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014297 {
14298 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010014299 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014300 && vim_isdigit(name[6])
14301 && vim_isdigit(name[8])
14302 && vim_isdigit(name[10]))
14303 {
14304 int major = atoi((char *)name + 6);
14305 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014306
14307 /* Expect "patch-9.9.01234". */
14308 n = (major < VIM_VERSION_MAJOR
14309 || (major == VIM_VERSION_MAJOR
14310 && (minor < VIM_VERSION_MINOR
14311 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014312 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014313 }
14314 else
14315 n = has_patch(atoi((char *)name + 5));
14316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317 else if (STRICMP(name, "vim_starting") == 0)
14318 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014319#ifdef FEAT_MBYTE
14320 else if (STRICMP(name, "multi_byte_encoding") == 0)
14321 n = has_mbyte;
14322#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014323#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14324 else if (STRICMP(name, "balloon_multiline") == 0)
14325 n = multiline_balloon_available();
14326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327#ifdef DYNAMIC_TCL
14328 else if (STRICMP(name, "tcl") == 0)
14329 n = tcl_enabled(FALSE);
14330#endif
14331#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14332 else if (STRICMP(name, "iconv") == 0)
14333 n = iconv_enabled(FALSE);
14334#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014335#ifdef DYNAMIC_LUA
14336 else if (STRICMP(name, "lua") == 0)
14337 n = lua_enabled(FALSE);
14338#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014339#ifdef DYNAMIC_MZSCHEME
14340 else if (STRICMP(name, "mzscheme") == 0)
14341 n = mzscheme_enabled(FALSE);
14342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343#ifdef DYNAMIC_RUBY
14344 else if (STRICMP(name, "ruby") == 0)
14345 n = ruby_enabled(FALSE);
14346#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014347#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348#ifdef DYNAMIC_PYTHON
14349 else if (STRICMP(name, "python") == 0)
14350 n = python_enabled(FALSE);
14351#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014352#endif
14353#ifdef FEAT_PYTHON3
14354#ifdef DYNAMIC_PYTHON3
14355 else if (STRICMP(name, "python3") == 0)
14356 n = python3_enabled(FALSE);
14357#endif
14358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359#ifdef DYNAMIC_PERL
14360 else if (STRICMP(name, "perl") == 0)
14361 n = perl_enabled(FALSE);
14362#endif
14363#ifdef FEAT_GUI
14364 else if (STRICMP(name, "gui_running") == 0)
14365 n = (gui.in_use || gui.starting);
14366# ifdef FEAT_GUI_W32
14367 else if (STRICMP(name, "gui_win32s") == 0)
14368 n = gui_is_win32s();
14369# endif
14370# ifdef FEAT_BROWSE
14371 else if (STRICMP(name, "browse") == 0)
14372 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14373# endif
14374#endif
14375#ifdef FEAT_SYN_HL
14376 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014377 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378#endif
14379#if defined(WIN3264)
14380 else if (STRICMP(name, "win95") == 0)
14381 n = mch_windows95();
14382#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014383#ifdef FEAT_NETBEANS_INTG
14384 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014385 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 }
14388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390}
14391
14392/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014393 * "has_key()" function
14394 */
14395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014396f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014397{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014398 if (argvars[0].v_type != VAR_DICT)
14399 {
14400 EMSG(_(e_dictreq));
14401 return;
14402 }
14403 if (argvars[0].vval.v_dict == NULL)
14404 return;
14405
14406 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014407 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014408}
14409
14410/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014411 * "haslocaldir()" function
14412 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014414f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014415{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014416 win_T *wp = NULL;
14417
14418 wp = find_tabwin(&argvars[0], &argvars[1]);
14419 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014420}
14421
14422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 * "hasmapto()" function
14424 */
14425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014426f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427{
14428 char_u *name;
14429 char_u *mode;
14430 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014431 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014434 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 mode = (char_u *)"nvo";
14436 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014437 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014438 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014439 if (argvars[2].v_type != VAR_UNKNOWN)
14440 abbr = get_tv_number(&argvars[2]);
14441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014442
Bram Moolenaar2c932302006-03-18 21:42:09 +000014443 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014444 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014446 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014447}
14448
14449/*
14450 * "histadd()" function
14451 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014453f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014454{
14455#ifdef FEAT_CMDHIST
14456 int histype;
14457 char_u *str;
14458 char_u buf[NUMBUFLEN];
14459#endif
14460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014461 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462 if (check_restricted() || check_secure())
14463 return;
14464#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014465 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14466 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 if (histype >= 0)
14468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014469 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470 if (*str != NUL)
14471 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014472 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 return;
14476 }
14477 }
14478#endif
14479}
14480
14481/*
14482 * "histdel()" function
14483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014485f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486{
14487#ifdef FEAT_CMDHIST
14488 int n;
14489 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014492 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14493 if (str == NULL)
14494 n = 0;
14495 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014498 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014501 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 else
14503 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014504 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014505 get_tv_string_buf(&argvars[1], buf));
14506 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507#endif
14508}
14509
14510/*
14511 * "histget()" function
14512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014514f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515{
14516#ifdef FEAT_CMDHIST
14517 int type;
14518 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014519 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014521 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14522 if (str == NULL)
14523 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014525 {
14526 type = get_histtype(str);
14527 if (argvars[1].v_type == VAR_UNKNOWN)
14528 idx = get_history_idx(type);
14529 else
14530 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14531 /* -1 on type error */
14532 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014535 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014536#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538}
14539
14540/*
14541 * "histnr()" function
14542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014544f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014545{
14546 int i;
14547
14548#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 char_u *history = get_tv_string_chk(&argvars[0]);
14550
14551 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552 if (i >= HIST_CMD && i < HIST_COUNT)
14553 i = get_history_idx(i);
14554 else
14555#endif
14556 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014557 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558}
14559
14560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561 * "highlightID(name)" function
14562 */
14563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014564f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014566 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014567}
14568
14569/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014570 * "highlight_exists()" function
14571 */
14572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014573f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014574{
14575 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14576}
14577
14578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014579 * "hostname()" function
14580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014582f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583{
14584 char_u hostname[256];
14585
14586 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587 rettv->v_type = VAR_STRING;
14588 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589}
14590
14591/*
14592 * iconv() function
14593 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014595f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
14597#ifdef FEAT_MBYTE
14598 char_u buf1[NUMBUFLEN];
14599 char_u buf2[NUMBUFLEN];
14600 char_u *from, *to, *str;
14601 vimconv_T vimconv;
14602#endif
14603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->v_type = VAR_STRING;
14605 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606
14607#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014608 str = get_tv_string(&argvars[0]);
14609 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14610 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611 vimconv.vc_type = CONV_NONE;
14612 convert_setup(&vimconv, from, to);
14613
14614 /* If the encodings are equal, no conversion needed. */
14615 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014616 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014618 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014619
14620 convert_setup(&vimconv, NULL, NULL);
14621 vim_free(from);
14622 vim_free(to);
14623#endif
14624}
14625
14626/*
14627 * "indent()" function
14628 */
14629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014630f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014631{
14632 linenr_T lnum;
14633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014634 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014636 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014637 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014639}
14640
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014641/*
14642 * "index()" function
14643 */
14644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014645f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014646{
Bram Moolenaar33570922005-01-25 22:26:29 +000014647 list_T *l;
14648 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014649 long idx = 0;
14650 int ic = FALSE;
14651
14652 rettv->vval.v_number = -1;
14653 if (argvars[0].v_type != VAR_LIST)
14654 {
14655 EMSG(_(e_listreq));
14656 return;
14657 }
14658 l = argvars[0].vval.v_list;
14659 if (l != NULL)
14660 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014661 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014662 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014664 int error = FALSE;
14665
Bram Moolenaar758711c2005-02-02 23:11:38 +000014666 /* Start at specified item. Use the cached index that list_find()
14667 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014669 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014670 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014671 ic = get_tv_number_chk(&argvars[3], &error);
14672 if (error)
14673 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014674 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014675
Bram Moolenaar758711c2005-02-02 23:11:38 +000014676 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014677 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014678 {
14679 rettv->vval.v_number = idx;
14680 break;
14681 }
14682 }
14683}
14684
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685static int inputsecret_flag = 0;
14686
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014687static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014688
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014690 * This function is used by f_input() and f_inputdialog() functions. The third
14691 * argument to f_input() specifies the type of completion to use at the
14692 * prompt. The third argument to f_inputdialog() specifies the value to return
14693 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014694 */
14695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014696get_user_input(
14697 typval_T *argvars,
14698 typval_T *rettv,
14699 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014701 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014702 char_u *p = NULL;
14703 int c;
14704 char_u buf[NUMBUFLEN];
14705 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014706 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014707 int xp_type = EXPAND_NOTHING;
14708 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014709
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014710 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014711 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014712
14713#ifdef NO_CONSOLE_INPUT
14714 /* While starting up, there is no place to enter text. */
14715 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014717#endif
14718
14719 cmd_silent = FALSE; /* Want to see the prompt. */
14720 if (prompt != NULL)
14721 {
14722 /* Only the part of the message after the last NL is considered as
14723 * prompt for the command line */
14724 p = vim_strrchr(prompt, '\n');
14725 if (p == NULL)
14726 p = prompt;
14727 else
14728 {
14729 ++p;
14730 c = *p;
14731 *p = NUL;
14732 msg_start();
14733 msg_clr_eos();
14734 msg_puts_attr(prompt, echo_attr);
14735 msg_didout = FALSE;
14736 msg_starthere();
14737 *p = c;
14738 }
14739 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014741 if (argvars[1].v_type != VAR_UNKNOWN)
14742 {
14743 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14744 if (defstr != NULL)
14745 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014747 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014748 {
14749 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014750 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014751 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014752
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014753 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014754 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014755
Bram Moolenaar4463f292005-09-25 22:20:24 +000014756 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14757 if (xp_name == NULL)
14758 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014759
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014760 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014761
Bram Moolenaar4463f292005-09-25 22:20:24 +000014762 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14763 &xp_arg) == FAIL)
14764 return;
14765 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014766 }
14767
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014768 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014769 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014770 int save_ex_normal_busy = ex_normal_busy;
14771 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014772 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014773 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14774 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014775 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014776 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014777 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014778 && argvars[1].v_type != VAR_UNKNOWN
14779 && argvars[2].v_type != VAR_UNKNOWN)
14780 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14781 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014782
14783 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014784
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014785 /* since the user typed this, no need to wait for return */
14786 need_wait_return = FALSE;
14787 msg_didout = FALSE;
14788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014789 cmd_silent = cmd_silent_save;
14790}
14791
14792/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014793 * "input()" function
14794 * Also handles inputsecret() when inputsecret is set.
14795 */
14796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014797f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014798{
14799 get_user_input(argvars, rettv, FALSE);
14800}
14801
14802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014803 * "inputdialog()" function
14804 */
14805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014806f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807{
14808#if defined(FEAT_GUI_TEXTDIALOG)
14809 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14810 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14811 {
14812 char_u *message;
14813 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014814 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014816 message = get_tv_string_chk(&argvars[0]);
14817 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014818 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014819 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 else
14821 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014822 if (message != NULL && defstr != NULL
14823 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014824 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014825 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826 else
14827 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828 if (message != NULL && defstr != NULL
14829 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014830 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014831 rettv->vval.v_string = vim_strsave(
14832 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014836 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 }
14838 else
14839#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014840 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841}
14842
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014843/*
14844 * "inputlist()" function
14845 */
14846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014847f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014848{
14849 listitem_T *li;
14850 int selected;
14851 int mouse_used;
14852
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014853#ifdef NO_CONSOLE_INPUT
14854 /* While starting up, there is no place to enter text. */
14855 if (no_console_input())
14856 return;
14857#endif
14858 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14859 {
14860 EMSG2(_(e_listarg), "inputlist()");
14861 return;
14862 }
14863
14864 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014865 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014866 lines_left = Rows; /* avoid more prompt */
14867 msg_scroll = TRUE;
14868 msg_clr_eos();
14869
14870 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14871 {
14872 msg_puts(get_tv_string(&li->li_tv));
14873 msg_putchar('\n');
14874 }
14875
14876 /* Ask for choice. */
14877 selected = prompt_for_number(&mouse_used);
14878 if (mouse_used)
14879 selected -= lines_left;
14880
14881 rettv->vval.v_number = selected;
14882}
14883
14884
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14886
14887/*
14888 * "inputrestore()" function
14889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014891f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014892{
14893 if (ga_userinput.ga_len > 0)
14894 {
14895 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014896 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14897 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014898 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014899 }
14900 else if (p_verbose > 1)
14901 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014902 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014903 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904 }
14905}
14906
14907/*
14908 * "inputsave()" function
14909 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014911f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014913 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 if (ga_grow(&ga_userinput, 1) == OK)
14915 {
14916 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14917 + ga_userinput.ga_len);
14918 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014919 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014920 }
14921 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014922 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923}
14924
14925/*
14926 * "inputsecret()" function
14927 */
14928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014929f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930{
14931 ++cmdline_star;
14932 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014933 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014934 --cmdline_star;
14935 --inputsecret_flag;
14936}
14937
14938/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014939 * "insert()" function
14940 */
14941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014942f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014943{
14944 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 listitem_T *item;
14946 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014947 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014948
14949 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014950 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014951 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014952 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014953 {
14954 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014955 before = get_tv_number_chk(&argvars[2], &error);
14956 if (error)
14957 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014958
Bram Moolenaar758711c2005-02-02 23:11:38 +000014959 if (before == l->lv_len)
14960 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014961 else
14962 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014963 item = list_find(l, before);
14964 if (item == NULL)
14965 {
14966 EMSGN(_(e_listidx), before);
14967 l = NULL;
14968 }
14969 }
14970 if (l != NULL)
14971 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014972 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014973 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014974 }
14975 }
14976}
14977
14978/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014979 * "invert(expr)" function
14980 */
14981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014982f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014983{
14984 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14985}
14986
14987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014988 * "isdirectory()" function
14989 */
14990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014991f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014993 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994}
14995
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014996/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014997 * "islocked()" function
14998 */
14999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015000f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015001{
15002 lval_T lv;
15003 char_u *end;
15004 dictitem_T *di;
15005
15006 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015007 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
15008 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015009 if (end != NULL && lv.ll_name != NULL)
15010 {
15011 if (*end != NUL)
15012 EMSG(_(e_trailing));
15013 else
15014 {
15015 if (lv.ll_tv == NULL)
15016 {
15017 if (check_changedtick(lv.ll_name))
15018 rettv->vval.v_number = 1; /* always locked */
15019 else
15020 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010015021 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015022 if (di != NULL)
15023 {
15024 /* Consider a variable locked when:
15025 * 1. the variable itself is locked
15026 * 2. the value of the variable is locked.
15027 * 3. the List or Dict value is locked.
15028 */
15029 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
15030 || tv_islocked(&di->di_tv));
15031 }
15032 }
15033 }
15034 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015035 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015036 else if (lv.ll_newkey != NULL)
15037 EMSG2(_(e_dictkey), lv.ll_newkey);
15038 else if (lv.ll_list != NULL)
15039 /* List item. */
15040 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
15041 else
15042 /* Dictionary item. */
15043 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
15044 }
15045 }
15046
15047 clear_lval(&lv);
15048}
15049
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010015050#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
15051/*
15052 * "isnan()" function
15053 */
15054 static void
15055f_isnan(typval_T *argvars, typval_T *rettv)
15056{
15057 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
15058 && isnan(argvars[0].vval.v_float);
15059}
15060#endif
15061
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015062static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015063
15064/*
15065 * Turn a dict into a list:
15066 * "what" == 0: list of keys
15067 * "what" == 1: list of values
15068 * "what" == 2: list of items
15069 */
15070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015071dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015072{
Bram Moolenaar33570922005-01-25 22:26:29 +000015073 list_T *l2;
15074 dictitem_T *di;
15075 hashitem_T *hi;
15076 listitem_T *li;
15077 listitem_T *li2;
15078 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015079 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015080
Bram Moolenaar8c711452005-01-14 21:53:12 +000015081 if (argvars[0].v_type != VAR_DICT)
15082 {
15083 EMSG(_(e_dictreq));
15084 return;
15085 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015086 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015087 return;
15088
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015089 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015090 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015091
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015092 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015093 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015095 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015097 --todo;
15098 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015099
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015100 li = listitem_alloc();
15101 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015102 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015103 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000015104
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015105 if (what == 0)
15106 {
15107 /* keys() */
15108 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015109 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015110 li->li_tv.vval.v_string = vim_strsave(di->di_key);
15111 }
15112 else if (what == 1)
15113 {
15114 /* values() */
15115 copy_tv(&di->di_tv, &li->li_tv);
15116 }
15117 else
15118 {
15119 /* items() */
15120 l2 = list_alloc();
15121 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015122 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015123 li->li_tv.vval.v_list = l2;
15124 if (l2 == NULL)
15125 break;
15126 ++l2->lv_refcount;
15127
15128 li2 = listitem_alloc();
15129 if (li2 == NULL)
15130 break;
15131 list_append(l2, li2);
15132 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015133 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015134 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
15135
15136 li2 = listitem_alloc();
15137 if (li2 == NULL)
15138 break;
15139 list_append(l2, li2);
15140 copy_tv(&di->di_tv, &li2->li_tv);
15141 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015142 }
15143 }
15144}
15145
15146/*
15147 * "items(dict)" function
15148 */
15149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015150f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015151{
15152 dict_list(argvars, rettv, 2);
15153}
15154
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010015155#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015156/*
15157 * Get the job from the argument.
15158 * Returns NULL if the job is invalid.
15159 */
15160 static job_T *
15161get_job_arg(typval_T *tv)
15162{
15163 job_T *job;
15164
15165 if (tv->v_type != VAR_JOB)
15166 {
15167 EMSG2(_(e_invarg2), get_tv_string(tv));
15168 return NULL;
15169 }
15170 job = tv->vval.v_job;
15171
15172 if (job == NULL)
15173 EMSG(_("E916: not a valid job"));
15174 return job;
15175}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015176
Bram Moolenaar835dc632016-02-07 14:27:38 +010015177/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015178 * "job_getchannel()" function
15179 */
15180 static void
15181f_job_getchannel(typval_T *argvars, typval_T *rettv)
15182{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015183 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015184
Bram Moolenaar65edff82016-02-21 16:40:11 +010015185 if (job != NULL)
15186 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015187 rettv->v_type = VAR_CHANNEL;
15188 rettv->vval.v_channel = job->jv_channel;
15189 if (job->jv_channel != NULL)
15190 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015191 }
15192}
15193
15194/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010015195 * "job_info()" function
15196 */
15197 static void
15198f_job_info(typval_T *argvars, typval_T *rettv)
15199{
15200 job_T *job = get_job_arg(&argvars[0]);
15201
15202 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
15203 job_info(job, rettv->vval.v_dict);
15204}
15205
15206/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015207 * "job_setoptions()" function
15208 */
15209 static void
15210f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15211{
15212 job_T *job = get_job_arg(&argvars[0]);
15213 jobopt_T opt;
15214
15215 if (job == NULL)
15216 return;
15217 clear_job_options(&opt);
Bram Moolenaar0e4c1de2016-04-07 21:40:38 +020015218 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == OK)
15219 job_set_options(job, &opt);
15220 free_job_options(&opt);
Bram Moolenaar65edff82016-02-21 16:40:11 +010015221}
15222
15223/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015224 * "job_start()" function
15225 */
15226 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010015227f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015228{
Bram Moolenaar835dc632016-02-07 14:27:38 +010015229 rettv->v_type = VAR_JOB;
Bram Moolenaar38499922016-04-22 20:46:52 +020015230 if (check_restricted() || check_secure())
15231 return;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015232 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015233}
15234
15235/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015236 * "job_status()" function
15237 */
15238 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015239f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015240{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015241 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015242
Bram Moolenaar65edff82016-02-21 16:40:11 +010015243 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015244 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015245 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010015246 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010015247 }
15248}
15249
15250/*
15251 * "job_stop()" function
15252 */
15253 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015254f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015255{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015256 job_T *job = get_job_arg(&argvars[0]);
15257
15258 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010015259 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015260}
15261#endif
15262
Bram Moolenaar071d4272004-06-13 20:20:40 +000015263/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015264 * "join()" function
15265 */
15266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015267f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015268{
15269 garray_T ga;
15270 char_u *sep;
15271
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015272 if (argvars[0].v_type != VAR_LIST)
15273 {
15274 EMSG(_(e_listreq));
15275 return;
15276 }
15277 if (argvars[0].vval.v_list == NULL)
15278 return;
15279 if (argvars[1].v_type == VAR_UNKNOWN)
15280 sep = (char_u *)" ";
15281 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015282 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015283
15284 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015285
15286 if (sep != NULL)
15287 {
15288 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar18dfb442016-05-31 22:31:23 +020015289 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, FALSE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 ga_append(&ga, NUL);
15291 rettv->vval.v_string = (char_u *)ga.ga_data;
15292 }
15293 else
15294 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015295}
15296
15297/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015298 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015299 */
15300 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015301f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015302{
15303 js_read_T reader;
15304
15305 reader.js_buf = get_tv_string(&argvars[0]);
15306 reader.js_fill = NULL;
15307 reader.js_used = 0;
15308 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15309 EMSG(_(e_invarg));
15310}
15311
15312/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015313 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015314 */
15315 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015316f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015317{
15318 rettv->v_type = VAR_STRING;
15319 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15320}
15321
15322/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015323 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015324 */
15325 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015326f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015327{
15328 js_read_T reader;
15329
15330 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015331 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015332 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015333 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015334 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015335}
15336
15337/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015338 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015339 */
15340 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015341f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015342{
15343 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015344 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015345}
15346
15347/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015348 * "keys()" function
15349 */
15350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015351f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015352{
15353 dict_list(argvars, rettv, 0);
15354}
15355
15356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 * "last_buffer_nr()" function.
15358 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015360f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015361{
15362 int n = 0;
15363 buf_T *buf;
15364
15365 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15366 if (n < buf->b_fnum)
15367 n = buf->b_fnum;
15368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015369 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015370}
15371
15372/*
15373 * "len()" function
15374 */
15375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015376f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015377{
15378 switch (argvars[0].v_type)
15379 {
15380 case VAR_STRING:
15381 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015382 rettv->vval.v_number = (varnumber_T)STRLEN(
15383 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015384 break;
15385 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015386 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015387 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015388 case VAR_DICT:
15389 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15390 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015391 case VAR_UNKNOWN:
15392 case VAR_SPECIAL:
15393 case VAR_FLOAT:
15394 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010015395 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015396 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015397 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015398 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015399 break;
15400 }
15401}
15402
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015403static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015404
15405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015406libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015407{
15408#ifdef FEAT_LIBCALL
15409 char_u *string_in;
15410 char_u **string_result;
15411 int nr_result;
15412#endif
15413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015414 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015415 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015417
15418 if (check_restricted() || check_secure())
15419 return;
15420
15421#ifdef FEAT_LIBCALL
15422 /* The first two args must be strings, otherwise its meaningless */
15423 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15424 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015425 string_in = NULL;
15426 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015427 string_in = argvars[2].vval.v_string;
15428 if (type == VAR_NUMBER)
15429 string_result = NULL;
15430 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015431 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015432 if (mch_libcall(argvars[0].vval.v_string,
15433 argvars[1].vval.v_string,
15434 string_in,
15435 argvars[2].vval.v_number,
15436 string_result,
15437 &nr_result) == OK
15438 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015440 }
15441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015442}
15443
15444/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015445 * "libcall()" function
15446 */
15447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015448f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015449{
15450 libcall_common(argvars, rettv, VAR_STRING);
15451}
15452
15453/*
15454 * "libcallnr()" function
15455 */
15456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015457f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015458{
15459 libcall_common(argvars, rettv, VAR_NUMBER);
15460}
15461
15462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 * "line(string)" function
15464 */
15465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015466f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467{
15468 linenr_T lnum = 0;
15469 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015470 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015471
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015472 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 if (fp != NULL)
15474 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015475 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476}
15477
15478/*
15479 * "line2byte(lnum)" function
15480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015482f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483{
15484#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486#else
15487 linenr_T lnum;
15488
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015489 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015490 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15494 if (rettv->vval.v_number >= 0)
15495 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496#endif
15497}
15498
15499/*
15500 * "lispindent(lnum)" function
15501 */
15502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504{
15505#ifdef FEAT_LISP
15506 pos_T pos;
15507 linenr_T lnum;
15508
15509 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015510 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15512 {
15513 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015514 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515 curwin->w_cursor = pos;
15516 }
15517 else
15518#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015519 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015520}
15521
15522/*
15523 * "localtime()" function
15524 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015526f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529}
15530
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015531static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532
15533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015534get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535{
15536 char_u *keys;
15537 char_u *which;
15538 char_u buf[NUMBUFLEN];
15539 char_u *keys_buf = NULL;
15540 char_u *rhs;
15541 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015542 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015543 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015544 mapblock_T *mp;
15545 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546
15547 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015548 rettv->v_type = VAR_STRING;
15549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552 if (*keys == NUL)
15553 return;
15554
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015555 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015557 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015558 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015559 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015560 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015561 if (argvars[3].v_type != VAR_UNKNOWN)
15562 get_dict = get_tv_number(&argvars[3]);
15563 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565 else
15566 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015567 if (which == NULL)
15568 return;
15569
Bram Moolenaar071d4272004-06-13 20:20:40 +000015570 mode = get_map_mode(&which, 0);
15571
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015572 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015573 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015575
15576 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015578 /* Return a string. */
15579 if (rhs != NULL)
15580 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581
Bram Moolenaarbd743252010-10-20 21:23:33 +020015582 }
15583 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15584 {
15585 /* Return a dictionary. */
15586 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15587 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15588 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589
Bram Moolenaarbd743252010-10-20 21:23:33 +020015590 dict_add_nr_str(dict, "lhs", 0L, lhs);
15591 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15592 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15593 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15594 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15595 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15596 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015597 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015598 dict_add_nr_str(dict, "mode", 0L, mapmode);
15599
15600 vim_free(lhs);
15601 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015602 }
15603}
15604
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015605#ifdef FEAT_FLOAT
15606/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015607 * "log()" function
15608 */
15609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015610f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015611{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015612 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015613
15614 rettv->v_type = VAR_FLOAT;
15615 if (get_float_arg(argvars, &f) == OK)
15616 rettv->vval.v_float = log(f);
15617 else
15618 rettv->vval.v_float = 0.0;
15619}
15620
15621/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015622 * "log10()" function
15623 */
15624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015625f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015626{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015627 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015628
15629 rettv->v_type = VAR_FLOAT;
15630 if (get_float_arg(argvars, &f) == OK)
15631 rettv->vval.v_float = log10(f);
15632 else
15633 rettv->vval.v_float = 0.0;
15634}
15635#endif
15636
Bram Moolenaar1dced572012-04-05 16:54:08 +020015637#ifdef FEAT_LUA
15638/*
15639 * "luaeval()" function
15640 */
15641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015642f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015643{
15644 char_u *str;
15645 char_u buf[NUMBUFLEN];
15646
15647 str = get_tv_string_buf(&argvars[0], buf);
15648 do_luaeval(str, argvars + 1, rettv);
15649}
15650#endif
15651
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015653 * "map()" function
15654 */
15655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015656f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015657{
15658 filter_map(argvars, rettv, TRUE);
15659}
15660
15661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015662 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 */
15664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015665f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015666{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668}
15669
15670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015671 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015672 */
15673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015674f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015675{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677}
15678
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015679static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680
15681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015683{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015684 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015685 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015687 char_u *pat;
15688 regmatch_T regmatch;
15689 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015690 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691 char_u *save_cpo;
15692 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015693 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015694 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015695 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 list_T *l = NULL;
15697 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015699 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015700
15701 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15702 save_cpo = p_cpo;
15703 p_cpo = (char_u *)"";
15704
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015705 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015706 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015707 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015708 /* type 3: return empty list when there are no matches.
15709 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015710 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015711 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015712 if (type == 4
15713 && (list_append_string(rettv->vval.v_list,
15714 (char_u *)"", 0) == FAIL
15715 || list_append_number(rettv->vval.v_list,
15716 (varnumber_T)-1) == FAIL
15717 || list_append_number(rettv->vval.v_list,
15718 (varnumber_T)-1) == FAIL
15719 || list_append_number(rettv->vval.v_list,
15720 (varnumber_T)-1) == FAIL))
15721 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020015722 list_free(rettv->vval.v_list);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015723 rettv->vval.v_list = NULL;
15724 goto theend;
15725 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726 }
15727 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015729 rettv->v_type = VAR_STRING;
15730 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015733 if (argvars[0].v_type == VAR_LIST)
15734 {
15735 if ((l = argvars[0].vval.v_list) == NULL)
15736 goto theend;
15737 li = l->lv_first;
15738 }
15739 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015740 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015741 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015742 len = (long)STRLEN(str);
15743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015745 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15746 if (pat == NULL)
15747 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015748
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015749 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015751 int error = FALSE;
15752
15753 start = get_tv_number_chk(&argvars[2], &error);
15754 if (error)
15755 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015756 if (l != NULL)
15757 {
15758 li = list_find(l, start);
15759 if (li == NULL)
15760 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015761 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015762 }
15763 else
15764 {
15765 if (start < 0)
15766 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015767 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015768 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015769 /* When "count" argument is there ignore matches before "start",
15770 * otherwise skip part of the string. Differs when pattern is "^"
15771 * or "\<". */
15772 if (argvars[3].v_type != VAR_UNKNOWN)
15773 startcol = start;
15774 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015775 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015776 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015777 len -= start;
15778 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015779 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015780
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015781 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015782 nth = get_tv_number_chk(&argvars[3], &error);
15783 if (error)
15784 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 }
15786
15787 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15788 if (regmatch.regprog != NULL)
15789 {
15790 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015791
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015792 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015793 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015794 if (l != NULL)
15795 {
15796 if (li == NULL)
15797 {
15798 match = FALSE;
15799 break;
15800 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015801 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015802 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015803 if (str == NULL)
15804 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015805 }
15806
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015807 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015808
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015809 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015810 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015811 if (l == NULL && !match)
15812 break;
15813
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015814 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015815 if (l != NULL)
15816 {
15817 li = li->li_next;
15818 ++idx;
15819 }
15820 else
15821 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015822#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015823 startcol = (colnr_T)(regmatch.startp[0]
15824 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015825#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015826 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015827#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015828 if (startcol > (colnr_T)len
15829 || str + startcol <= regmatch.startp[0])
15830 {
15831 match = FALSE;
15832 break;
15833 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015834 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015835 }
15836
15837 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015838 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015839 if (type == 4)
15840 {
15841 listitem_T *li1 = rettv->vval.v_list->lv_first;
15842 listitem_T *li2 = li1->li_next;
15843 listitem_T *li3 = li2->li_next;
15844 listitem_T *li4 = li3->li_next;
15845
Bram Moolenaar3c809342016-06-01 22:34:48 +020015846 vim_free(li1->li_tv.vval.v_string);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015847 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15848 (int)(regmatch.endp[0] - regmatch.startp[0]));
15849 li3->li_tv.vval.v_number =
15850 (varnumber_T)(regmatch.startp[0] - expr);
15851 li4->li_tv.vval.v_number =
15852 (varnumber_T)(regmatch.endp[0] - expr);
15853 if (l != NULL)
15854 li2->li_tv.vval.v_number = (varnumber_T)idx;
15855 }
15856 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015857 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015858 int i;
15859
15860 /* return list with matched string and submatches */
15861 for (i = 0; i < NSUBEXP; ++i)
15862 {
15863 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015864 {
15865 if (list_append_string(rettv->vval.v_list,
15866 (char_u *)"", 0) == FAIL)
15867 break;
15868 }
15869 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015870 regmatch.startp[i],
15871 (int)(regmatch.endp[i] - regmatch.startp[i]))
15872 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015873 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015874 }
15875 }
15876 else if (type == 2)
15877 {
15878 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015879 if (l != NULL)
15880 copy_tv(&li->li_tv, rettv);
15881 else
15882 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015884 }
15885 else if (l != NULL)
15886 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 else
15888 {
15889 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015890 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015891 (varnumber_T)(regmatch.startp[0] - str);
15892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015893 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015895 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896 }
15897 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015898 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015899 }
15900
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015901 if (type == 4 && l == NULL)
15902 /* matchstrpos() without a list: drop the second item. */
15903 listitem_remove(rettv->vval.v_list,
15904 rettv->vval.v_list->lv_first->li_next);
15905
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015907 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 p_cpo = save_cpo;
15909}
15910
15911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912 * "match()" function
15913 */
15914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916{
15917 find_some_match(argvars, rettv, 1);
15918}
15919
15920/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015921 * "matchadd()" function
15922 */
15923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015924f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015925{
15926#ifdef FEAT_SEARCH_EXTRA
15927 char_u buf[NUMBUFLEN];
15928 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15929 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15930 int prio = 10; /* default priority */
15931 int id = -1;
15932 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015933 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015934
15935 rettv->vval.v_number = -1;
15936
15937 if (grp == NULL || pat == NULL)
15938 return;
15939 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015940 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015941 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015942 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015943 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015944 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015945 if (argvars[4].v_type != VAR_UNKNOWN)
15946 {
15947 if (argvars[4].v_type != VAR_DICT)
15948 {
15949 EMSG(_(e_dictreq));
15950 return;
15951 }
15952 if (dict_find(argvars[4].vval.v_dict,
15953 (char_u *)"conceal", -1) != NULL)
15954 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15955 (char_u *)"conceal", FALSE);
15956 }
15957 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015958 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015959 if (error == TRUE)
15960 return;
15961 if (id >= 1 && id <= 3)
15962 {
15963 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15964 return;
15965 }
15966
Bram Moolenaar6561d522015-07-21 15:48:27 +020015967 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15968 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015969#endif
15970}
15971
15972/*
15973 * "matchaddpos()" function
15974 */
15975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015976f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015977{
15978#ifdef FEAT_SEARCH_EXTRA
15979 char_u buf[NUMBUFLEN];
15980 char_u *group;
15981 int prio = 10;
15982 int id = -1;
15983 int error = FALSE;
15984 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015985 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015986
15987 rettv->vval.v_number = -1;
15988
15989 group = get_tv_string_buf_chk(&argvars[0], buf);
15990 if (group == NULL)
15991 return;
15992
15993 if (argvars[1].v_type != VAR_LIST)
15994 {
15995 EMSG2(_(e_listarg), "matchaddpos()");
15996 return;
15997 }
15998 l = argvars[1].vval.v_list;
15999 if (l == NULL)
16000 return;
16001
16002 if (argvars[2].v_type != VAR_UNKNOWN)
16003 {
16004 prio = get_tv_number_chk(&argvars[2], &error);
16005 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016006 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016007 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016008 if (argvars[4].v_type != VAR_UNKNOWN)
16009 {
16010 if (argvars[4].v_type != VAR_DICT)
16011 {
16012 EMSG(_(e_dictreq));
16013 return;
16014 }
16015 if (dict_find(argvars[4].vval.v_dict,
16016 (char_u *)"conceal", -1) != NULL)
16017 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16018 (char_u *)"conceal", FALSE);
16019 }
16020 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016021 }
16022 if (error == TRUE)
16023 return;
16024
16025 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16026 if (id == 1 || id == 2)
16027 {
16028 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16029 return;
16030 }
16031
Bram Moolenaar6561d522015-07-21 15:48:27 +020016032 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16033 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016034#endif
16035}
16036
16037/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016038 * "matcharg()" function
16039 */
16040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016041f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016042{
16043 if (rettv_list_alloc(rettv) == OK)
16044 {
16045#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016046 int id = get_tv_number(&argvars[0]);
16047 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016048
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016049 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016050 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016051 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16052 {
16053 list_append_string(rettv->vval.v_list,
16054 syn_id2name(m->hlg_id), -1);
16055 list_append_string(rettv->vval.v_list, m->pattern, -1);
16056 }
16057 else
16058 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016059 list_append_string(rettv->vval.v_list, NULL, -1);
16060 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016061 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016062 }
16063#endif
16064 }
16065}
16066
16067/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016068 * "matchdelete()" function
16069 */
16070 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016071f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016072{
16073#ifdef FEAT_SEARCH_EXTRA
16074 rettv->vval.v_number = match_delete(curwin,
16075 (int)get_tv_number(&argvars[0]), TRUE);
16076#endif
16077}
16078
16079/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016080 * "matchend()" function
16081 */
16082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016083f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016084{
16085 find_some_match(argvars, rettv, 0);
16086}
16087
16088/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016089 * "matchlist()" function
16090 */
16091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016092f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016093{
16094 find_some_match(argvars, rettv, 3);
16095}
16096
16097/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098 * "matchstr()" function
16099 */
16100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016101f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016102{
16103 find_some_match(argvars, rettv, 2);
16104}
16105
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020016106/*
16107 * "matchstrpos()" function
16108 */
16109 static void
16110f_matchstrpos(typval_T *argvars, typval_T *rettv)
16111{
16112 find_some_match(argvars, rettv, 4);
16113}
16114
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016115static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016116
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016119{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016120 long n = 0;
16121 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016122 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016123
16124 if (argvars[0].v_type == VAR_LIST)
16125 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016126 list_T *l;
16127 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016128
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016129 l = argvars[0].vval.v_list;
16130 if (l != NULL)
16131 {
16132 li = l->lv_first;
16133 if (li != NULL)
16134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016135 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016136 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016137 {
16138 li = li->li_next;
16139 if (li == NULL)
16140 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016141 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016142 if (domax ? i > n : i < n)
16143 n = i;
16144 }
16145 }
16146 }
16147 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016148 else if (argvars[0].v_type == VAR_DICT)
16149 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016151 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016153 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016154
16155 d = argvars[0].vval.v_dict;
16156 if (d != NULL)
16157 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016158 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016160 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016161 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016162 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016163 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016164 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016165 if (first)
16166 {
16167 n = i;
16168 first = FALSE;
16169 }
16170 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016171 n = i;
16172 }
16173 }
16174 }
16175 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016176 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016177 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016178 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016179}
16180
16181/*
16182 * "max()" function
16183 */
16184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016185f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016186{
16187 max_min(argvars, rettv, TRUE);
16188}
16189
16190/*
16191 * "min()" function
16192 */
16193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016194f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016195{
16196 max_min(argvars, rettv, FALSE);
16197}
16198
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016199static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016200
16201/*
16202 * Create the directory in which "dir" is located, and higher levels when
16203 * needed.
16204 */
16205 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016206mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016207{
16208 char_u *p;
16209 char_u *updir;
16210 int r = FAIL;
16211
16212 /* Get end of directory name in "dir".
16213 * We're done when it's "/" or "c:/". */
16214 p = gettail_sep(dir);
16215 if (p <= get_past_head(dir))
16216 return OK;
16217
16218 /* If the directory exists we're done. Otherwise: create it.*/
16219 updir = vim_strnsave(dir, (int)(p - dir));
16220 if (updir == NULL)
16221 return FAIL;
16222 if (mch_isdir(updir))
16223 r = OK;
16224 else if (mkdir_recurse(updir, prot) == OK)
16225 r = vim_mkdir_emsg(updir, prot);
16226 vim_free(updir);
16227 return r;
16228}
16229
16230#ifdef vim_mkdir
16231/*
16232 * "mkdir()" function
16233 */
16234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016235f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016236{
16237 char_u *dir;
16238 char_u buf[NUMBUFLEN];
16239 int prot = 0755;
16240
16241 rettv->vval.v_number = FAIL;
16242 if (check_restricted() || check_secure())
16243 return;
16244
16245 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016246 if (*dir == NUL)
16247 rettv->vval.v_number = FAIL;
16248 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016249 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016250 if (*gettail(dir) == NUL)
16251 /* remove trailing slashes */
16252 *gettail_sep(dir) = NUL;
16253
16254 if (argvars[1].v_type != VAR_UNKNOWN)
16255 {
16256 if (argvars[2].v_type != VAR_UNKNOWN)
16257 prot = get_tv_number_chk(&argvars[2], NULL);
16258 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16259 mkdir_recurse(dir, prot);
16260 }
16261 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016262 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016263}
16264#endif
16265
Bram Moolenaar0d660222005-01-07 21:51:51 +000016266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016267 * "mode()" function
16268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016270f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016272 char_u buf[3];
16273
16274 buf[1] = NUL;
16275 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016276
Bram Moolenaar071d4272004-06-13 20:20:40 +000016277 if (VIsual_active)
16278 {
16279 if (VIsual_select)
16280 buf[0] = VIsual_mode + 's' - 'v';
16281 else
16282 buf[0] = VIsual_mode;
16283 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016284 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016285 || State == CONFIRM)
16286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016288 if (State == ASKMORE)
16289 buf[1] = 'm';
16290 else if (State == CONFIRM)
16291 buf[1] = '?';
16292 }
16293 else if (State == EXTERNCMD)
16294 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016295 else if (State & INSERT)
16296 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016297#ifdef FEAT_VREPLACE
16298 if (State & VREPLACE_FLAG)
16299 {
16300 buf[0] = 'R';
16301 buf[1] = 'v';
16302 }
16303 else
16304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 if (State & REPLACE_FLAG)
16306 buf[0] = 'R';
16307 else
16308 buf[0] = 'i';
16309 }
16310 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016313 if (exmode_active)
16314 buf[1] = 'v';
16315 }
16316 else if (exmode_active)
16317 {
16318 buf[0] = 'c';
16319 buf[1] = 'e';
16320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016324 if (finish_op)
16325 buf[1] = 'o';
16326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016328 /* Clear out the minor mode when the argument is not a non-zero number or
16329 * non-empty string. */
16330 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016331 buf[1] = NUL;
16332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016333 rettv->vval.v_string = vim_strsave(buf);
16334 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335}
16336
Bram Moolenaar429fa852013-04-15 12:27:36 +020016337#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016338/*
16339 * "mzeval()" function
16340 */
16341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016342f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016343{
16344 char_u *str;
16345 char_u buf[NUMBUFLEN];
16346
16347 str = get_tv_string_buf(&argvars[0], buf);
16348 do_mzeval(str, rettv);
16349}
Bram Moolenaar75676462013-01-30 14:55:42 +010016350
16351 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016352mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016353{
16354 typval_T argvars[3];
16355
16356 argvars[0].v_type = VAR_STRING;
16357 argvars[0].vval.v_string = name;
16358 copy_tv(args, &argvars[1]);
16359 argvars[2].v_type = VAR_UNKNOWN;
16360 f_call(argvars, rettv);
16361 clear_tv(&argvars[1]);
16362}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016363#endif
16364
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016366 * "nextnonblank()" function
16367 */
16368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016370{
16371 linenr_T lnum;
16372
16373 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16374 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016375 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016376 {
16377 lnum = 0;
16378 break;
16379 }
16380 if (*skipwhite(ml_get(lnum)) != NUL)
16381 break;
16382 }
16383 rettv->vval.v_number = lnum;
16384}
16385
16386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387 * "nr2char()" function
16388 */
16389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016390f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391{
16392 char_u buf[NUMBUFLEN];
16393
16394#ifdef FEAT_MBYTE
16395 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016396 {
16397 int utf8 = 0;
16398
16399 if (argvars[1].v_type != VAR_UNKNOWN)
16400 utf8 = get_tv_number_chk(&argvars[1], NULL);
16401 if (utf8)
16402 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16403 else
16404 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016406 else
16407#endif
16408 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016409 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 buf[1] = NUL;
16411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016412 rettv->v_type = VAR_STRING;
16413 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016414}
16415
16416/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016417 * "or(expr, expr)" function
16418 */
16419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016420f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016421{
16422 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16423 | get_tv_number_chk(&argvars[1], NULL);
16424}
16425
16426/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016427 * "pathshorten()" function
16428 */
16429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016430f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016431{
16432 char_u *p;
16433
16434 rettv->v_type = VAR_STRING;
16435 p = get_tv_string_chk(&argvars[0]);
16436 if (p == NULL)
16437 rettv->vval.v_string = NULL;
16438 else
16439 {
16440 p = vim_strsave(p);
16441 rettv->vval.v_string = p;
16442 if (p != NULL)
16443 shorten_dir(p);
16444 }
16445}
16446
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016447#ifdef FEAT_PERL
16448/*
16449 * "perleval()" function
16450 */
16451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016452f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016453{
16454 char_u *str;
16455 char_u buf[NUMBUFLEN];
16456
16457 str = get_tv_string_buf(&argvars[0], buf);
16458 do_perleval(str, rettv);
16459}
16460#endif
16461
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016462#ifdef FEAT_FLOAT
16463/*
16464 * "pow()" function
16465 */
16466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016467f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016468{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016469 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016470
16471 rettv->v_type = VAR_FLOAT;
16472 if (get_float_arg(argvars, &fx) == OK
16473 && get_float_arg(&argvars[1], &fy) == OK)
16474 rettv->vval.v_float = pow(fx, fy);
16475 else
16476 rettv->vval.v_float = 0.0;
16477}
16478#endif
16479
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016480/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481 * "prevnonblank()" function
16482 */
16483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016484f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016485{
16486 linenr_T lnum;
16487
16488 lnum = get_tv_lnum(argvars);
16489 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16490 lnum = 0;
16491 else
16492 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16493 --lnum;
16494 rettv->vval.v_number = lnum;
16495}
16496
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016497/* This dummy va_list is here because:
16498 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16499 * - locally in the function results in a "used before set" warning
16500 * - using va_start() to initialize it gives "function with fixed args" error */
16501static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016502
Bram Moolenaar8c711452005-01-14 21:53:12 +000016503/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016504 * "printf()" function
16505 */
16506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016507f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016508{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016509 char_u buf[NUMBUFLEN];
16510 int len;
16511 char_u *s;
16512 int saved_did_emsg = did_emsg;
16513 char *fmt;
16514
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016515 rettv->v_type = VAR_STRING;
16516 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016517
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016518 /* Get the required length, allocate the buffer and do it for real. */
16519 did_emsg = FALSE;
16520 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16521 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16522 if (!did_emsg)
16523 {
16524 s = alloc(len + 1);
16525 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016526 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016527 rettv->vval.v_string = s;
16528 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016529 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016530 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016531 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016532}
16533
16534/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016535 * "pumvisible()" function
16536 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016539{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016540#ifdef FEAT_INS_EXPAND
16541 if (pum_visible())
16542 rettv->vval.v_number = 1;
16543#endif
16544}
16545
Bram Moolenaardb913952012-06-29 12:54:53 +020016546#ifdef FEAT_PYTHON3
16547/*
16548 * "py3eval()" function
16549 */
16550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016551f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016552{
16553 char_u *str;
16554 char_u buf[NUMBUFLEN];
16555
16556 str = get_tv_string_buf(&argvars[0], buf);
16557 do_py3eval(str, rettv);
16558}
16559#endif
16560
16561#ifdef FEAT_PYTHON
16562/*
16563 * "pyeval()" function
16564 */
16565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016566f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016567{
16568 char_u *str;
16569 char_u buf[NUMBUFLEN];
16570
16571 str = get_tv_string_buf(&argvars[0], buf);
16572 do_pyeval(str, rettv);
16573}
16574#endif
16575
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016576/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016577 * "range()" function
16578 */
16579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016580f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016581{
16582 long start;
16583 long end;
16584 long stride = 1;
16585 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016586 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016588 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016589 if (argvars[1].v_type == VAR_UNKNOWN)
16590 {
16591 end = start - 1;
16592 start = 0;
16593 }
16594 else
16595 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016596 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016597 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016598 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016599 }
16600
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016601 if (error)
16602 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016603 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016604 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016605 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016606 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016607 else
16608 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016609 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016610 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016611 if (list_append_number(rettv->vval.v_list,
16612 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016613 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016614 }
16615}
16616
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016617/*
16618 * "readfile()" function
16619 */
16620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016621f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016622{
16623 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016624 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016625 char_u *fname;
16626 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016627 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16628 int io_size = sizeof(buf);
16629 int readlen; /* size of last fread() */
16630 char_u *prev = NULL; /* previously read bytes, if any */
16631 long prevlen = 0; /* length of data in prev */
16632 long prevsize = 0; /* size of prev buffer */
16633 long maxline = MAXLNUM;
16634 long cnt = 0;
16635 char_u *p; /* position in buf */
16636 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016637
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016638 if (argvars[1].v_type != VAR_UNKNOWN)
16639 {
16640 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16641 binary = TRUE;
16642 if (argvars[2].v_type != VAR_UNKNOWN)
16643 maxline = get_tv_number(&argvars[2]);
16644 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016645
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016646 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648
16649 /* Always open the file in binary mode, library functions have a mind of
16650 * their own about CR-LF conversion. */
16651 fname = get_tv_string(&argvars[0]);
16652 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16653 {
16654 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16655 return;
16656 }
16657
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016658 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016659 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016660 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016661
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016662 /* This for loop processes what was read, but is also entered at end
16663 * of file so that either:
16664 * - an incomplete line gets written
16665 * - a "binary" file gets an empty line at the end if it ends in a
16666 * newline. */
16667 for (p = buf, start = buf;
16668 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16669 ++p)
16670 {
16671 if (*p == '\n' || readlen <= 0)
16672 {
16673 listitem_T *li;
16674 char_u *s = NULL;
16675 long_u len = p - start;
16676
16677 /* Finished a line. Remove CRs before NL. */
16678 if (readlen > 0 && !binary)
16679 {
16680 while (len > 0 && start[len - 1] == '\r')
16681 --len;
16682 /* removal may cross back to the "prev" string */
16683 if (len == 0)
16684 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16685 --prevlen;
16686 }
16687 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016688 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016689 else
16690 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016691 /* Change "prev" buffer to be the right size. This way
16692 * the bytes are only copied once, and very long lines are
16693 * allocated only once. */
16694 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016695 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016696 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016697 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016698 prev = NULL; /* the list will own the string */
16699 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016700 }
16701 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016702 if (s == NULL)
16703 {
16704 do_outofmem_msg((long_u) prevlen + len + 1);
16705 failed = TRUE;
16706 break;
16707 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016708
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016709 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016710 {
16711 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016712 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016713 break;
16714 }
16715 li->li_tv.v_type = VAR_STRING;
16716 li->li_tv.v_lock = 0;
16717 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016718 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016719
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016720 start = p + 1; /* step over newline */
16721 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016722 break;
16723 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016724 else if (*p == NUL)
16725 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016726#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016727 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16728 * when finding the BF and check the previous two bytes. */
16729 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016730 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016731 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16732 * + 1, these may be in the "prev" string. */
16733 char_u back1 = p >= buf + 1 ? p[-1]
16734 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16735 char_u back2 = p >= buf + 2 ? p[-2]
16736 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16737 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016738
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016739 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016740 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016741 char_u *dest = p - 2;
16742
16743 /* Usually a BOM is at the beginning of a file, and so at
16744 * the beginning of a line; then we can just step over it.
16745 */
16746 if (start == dest)
16747 start = p + 1;
16748 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016749 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016750 /* have to shuffle buf to close gap */
16751 int adjust_prevlen = 0;
16752
16753 if (dest < buf)
16754 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016755 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016756 dest = buf;
16757 }
16758 if (readlen > p - buf + 1)
16759 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16760 readlen -= 3 - adjust_prevlen;
16761 prevlen -= adjust_prevlen;
16762 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016763 }
16764 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016765 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016766#endif
16767 } /* for */
16768
16769 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16770 break;
16771 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016772 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016773 /* There's part of a line in buf, store it in "prev". */
16774 if (p - start + prevlen >= prevsize)
16775 {
16776 /* need bigger "prev" buffer */
16777 char_u *newprev;
16778
16779 /* A common use case is ordinary text files and "prev" gets a
16780 * fragment of a line, so the first allocation is made
16781 * small, to avoid repeatedly 'allocing' large and
16782 * 'reallocing' small. */
16783 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016784 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016785 else
16786 {
16787 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016788 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016789 prevsize = grow50pc > growmin ? grow50pc : growmin;
16790 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016791 newprev = prev == NULL ? alloc(prevsize)
16792 : vim_realloc(prev, prevsize);
16793 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016794 {
16795 do_outofmem_msg((long_u)prevsize);
16796 failed = TRUE;
16797 break;
16798 }
16799 prev = newprev;
16800 }
16801 /* Add the line part to end of "prev". */
16802 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016803 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016804 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016805 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016806
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016807 /*
16808 * For a negative line count use only the lines at the end of the file,
16809 * free the rest.
16810 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016811 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016812 while (cnt > -maxline)
16813 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016814 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016815 --cnt;
16816 }
16817
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016818 if (failed)
16819 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020016820 list_free(rettv->vval.v_list);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016821 /* readfile doc says an empty list is returned on error */
16822 rettv->vval.v_list = list_alloc();
16823 }
16824
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016825 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016826 fclose(fd);
16827}
16828
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016829#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016830static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016831
16832/*
16833 * Convert a List to proftime_T.
16834 * Return FAIL when there is something wrong.
16835 */
16836 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016837list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016838{
16839 long n1, n2;
16840 int error = FALSE;
16841
16842 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16843 || arg->vval.v_list->lv_len != 2)
16844 return FAIL;
16845 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16846 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16847# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016848 tm->HighPart = n1;
16849 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016850# else
16851 tm->tv_sec = n1;
16852 tm->tv_usec = n2;
16853# endif
16854 return error ? FAIL : OK;
16855}
16856#endif /* FEAT_RELTIME */
16857
16858/*
16859 * "reltime()" function
16860 */
16861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016862f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016863{
16864#ifdef FEAT_RELTIME
16865 proftime_T res;
16866 proftime_T start;
16867
16868 if (argvars[0].v_type == VAR_UNKNOWN)
16869 {
16870 /* No arguments: get current time. */
16871 profile_start(&res);
16872 }
16873 else if (argvars[1].v_type == VAR_UNKNOWN)
16874 {
16875 if (list2proftime(&argvars[0], &res) == FAIL)
16876 return;
16877 profile_end(&res);
16878 }
16879 else
16880 {
16881 /* Two arguments: compute the difference. */
16882 if (list2proftime(&argvars[0], &start) == FAIL
16883 || list2proftime(&argvars[1], &res) == FAIL)
16884 return;
16885 profile_sub(&res, &start);
16886 }
16887
16888 if (rettv_list_alloc(rettv) == OK)
16889 {
16890 long n1, n2;
16891
16892# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016893 n1 = res.HighPart;
16894 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016895# else
16896 n1 = res.tv_sec;
16897 n2 = res.tv_usec;
16898# endif
16899 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16900 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16901 }
16902#endif
16903}
16904
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016905#ifdef FEAT_FLOAT
16906/*
16907 * "reltimefloat()" function
16908 */
16909 static void
16910f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16911{
16912# ifdef FEAT_RELTIME
16913 proftime_T tm;
16914# endif
16915
16916 rettv->v_type = VAR_FLOAT;
16917 rettv->vval.v_float = 0;
16918# ifdef FEAT_RELTIME
16919 if (list2proftime(&argvars[0], &tm) == OK)
16920 rettv->vval.v_float = profile_float(&tm);
16921# endif
16922}
16923#endif
16924
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016925/*
16926 * "reltimestr()" function
16927 */
16928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016929f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016930{
16931#ifdef FEAT_RELTIME
16932 proftime_T tm;
16933#endif
16934
16935 rettv->v_type = VAR_STRING;
16936 rettv->vval.v_string = NULL;
16937#ifdef FEAT_RELTIME
16938 if (list2proftime(&argvars[0], &tm) == OK)
16939 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16940#endif
16941}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016942
Bram Moolenaar0d660222005-01-07 21:51:51 +000016943#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016944static void make_connection(void);
16945static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016946
16947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016948make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949{
16950 if (X_DISPLAY == NULL
16951# ifdef FEAT_GUI
16952 && !gui.in_use
16953# endif
16954 )
16955 {
16956 x_force_connect = TRUE;
16957 setup_term_clip();
16958 x_force_connect = FALSE;
16959 }
16960}
16961
16962 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016963check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964{
16965 make_connection();
16966 if (X_DISPLAY == NULL)
16967 {
16968 EMSG(_("E240: No connection to Vim server"));
16969 return FAIL;
16970 }
16971 return OK;
16972}
16973#endif
16974
16975#ifdef FEAT_CLIENTSERVER
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016977remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016978{
16979 char_u *server_name;
16980 char_u *keys;
16981 char_u *r = NULL;
16982 char_u buf[NUMBUFLEN];
16983# ifdef WIN32
16984 HWND w;
16985# else
16986 Window w;
16987# endif
16988
16989 if (check_restricted() || check_secure())
16990 return;
16991
16992# ifdef FEAT_X11
16993 if (check_connection() == FAIL)
16994 return;
16995# endif
16996
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016997 server_name = get_tv_string_chk(&argvars[0]);
16998 if (server_name == NULL)
16999 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 keys = get_tv_string_buf(&argvars[1], buf);
17001# ifdef WIN32
17002 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17003# else
17004 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17005 < 0)
17006# endif
17007 {
17008 if (r != NULL)
17009 EMSG(r); /* sending worked but evaluation failed */
17010 else
17011 EMSG2(_("E241: Unable to send to %s"), server_name);
17012 return;
17013 }
17014
17015 rettv->vval.v_string = r;
17016
17017 if (argvars[2].v_type != VAR_UNKNOWN)
17018 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017019 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017020 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017021 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017022
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017023 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017024 v.di_tv.v_type = VAR_STRING;
17025 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017026 idvar = get_tv_string_chk(&argvars[2]);
17027 if (idvar != NULL)
17028 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017029 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 }
17031}
17032#endif
17033
17034/*
17035 * "remote_expr()" function
17036 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017038f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039{
17040 rettv->v_type = VAR_STRING;
17041 rettv->vval.v_string = NULL;
17042#ifdef FEAT_CLIENTSERVER
17043 remote_common(argvars, rettv, TRUE);
17044#endif
17045}
17046
17047/*
17048 * "remote_foreground()" function
17049 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017051f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053#ifdef FEAT_CLIENTSERVER
17054# ifdef WIN32
17055 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017056 {
17057 char_u *server_name = get_tv_string_chk(&argvars[0]);
17058
17059 if (server_name != NULL)
17060 serverForeground(server_name);
17061 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062# else
17063 /* Send a foreground() expression to the server. */
17064 argvars[1].v_type = VAR_STRING;
17065 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17066 argvars[2].v_type = VAR_UNKNOWN;
17067 remote_common(argvars, rettv, TRUE);
17068 vim_free(argvars[1].vval.v_string);
17069# endif
17070#endif
17071}
17072
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017074f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075{
17076#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017077 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 char_u *s = NULL;
17079# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017080 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083
17084 if (check_restricted() || check_secure())
17085 {
17086 rettv->vval.v_number = -1;
17087 return;
17088 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017089 serverid = get_tv_string_chk(&argvars[0]);
17090 if (serverid == NULL)
17091 {
17092 rettv->vval.v_number = -1;
17093 return; /* type error; errmsg already given */
17094 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017096 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017097 if (n == 0)
17098 rettv->vval.v_number = -1;
17099 else
17100 {
17101 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17102 rettv->vval.v_number = (s != NULL);
17103 }
17104# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105 if (check_connection() == FAIL)
17106 return;
17107
17108 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017109 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110# endif
17111
17112 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017114 char_u *retvar;
17115
Bram Moolenaar33570922005-01-25 22:26:29 +000017116 v.di_tv.v_type = VAR_STRING;
17117 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017118 retvar = get_tv_string_chk(&argvars[1]);
17119 if (retvar != NULL)
17120 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017121 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122 }
17123#else
17124 rettv->vval.v_number = -1;
17125#endif
17126}
17127
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017129f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130{
17131 char_u *r = NULL;
17132
17133#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017134 char_u *serverid = get_tv_string_chk(&argvars[0]);
17135
17136 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017137 {
17138# ifdef WIN32
17139 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017140 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017142 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 if (n != 0)
17144 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17145 if (r == NULL)
17146# else
17147 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017148 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149# endif
17150 EMSG(_("E277: Unable to read a server reply"));
17151 }
17152#endif
17153 rettv->v_type = VAR_STRING;
17154 rettv->vval.v_string = r;
17155}
17156
17157/*
17158 * "remote_send()" function
17159 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017161f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017162{
17163 rettv->v_type = VAR_STRING;
17164 rettv->vval.v_string = NULL;
17165#ifdef FEAT_CLIENTSERVER
17166 remote_common(argvars, rettv, FALSE);
17167#endif
17168}
17169
17170/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017171 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017172 */
17173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017174f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017175{
Bram Moolenaar33570922005-01-25 22:26:29 +000017176 list_T *l;
17177 listitem_T *item, *item2;
17178 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017179 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017180 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017181 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017182 dict_T *d;
17183 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017184 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017185
Bram Moolenaar8c711452005-01-14 21:53:12 +000017186 if (argvars[0].v_type == VAR_DICT)
17187 {
17188 if (argvars[2].v_type != VAR_UNKNOWN)
17189 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017190 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017191 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017193 key = get_tv_string_chk(&argvars[1]);
17194 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017196 di = dict_find(d, key, -1);
17197 if (di == NULL)
17198 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017199 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17200 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017201 {
17202 *rettv = di->di_tv;
17203 init_tv(&di->di_tv);
17204 dictitem_remove(d, di);
17205 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017206 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017207 }
17208 }
17209 else if (argvars[0].v_type != VAR_LIST)
17210 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017211 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017212 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 int error = FALSE;
17215
17216 idx = get_tv_number_chk(&argvars[1], &error);
17217 if (error)
17218 ; /* type error: do nothing, errmsg already given */
17219 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017220 EMSGN(_(e_listidx), idx);
17221 else
17222 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017223 if (argvars[2].v_type == VAR_UNKNOWN)
17224 {
17225 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017226 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017227 *rettv = item->li_tv;
17228 vim_free(item);
17229 }
17230 else
17231 {
17232 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017233 end = get_tv_number_chk(&argvars[2], &error);
17234 if (error)
17235 ; /* type error: do nothing */
17236 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017237 EMSGN(_(e_listidx), end);
17238 else
17239 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017240 int cnt = 0;
17241
17242 for (li = item; li != NULL; li = li->li_next)
17243 {
17244 ++cnt;
17245 if (li == item2)
17246 break;
17247 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017248 if (li == NULL) /* didn't find "item2" after "item" */
17249 EMSG(_(e_invrange));
17250 else
17251 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017252 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017253 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017254 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017255 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017256 l->lv_first = item;
17257 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017258 item->li_prev = NULL;
17259 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017260 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017261 }
17262 }
17263 }
17264 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017265 }
17266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267}
17268
17269/*
17270 * "rename({from}, {to})" function
17271 */
17272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017273f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274{
17275 char_u buf[NUMBUFLEN];
17276
17277 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017278 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017280 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17281 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282}
17283
17284/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017285 * "repeat()" function
17286 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017288f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017289{
17290 char_u *p;
17291 int n;
17292 int slen;
17293 int len;
17294 char_u *r;
17295 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017296
17297 n = get_tv_number(&argvars[1]);
17298 if (argvars[0].v_type == VAR_LIST)
17299 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017300 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017301 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017302 if (list_extend(rettv->vval.v_list,
17303 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017304 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017305 }
17306 else
17307 {
17308 p = get_tv_string(&argvars[0]);
17309 rettv->v_type = VAR_STRING;
17310 rettv->vval.v_string = NULL;
17311
17312 slen = (int)STRLEN(p);
17313 len = slen * n;
17314 if (len <= 0)
17315 return;
17316
17317 r = alloc(len + 1);
17318 if (r != NULL)
17319 {
17320 for (i = 0; i < n; i++)
17321 mch_memmove(r + i * slen, p, (size_t)slen);
17322 r[len] = NUL;
17323 }
17324
17325 rettv->vval.v_string = r;
17326 }
17327}
17328
17329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 * "resolve()" function
17331 */
17332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017333f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017334{
17335 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017336#ifdef HAVE_READLINK
17337 char_u *buf = NULL;
17338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017340 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341#ifdef FEAT_SHORTCUT
17342 {
17343 char_u *v = NULL;
17344
17345 v = mch_resolve_shortcut(p);
17346 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017347 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017349 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350 }
17351#else
17352# ifdef HAVE_READLINK
17353 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 char_u *cpy;
17355 int len;
17356 char_u *remain = NULL;
17357 char_u *q;
17358 int is_relative_to_current = FALSE;
17359 int has_trailing_pathsep = FALSE;
17360 int limit = 100;
17361
17362 p = vim_strsave(p);
17363
17364 if (p[0] == '.' && (vim_ispathsep(p[1])
17365 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17366 is_relative_to_current = TRUE;
17367
17368 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017369 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017370 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017371 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017372 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017374
17375 q = getnextcomp(p);
17376 if (*q != NUL)
17377 {
17378 /* Separate the first path component in "p", and keep the
17379 * remainder (beginning with the path separator). */
17380 remain = vim_strsave(q - 1);
17381 q[-1] = NUL;
17382 }
17383
Bram Moolenaard9462e32011-04-11 21:35:11 +020017384 buf = alloc(MAXPATHL + 1);
17385 if (buf == NULL)
17386 goto fail;
17387
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 for (;;)
17389 {
17390 for (;;)
17391 {
17392 len = readlink((char *)p, (char *)buf, MAXPATHL);
17393 if (len <= 0)
17394 break;
17395 buf[len] = NUL;
17396
17397 if (limit-- == 0)
17398 {
17399 vim_free(p);
17400 vim_free(remain);
17401 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017402 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403 goto fail;
17404 }
17405
17406 /* Ensure that the result will have a trailing path separator
17407 * if the argument has one. */
17408 if (remain == NULL && has_trailing_pathsep)
17409 add_pathsep(buf);
17410
17411 /* Separate the first path component in the link value and
17412 * concatenate the remainders. */
17413 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17414 if (*q != NUL)
17415 {
17416 if (remain == NULL)
17417 remain = vim_strsave(q - 1);
17418 else
17419 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017420 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 if (cpy != NULL)
17422 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017423 vim_free(remain);
17424 remain = cpy;
17425 }
17426 }
17427 q[-1] = NUL;
17428 }
17429
17430 q = gettail(p);
17431 if (q > p && *q == NUL)
17432 {
17433 /* Ignore trailing path separator. */
17434 q[-1] = NUL;
17435 q = gettail(p);
17436 }
17437 if (q > p && !mch_isFullName(buf))
17438 {
17439 /* symlink is relative to directory of argument */
17440 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17441 if (cpy != NULL)
17442 {
17443 STRCPY(cpy, p);
17444 STRCPY(gettail(cpy), buf);
17445 vim_free(p);
17446 p = cpy;
17447 }
17448 }
17449 else
17450 {
17451 vim_free(p);
17452 p = vim_strsave(buf);
17453 }
17454 }
17455
17456 if (remain == NULL)
17457 break;
17458
17459 /* Append the first path component of "remain" to "p". */
17460 q = getnextcomp(remain + 1);
17461 len = q - remain - (*q != NUL);
17462 cpy = vim_strnsave(p, STRLEN(p) + len);
17463 if (cpy != NULL)
17464 {
17465 STRNCAT(cpy, remain, len);
17466 vim_free(p);
17467 p = cpy;
17468 }
17469 /* Shorten "remain". */
17470 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017471 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 else
17473 {
17474 vim_free(remain);
17475 remain = NULL;
17476 }
17477 }
17478
17479 /* If the result is a relative path name, make it explicitly relative to
17480 * the current directory if and only if the argument had this form. */
17481 if (!vim_ispathsep(*p))
17482 {
17483 if (is_relative_to_current
17484 && *p != NUL
17485 && !(p[0] == '.'
17486 && (p[1] == NUL
17487 || vim_ispathsep(p[1])
17488 || (p[1] == '.'
17489 && (p[2] == NUL
17490 || vim_ispathsep(p[2]))))))
17491 {
17492 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017493 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017494 if (cpy != NULL)
17495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 vim_free(p);
17497 p = cpy;
17498 }
17499 }
17500 else if (!is_relative_to_current)
17501 {
17502 /* Strip leading "./". */
17503 q = p;
17504 while (q[0] == '.' && vim_ispathsep(q[1]))
17505 q += 2;
17506 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017507 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017508 }
17509 }
17510
17511 /* Ensure that the result will have no trailing path separator
17512 * if the argument had none. But keep "/" or "//". */
17513 if (!has_trailing_pathsep)
17514 {
17515 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017516 if (after_pathsep(p, q))
17517 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518 }
17519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017520 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 }
17522# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017524# endif
17525#endif
17526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017527 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528
17529#ifdef HAVE_READLINK
17530fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017531 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017532#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017533 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534}
17535
17536/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017537 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017538 */
17539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017540f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541{
Bram Moolenaar33570922005-01-25 22:26:29 +000017542 list_T *l;
17543 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544
Bram Moolenaar0d660222005-01-07 21:51:51 +000017545 if (argvars[0].v_type != VAR_LIST)
17546 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017547 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017548 && !tv_check_lock(l->lv_lock,
17549 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017550 {
17551 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017552 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017553 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017554 while (li != NULL)
17555 {
17556 ni = li->li_prev;
17557 list_append(l, li);
17558 li = ni;
17559 }
17560 rettv->vval.v_list = l;
17561 rettv->v_type = VAR_LIST;
17562 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017563 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565}
17566
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017567#define SP_NOMOVE 0x01 /* don't move cursor */
17568#define SP_REPEAT 0x02 /* repeat to find outer pair */
17569#define SP_RETCOUNT 0x04 /* return matchcount */
17570#define SP_SETPCMARK 0x08 /* set previous context mark */
17571#define SP_START 0x10 /* accept match at start position */
17572#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17573#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017574#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017575
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017576static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017577
17578/*
17579 * Get flags for a search function.
17580 * Possibly sets "p_ws".
17581 * Returns BACKWARD, FORWARD or zero (for an error).
17582 */
17583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017584get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585{
17586 int dir = FORWARD;
17587 char_u *flags;
17588 char_u nbuf[NUMBUFLEN];
17589 int mask;
17590
17591 if (varp->v_type != VAR_UNKNOWN)
17592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017593 flags = get_tv_string_buf_chk(varp, nbuf);
17594 if (flags == NULL)
17595 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017596 while (*flags != NUL)
17597 {
17598 switch (*flags)
17599 {
17600 case 'b': dir = BACKWARD; break;
17601 case 'w': p_ws = TRUE; break;
17602 case 'W': p_ws = FALSE; break;
17603 default: mask = 0;
17604 if (flagsp != NULL)
17605 switch (*flags)
17606 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017607 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017608 case 'e': mask = SP_END; break;
17609 case 'm': mask = SP_RETCOUNT; break;
17610 case 'n': mask = SP_NOMOVE; break;
17611 case 'p': mask = SP_SUBPAT; break;
17612 case 'r': mask = SP_REPEAT; break;
17613 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017614 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017615 }
17616 if (mask == 0)
17617 {
17618 EMSG2(_(e_invarg2), flags);
17619 dir = 0;
17620 }
17621 else
17622 *flagsp |= mask;
17623 }
17624 if (dir == 0)
17625 break;
17626 ++flags;
17627 }
17628 }
17629 return dir;
17630}
17631
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017633 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017636search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017638 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 char_u *pat;
17640 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017641 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 int save_p_ws = p_ws;
17643 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017644 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017645 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017646 proftime_T tm;
17647#ifdef FEAT_RELTIME
17648 long time_limit = 0;
17649#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017650 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017651 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017653 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017654 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017655 if (dir == 0)
17656 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017657 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017658 if (flags & SP_START)
17659 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017660 if (flags & SP_END)
17661 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017662 if (flags & SP_COLUMN)
17663 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017664
Bram Moolenaar76929292008-01-06 19:07:36 +000017665 /* Optional arguments: line number to stop searching and timeout. */
17666 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017667 {
17668 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17669 if (lnum_stop < 0)
17670 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017671#ifdef FEAT_RELTIME
17672 if (argvars[3].v_type != VAR_UNKNOWN)
17673 {
17674 time_limit = get_tv_number_chk(&argvars[3], NULL);
17675 if (time_limit < 0)
17676 goto theend;
17677 }
17678#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017679 }
17680
Bram Moolenaar76929292008-01-06 19:07:36 +000017681#ifdef FEAT_RELTIME
17682 /* Set the time limit, if there is one. */
17683 profile_setlimit(time_limit, &tm);
17684#endif
17685
Bram Moolenaar231334e2005-07-25 20:46:57 +000017686 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017687 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017688 * Check to make sure only those flags are set.
17689 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17690 * flags cannot be set. Check for that condition also.
17691 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017692 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017693 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017694 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017695 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017696 goto theend;
17697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017699 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017700 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017701 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017702 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017704 if (flags & SP_SUBPAT)
17705 retval = subpatnum;
17706 else
17707 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017708 if (flags & SP_SETPCMARK)
17709 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017711 if (match_pos != NULL)
17712 {
17713 /* Store the match cursor position */
17714 match_pos->lnum = pos.lnum;
17715 match_pos->col = pos.col + 1;
17716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 /* "/$" will put the cursor after the end of the line, may need to
17718 * correct that here */
17719 check_cursor();
17720 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017721
17722 /* If 'n' flag is used: restore cursor position. */
17723 if (flags & SP_NOMOVE)
17724 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017725 else
17726 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017727theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729
17730 return retval;
17731}
17732
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017733#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017734
17735/*
17736 * round() is not in C90, use ceil() or floor() instead.
17737 */
17738 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017739vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017740{
17741 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17742}
17743
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017744/*
17745 * "round({float})" function
17746 */
17747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017748f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017749{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017750 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017751
17752 rettv->v_type = VAR_FLOAT;
17753 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017754 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017755 else
17756 rettv->vval.v_float = 0.0;
17757}
17758#endif
17759
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017760/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017761 * "screenattr()" function
17762 */
17763 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017764f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017765{
17766 int row;
17767 int col;
17768 int c;
17769
17770 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17771 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17772 if (row < 0 || row >= screen_Rows
17773 || col < 0 || col >= screen_Columns)
17774 c = -1;
17775 else
17776 c = ScreenAttrs[LineOffset[row] + col];
17777 rettv->vval.v_number = c;
17778}
17779
17780/*
17781 * "screenchar()" function
17782 */
17783 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017784f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017785{
17786 int row;
17787 int col;
17788 int off;
17789 int c;
17790
17791 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17792 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17793 if (row < 0 || row >= screen_Rows
17794 || col < 0 || col >= screen_Columns)
17795 c = -1;
17796 else
17797 {
17798 off = LineOffset[row] + col;
17799#ifdef FEAT_MBYTE
17800 if (enc_utf8 && ScreenLinesUC[off] != 0)
17801 c = ScreenLinesUC[off];
17802 else
17803#endif
17804 c = ScreenLines[off];
17805 }
17806 rettv->vval.v_number = c;
17807}
17808
17809/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017810 * "screencol()" function
17811 *
17812 * First column is 1 to be consistent with virtcol().
17813 */
17814 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017815f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017816{
17817 rettv->vval.v_number = screen_screencol() + 1;
17818}
17819
17820/*
17821 * "screenrow()" function
17822 */
17823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017824f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017825{
17826 rettv->vval.v_number = screen_screenrow() + 1;
17827}
17828
17829/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017830 * "search()" function
17831 */
17832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017833f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017834{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017835 int flags = 0;
17836
17837 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017838}
17839
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017841 * "searchdecl()" function
17842 */
17843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017844f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017845{
17846 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017847 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017848 int error = FALSE;
17849 char_u *name;
17850
17851 rettv->vval.v_number = 1; /* default: FAIL */
17852
17853 name = get_tv_string_chk(&argvars[0]);
17854 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017855 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017856 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017857 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17858 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17859 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017860 if (!error && name != NULL)
17861 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017862 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017863}
17864
17865/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017866 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017869searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870{
17871 char_u *spat, *mpat, *epat;
17872 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017873 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874 int dir;
17875 int flags = 0;
17876 char_u nbuf1[NUMBUFLEN];
17877 char_u nbuf2[NUMBUFLEN];
17878 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017879 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017880 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017881 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017882
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017884 spat = get_tv_string_chk(&argvars[0]);
17885 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17886 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17887 if (spat == NULL || mpat == NULL || epat == NULL)
17888 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 /* Handle the optional fourth argument: flags */
17891 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017892 if (dir == 0)
17893 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017894
17895 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017896 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17897 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017898 if ((flags & (SP_END | SP_SUBPAT)) != 0
17899 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017900 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017901 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017902 goto theend;
17903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017904
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017905 /* Using 'r' implies 'W', otherwise it doesn't work. */
17906 if (flags & SP_REPEAT)
17907 p_ws = FALSE;
17908
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017909 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017910 if (argvars[3].v_type == VAR_UNKNOWN
17911 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912 skip = (char_u *)"";
17913 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017915 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017916 if (argvars[5].v_type != VAR_UNKNOWN)
17917 {
17918 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17919 if (lnum_stop < 0)
17920 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017921#ifdef FEAT_RELTIME
17922 if (argvars[6].v_type != VAR_UNKNOWN)
17923 {
17924 time_limit = get_tv_number_chk(&argvars[6], NULL);
17925 if (time_limit < 0)
17926 goto theend;
17927 }
17928#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017929 }
17930 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017931 if (skip == NULL)
17932 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017933
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017934 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017935 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017936
17937theend:
17938 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017939
17940 return retval;
17941}
17942
17943/*
17944 * "searchpair()" function
17945 */
17946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017947f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017948{
17949 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17950}
17951
17952/*
17953 * "searchpairpos()" function
17954 */
17955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017956f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017957{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017958 pos_T match_pos;
17959 int lnum = 0;
17960 int col = 0;
17961
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017962 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017963 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017964
17965 if (searchpair_cmn(argvars, &match_pos) > 0)
17966 {
17967 lnum = match_pos.lnum;
17968 col = match_pos.col;
17969 }
17970
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017971 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17972 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017973}
17974
17975/*
17976 * Search for a start/middle/end thing.
17977 * Used by searchpair(), see its documentation for the details.
17978 * Returns 0 or -1 for no match,
17979 */
17980 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017981do_searchpair(
17982 char_u *spat, /* start pattern */
17983 char_u *mpat, /* middle pattern */
17984 char_u *epat, /* end pattern */
17985 int dir, /* BACKWARD or FORWARD */
17986 char_u *skip, /* skip expression */
17987 int flags, /* SP_SETPCMARK and other SP_ values */
17988 pos_T *match_pos,
17989 linenr_T lnum_stop, /* stop at this line if not zero */
17990 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017991{
17992 char_u *save_cpo;
17993 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17994 long retval = 0;
17995 pos_T pos;
17996 pos_T firstpos;
17997 pos_T foundpos;
17998 pos_T save_cursor;
17999 pos_T save_pos;
18000 int n;
18001 int r;
18002 int nest = 1;
18003 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018004 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018005 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018006
18007 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18008 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018009 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018010
Bram Moolenaar76929292008-01-06 19:07:36 +000018011#ifdef FEAT_RELTIME
18012 /* Set the time limit, if there is one. */
18013 profile_setlimit(time_limit, &tm);
18014#endif
18015
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018016 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18017 * start/middle/end (pat3, for the top pair). */
18018 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18019 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18020 if (pat2 == NULL || pat3 == NULL)
18021 goto theend;
18022 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18023 if (*mpat == NUL)
18024 STRCPY(pat3, pat2);
18025 else
18026 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18027 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018028 if (flags & SP_START)
18029 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018030
Bram Moolenaar071d4272004-06-13 20:20:40 +000018031 save_cursor = curwin->w_cursor;
18032 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018033 clearpos(&firstpos);
18034 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 pat = pat3;
18036 for (;;)
18037 {
18038 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018039 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18041 /* didn't find it or found the first match again: FAIL */
18042 break;
18043
18044 if (firstpos.lnum == 0)
18045 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018046 if (equalpos(pos, foundpos))
18047 {
18048 /* Found the same position again. Can happen with a pattern that
18049 * has "\zs" at the end and searching backwards. Advance one
18050 * character and try again. */
18051 if (dir == BACKWARD)
18052 decl(&pos);
18053 else
18054 incl(&pos);
18055 }
18056 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018057
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018058 /* clear the start flag to avoid getting stuck here */
18059 options &= ~SEARCH_START;
18060
Bram Moolenaar071d4272004-06-13 20:20:40 +000018061 /* If the skip pattern matches, ignore this match. */
18062 if (*skip != NUL)
18063 {
18064 save_pos = curwin->w_cursor;
18065 curwin->w_cursor = pos;
18066 r = eval_to_bool(skip, &err, NULL, FALSE);
18067 curwin->w_cursor = save_pos;
18068 if (err)
18069 {
18070 /* Evaluating {skip} caused an error, break here. */
18071 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018072 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018073 break;
18074 }
18075 if (r)
18076 continue;
18077 }
18078
18079 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18080 {
18081 /* Found end when searching backwards or start when searching
18082 * forward: nested pair. */
18083 ++nest;
18084 pat = pat2; /* nested, don't search for middle */
18085 }
18086 else
18087 {
18088 /* Found end when searching forward or start when searching
18089 * backward: end of (nested) pair; or found middle in outer pair. */
18090 if (--nest == 1)
18091 pat = pat3; /* outer level, search for middle */
18092 }
18093
18094 if (nest == 0)
18095 {
18096 /* Found the match: return matchcount or line number. */
18097 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018098 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018100 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018101 if (flags & SP_SETPCMARK)
18102 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018103 curwin->w_cursor = pos;
18104 if (!(flags & SP_REPEAT))
18105 break;
18106 nest = 1; /* search for next unmatched */
18107 }
18108 }
18109
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018110 if (match_pos != NULL)
18111 {
18112 /* Store the match cursor position */
18113 match_pos->lnum = curwin->w_cursor.lnum;
18114 match_pos->col = curwin->w_cursor.col + 1;
18115 }
18116
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018118 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 curwin->w_cursor = save_cursor;
18120
18121theend:
18122 vim_free(pat2);
18123 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018124 if (p_cpo == empty_option)
18125 p_cpo = save_cpo;
18126 else
18127 /* Darn, evaluating the {skip} expression changed the value. */
18128 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018129
18130 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018131}
18132
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018133/*
18134 * "searchpos()" function
18135 */
18136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018137f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018138{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018139 pos_T match_pos;
18140 int lnum = 0;
18141 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018142 int n;
18143 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018144
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018145 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018146 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018147
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018148 n = search_cmn(argvars, &match_pos, &flags);
18149 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018150 {
18151 lnum = match_pos.lnum;
18152 col = match_pos.col;
18153 }
18154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018155 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18156 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018157 if (flags & SP_SUBPAT)
18158 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018159}
18160
Bram Moolenaar0d660222005-01-07 21:51:51 +000018161 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018162f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018164#ifdef FEAT_CLIENTSERVER
18165 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018166 char_u *server = get_tv_string_chk(&argvars[0]);
18167 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168
Bram Moolenaar0d660222005-01-07 21:51:51 +000018169 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018170 if (server == NULL || reply == NULL)
18171 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018172 if (check_restricted() || check_secure())
18173 return;
18174# ifdef FEAT_X11
18175 if (check_connection() == FAIL)
18176 return;
18177# endif
18178
18179 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018180 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181 EMSG(_("E258: Unable to send to client"));
18182 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018184 rettv->vval.v_number = 0;
18185#else
18186 rettv->vval.v_number = -1;
18187#endif
18188}
18189
Bram Moolenaar0d660222005-01-07 21:51:51 +000018190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018191f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018192{
18193 char_u *r = NULL;
18194
18195#ifdef FEAT_CLIENTSERVER
18196# ifdef WIN32
18197 r = serverGetVimNames();
18198# else
18199 make_connection();
18200 if (X_DISPLAY != NULL)
18201 r = serverGetVimNames(X_DISPLAY);
18202# endif
18203#endif
18204 rettv->v_type = VAR_STRING;
18205 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206}
18207
18208/*
18209 * "setbufvar()" function
18210 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018212f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018213{
18214 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018217 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018218 char_u nbuf[NUMBUFLEN];
18219
18220 if (check_restricted() || check_secure())
18221 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018222 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18223 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018224 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 varp = &argvars[2];
18226
18227 if (buf != NULL && varname != NULL && varp != NULL)
18228 {
18229 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231
18232 if (*varname == '&')
18233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018234 long numval;
18235 char_u *strval;
18236 int error = FALSE;
18237
Bram Moolenaar071d4272004-06-13 20:20:40 +000018238 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018239 numval = get_tv_number_chk(varp, &error);
18240 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018241 if (!error && strval != NULL)
18242 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 }
18244 else
18245 {
18246 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18247 if (bufvarname != NULL)
18248 {
18249 STRCPY(bufvarname, "b:");
18250 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018251 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 vim_free(bufvarname);
18253 }
18254 }
18255
18256 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018259}
18260
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018262f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018263{
18264 dict_T *d;
18265 dictitem_T *di;
18266 char_u *csearch;
18267
18268 if (argvars[0].v_type != VAR_DICT)
18269 {
18270 EMSG(_(e_dictreq));
18271 return;
18272 }
18273
18274 if ((d = argvars[0].vval.v_dict) != NULL)
18275 {
18276 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18277 if (csearch != NULL)
18278 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018279#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018280 if (enc_utf8)
18281 {
18282 int pcc[MAX_MCO];
18283 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018284
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018285 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18286 }
18287 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018288#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018289 set_last_csearch(PTR2CHAR(csearch),
18290 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018291 }
18292
18293 di = dict_find(d, (char_u *)"forward", -1);
18294 if (di != NULL)
18295 set_csearch_direction(get_tv_number(&di->di_tv)
18296 ? FORWARD : BACKWARD);
18297
18298 di = dict_find(d, (char_u *)"until", -1);
18299 if (di != NULL)
18300 set_csearch_until(!!get_tv_number(&di->di_tv));
18301 }
18302}
18303
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304/*
18305 * "setcmdpos()" function
18306 */
18307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018308f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018309{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018310 int pos = (int)get_tv_number(&argvars[0]) - 1;
18311
18312 if (pos >= 0)
18313 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314}
18315
18316/*
Bram Moolenaar80492532016-03-08 17:08:53 +010018317 * "setfperm({fname}, {mode})" function
18318 */
18319 static void
18320f_setfperm(typval_T *argvars, typval_T *rettv)
18321{
18322 char_u *fname;
18323 char_u modebuf[NUMBUFLEN];
18324 char_u *mode_str;
18325 int i;
18326 int mask;
18327 int mode = 0;
18328
18329 rettv->vval.v_number = 0;
18330 fname = get_tv_string_chk(&argvars[0]);
18331 if (fname == NULL)
18332 return;
18333 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
18334 if (mode_str == NULL)
18335 return;
18336 if (STRLEN(mode_str) != 9)
18337 {
18338 EMSG2(_(e_invarg2), mode_str);
18339 return;
18340 }
18341
18342 mask = 1;
18343 for (i = 8; i >= 0; --i)
18344 {
18345 if (mode_str[i] != '-')
18346 mode |= mask;
18347 mask = mask << 1;
18348 }
18349 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
18350}
18351
18352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353 * "setline()" function
18354 */
18355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018356f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357{
18358 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018359 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018360 list_T *l = NULL;
18361 listitem_T *li = NULL;
18362 long added = 0;
18363 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018364
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018365 lnum = get_tv_lnum(&argvars[0]);
18366 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018368 l = argvars[1].vval.v_list;
18369 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018371 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018372 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018373
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018374 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018375 for (;;)
18376 {
18377 if (l != NULL)
18378 {
18379 /* list argument, get next string */
18380 if (li == NULL)
18381 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018382 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018383 li = li->li_next;
18384 }
18385
18386 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018387 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018388 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018389
18390 /* When coming here from Insert mode, sync undo, so that this can be
18391 * undone separately from what was previously inserted. */
18392 if (u_sync_once == 2)
18393 {
18394 u_sync_once = 1; /* notify that u_sync() was called */
18395 u_sync(TRUE);
18396 }
18397
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018398 if (lnum <= curbuf->b_ml.ml_line_count)
18399 {
18400 /* existing line, replace it */
18401 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18402 {
18403 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018404 if (lnum == curwin->w_cursor.lnum)
18405 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018406 rettv->vval.v_number = 0; /* OK */
18407 }
18408 }
18409 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18410 {
18411 /* lnum is one past the last line, append the line */
18412 ++added;
18413 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18414 rettv->vval.v_number = 0; /* OK */
18415 }
18416
18417 if (l == NULL) /* only one string argument */
18418 break;
18419 ++lnum;
18420 }
18421
18422 if (added > 0)
18423 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424}
18425
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018426static 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 +000018427
Bram Moolenaar071d4272004-06-13 20:20:40 +000018428/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018429 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018430 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018432set_qf_ll_list(
18433 win_T *wp UNUSED,
18434 typval_T *list_arg UNUSED,
18435 typval_T *action_arg UNUSED,
18436 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018437{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018438#ifdef FEAT_QUICKFIX
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018439 static char *e_invact = N_("E927: Invalid action: '%s'");
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018440 char_u *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018441 int action = 0;
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018442#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018443
Bram Moolenaar2641f772005-03-25 21:58:17 +000018444 rettv->vval.v_number = -1;
18445
18446#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018447 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018448 EMSG(_(e_listreq));
18449 else
18450 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018451 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018452
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018453 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018454 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018455 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018456 if (act == NULL)
18457 return; /* type error; errmsg already given */
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018458 if ((*act == 'a' || *act == 'r' || *act == ' ') && act[1] == NUL)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018459 action = *act;
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018460 else
18461 EMSG2(_(e_invact), act);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018462 }
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018463 else if (action_arg->v_type == VAR_UNKNOWN)
18464 action = ' ';
18465 else
18466 EMSG(_(e_stringreq));
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018467
Bram Moolenaard106e5b2016-04-21 19:38:07 +020018468 if (l != NULL && action && set_errorlist(wp, l, action,
Bram Moolenaar81484f42012-12-05 15:16:47 +010018469 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018470 rettv->vval.v_number = 0;
18471 }
18472#endif
18473}
18474
18475/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018476 * "setloclist()" function
18477 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018479f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018480{
18481 win_T *win;
18482
18483 rettv->vval.v_number = -1;
18484
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018485 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018486 if (win != NULL)
18487 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18488}
18489
18490/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018491 * "setmatches()" function
18492 */
18493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018494f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018495{
18496#ifdef FEAT_SEARCH_EXTRA
18497 list_T *l;
18498 listitem_T *li;
18499 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018500 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018501
18502 rettv->vval.v_number = -1;
18503 if (argvars[0].v_type != VAR_LIST)
18504 {
18505 EMSG(_(e_listreq));
18506 return;
18507 }
18508 if ((l = argvars[0].vval.v_list) != NULL)
18509 {
18510
18511 /* To some extent make sure that we are dealing with a list from
18512 * "getmatches()". */
18513 li = l->lv_first;
18514 while (li != NULL)
18515 {
18516 if (li->li_tv.v_type != VAR_DICT
18517 || (d = li->li_tv.vval.v_dict) == NULL)
18518 {
18519 EMSG(_(e_invarg));
18520 return;
18521 }
18522 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018523 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18524 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018525 && dict_find(d, (char_u *)"priority", -1) != NULL
18526 && dict_find(d, (char_u *)"id", -1) != NULL))
18527 {
18528 EMSG(_(e_invarg));
18529 return;
18530 }
18531 li = li->li_next;
18532 }
18533
18534 clear_matches(curwin);
18535 li = l->lv_first;
18536 while (li != NULL)
18537 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018538 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018539 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018540 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018541 char_u *group;
18542 int priority;
18543 int id;
18544 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018545
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018546 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018547 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18548 {
18549 if (s == NULL)
18550 {
18551 s = list_alloc();
18552 if (s == NULL)
18553 return;
18554 }
18555
18556 /* match from matchaddpos() */
18557 for (i = 1; i < 9; i++)
18558 {
18559 sprintf((char *)buf, (char *)"pos%d", i);
18560 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18561 {
18562 if (di->di_tv.v_type != VAR_LIST)
18563 return;
18564
18565 list_append_tv(s, &di->di_tv);
18566 s->lv_refcount++;
18567 }
18568 else
18569 break;
18570 }
18571 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018572
18573 group = get_dict_string(d, (char_u *)"group", FALSE);
18574 priority = (int)get_dict_number(d, (char_u *)"priority");
18575 id = (int)get_dict_number(d, (char_u *)"id");
18576 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18577 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18578 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018579 if (i == 0)
18580 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018581 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018582 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018583 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018584 }
18585 else
18586 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018587 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018588 list_unref(s);
18589 s = NULL;
18590 }
18591
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018592 li = li->li_next;
18593 }
18594 rettv->vval.v_number = 0;
18595 }
18596#endif
18597}
18598
18599/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018600 * "setpos()" function
18601 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018603f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018604{
18605 pos_T pos;
18606 int fnum;
18607 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018608 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018609
Bram Moolenaar08250432008-02-13 11:42:46 +000018610 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018611 name = get_tv_string_chk(argvars);
18612 if (name != NULL)
18613 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018614 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018615 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018616 if (--pos.col < 0)
18617 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018618 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018619 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018620 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018621 if (fnum == curbuf->b_fnum)
18622 {
18623 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018624 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018625 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018626 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018627 curwin->w_set_curswant = FALSE;
18628 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018629 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018630 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018631 }
18632 else
18633 EMSG(_(e_invarg));
18634 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018635 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18636 {
18637 /* set mark */
18638 if (setmark_pos(name[1], &pos, fnum) == OK)
18639 rettv->vval.v_number = 0;
18640 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018641 else
18642 EMSG(_(e_invarg));
18643 }
18644 }
18645}
18646
18647/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018648 * "setqflist()" function
18649 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018651f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018652{
18653 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18654}
18655
18656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 * "setreg()" function
18658 */
18659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018660f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661{
18662 int regname;
18663 char_u *strregname;
18664 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018665 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 int append;
18667 char_u yank_type;
18668 long block_len;
18669
18670 block_len = -1;
18671 yank_type = MAUTO;
18672 append = FALSE;
18673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018674 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018677 if (strregname == NULL)
18678 return; /* type error; errmsg already given */
18679 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680 if (regname == 0 || regname == '@')
18681 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018682
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018683 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018685 stropt = get_tv_string_chk(&argvars[2]);
18686 if (stropt == NULL)
18687 return; /* type error */
18688 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689 switch (*stropt)
18690 {
18691 case 'a': case 'A': /* append */
18692 append = TRUE;
18693 break;
18694 case 'v': case 'c': /* character-wise selection */
18695 yank_type = MCHAR;
18696 break;
18697 case 'V': case 'l': /* line-wise selection */
18698 yank_type = MLINE;
18699 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 case 'b': case Ctrl_V: /* block-wise selection */
18701 yank_type = MBLOCK;
18702 if (VIM_ISDIGIT(stropt[1]))
18703 {
18704 ++stropt;
18705 block_len = getdigits(&stropt) - 1;
18706 --stropt;
18707 }
18708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 }
18710 }
18711
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018712 if (argvars[1].v_type == VAR_LIST)
18713 {
18714 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018715 char_u **allocval;
18716 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018717 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018718 char_u **curallocval;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018719 list_T *ll = argvars[1].vval.v_list;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018720 listitem_T *li;
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018721 int len;
18722
18723 /* If the list is NULL handle like an empty list. */
18724 len = ll == NULL ? 0 : ll->lv_len;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018725
Bram Moolenaar7d647822014-04-05 21:28:56 +020018726 /* First half: use for pointers to result lines; second half: use for
18727 * pointers to allocated copies. */
18728 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018729 if (lstval == NULL)
18730 return;
18731 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018732 allocval = lstval + len + 2;
18733 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018734
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020018735 for (li = ll == NULL ? NULL : ll->lv_first; li != NULL;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018736 li = li->li_next)
18737 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018738 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018739 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018740 goto free_lstval;
18741 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018742 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018743 /* Need to make a copy, next get_tv_string_buf_chk() will
18744 * overwrite the string. */
18745 strval = vim_strsave(buf);
18746 if (strval == NULL)
18747 goto free_lstval;
18748 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018749 }
18750 *curval++ = strval;
18751 }
18752 *curval++ = NULL;
18753
18754 write_reg_contents_lst(regname, lstval, -1,
18755 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018756free_lstval:
18757 while (curallocval > allocval)
18758 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018759 vim_free(lstval);
18760 }
18761 else
18762 {
18763 strval = get_tv_string_chk(&argvars[1]);
18764 if (strval == NULL)
18765 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018766 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018767 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018769 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018770}
18771
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018772/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018773 * "settabvar()" function
18774 */
18775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018776f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018777{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018778#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018779 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018780 tabpage_T *tp;
18781#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018782 char_u *varname, *tabvarname;
18783 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018784
18785 rettv->vval.v_number = 0;
18786
18787 if (check_restricted() || check_secure())
18788 return;
18789
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018790#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018791 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018792#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018793 varname = get_tv_string_chk(&argvars[1]);
18794 varp = &argvars[2];
18795
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018796 if (varname != NULL && varp != NULL
18797#ifdef FEAT_WINDOWS
18798 && tp != NULL
18799#endif
18800 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018801 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018802#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018803 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018804 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018805#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018806
18807 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18808 if (tabvarname != NULL)
18809 {
18810 STRCPY(tabvarname, "t:");
18811 STRCPY(tabvarname + 2, varname);
18812 set_var(tabvarname, varp, TRUE);
18813 vim_free(tabvarname);
18814 }
18815
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018816#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018817 /* Restore current tabpage */
18818 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018819 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018820#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018821 }
18822}
18823
18824/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018825 * "settabwinvar()" function
18826 */
18827 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018828f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018829{
18830 setwinvar(argvars, rettv, 1);
18831}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018832
18833/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018834 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018837f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018839 setwinvar(argvars, rettv, 0);
18840}
18841
18842/*
18843 * "setwinvar()" and "settabwinvar()" functions
18844 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018845
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018847setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018848{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 win_T *win;
18850#ifdef FEAT_WINDOWS
18851 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018852 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018853 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018854#endif
18855 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018856 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018857 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018858 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018859
18860 if (check_restricted() || check_secure())
18861 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018862
18863#ifdef FEAT_WINDOWS
18864 if (off == 1)
18865 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18866 else
18867 tp = curtab;
18868#endif
18869 win = find_win_by_nr(&argvars[off], tp);
18870 varname = get_tv_string_chk(&argvars[off + 1]);
18871 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872
18873 if (win != NULL && varname != NULL && varp != NULL)
18874 {
18875#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018876 need_switch_win = !(tp == curtab && win == curwin);
18877 if (!need_switch_win
18878 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018881 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018883 long numval;
18884 char_u *strval;
18885 int error = FALSE;
18886
18887 ++varname;
18888 numval = get_tv_number_chk(varp, &error);
18889 strval = get_tv_string_buf_chk(varp, nbuf);
18890 if (!error && strval != NULL)
18891 set_option_value(varname, numval, strval, OPT_LOCAL);
18892 }
18893 else
18894 {
18895 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18896 if (winvarname != NULL)
18897 {
18898 STRCPY(winvarname, "w:");
18899 STRCPY(winvarname + 2, varname);
18900 set_var(winvarname, varp, TRUE);
18901 vim_free(winvarname);
18902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018903 }
18904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018906 if (need_switch_win)
18907 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018908#endif
18909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018910}
18911
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018912#ifdef FEAT_CRYPT
18913/*
18914 * "sha256({string})" function
18915 */
18916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018917f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018918{
18919 char_u *p;
18920
18921 p = get_tv_string(&argvars[0]);
18922 rettv->vval.v_string = vim_strsave(
18923 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18924 rettv->v_type = VAR_STRING;
18925}
18926#endif /* FEAT_CRYPT */
18927
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018929 * "shellescape({string})" function
18930 */
18931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018932f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018933{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018934 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018935 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018936 rettv->v_type = VAR_STRING;
18937}
18938
18939/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018940 * shiftwidth() function
18941 */
18942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018943f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018944{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018945 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018946}
18947
18948/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018949 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 */
18951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018952f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018953{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018954 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018955
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956 p = get_tv_string(&argvars[0]);
18957 rettv->vval.v_string = vim_strsave(p);
18958 simplify_filename(rettv->vval.v_string); /* simplify in place */
18959 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018960}
18961
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018962#ifdef FEAT_FLOAT
18963/*
18964 * "sin()" function
18965 */
18966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018967f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018968{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018969 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018970
18971 rettv->v_type = VAR_FLOAT;
18972 if (get_float_arg(argvars, &f) == OK)
18973 rettv->vval.v_float = sin(f);
18974 else
18975 rettv->vval.v_float = 0.0;
18976}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018977
18978/*
18979 * "sinh()" function
18980 */
18981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018982f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018983{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018984 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018985
18986 rettv->v_type = VAR_FLOAT;
18987 if (get_float_arg(argvars, &f) == OK)
18988 rettv->vval.v_float = sinh(f);
18989 else
18990 rettv->vval.v_float = 0.0;
18991}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018992#endif
18993
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994static int
18995#ifdef __BORLANDC__
18996 _RTLENTRYF
18997#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018998 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999static int
19000#ifdef __BORLANDC__
19001 _RTLENTRYF
19002#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019003 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019005/* struct used in the array that's given to qsort() */
19006typedef struct
19007{
19008 listitem_T *item;
19009 int idx;
19010} sortItem_T;
19011
Bram Moolenaar0b962472016-02-22 22:51:33 +010019012/* struct storing information about current sort */
19013typedef struct
19014{
19015 int item_compare_ic;
19016 int item_compare_numeric;
19017 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019018#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019019 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019020#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019021 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019022 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019023 dict_T *item_compare_selfdict;
19024 int item_compare_func_err;
19025 int item_compare_keep_zero;
19026} sortinfo_T;
19027static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019028static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019029#define ITEM_COMPARE_FAIL 999
19030
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019032 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019034 static int
19035#ifdef __BORLANDC__
19036_RTLENTRYF
19037#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019038item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019040 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019041 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019042 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019043 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019044 int res;
19045 char_u numbuf1[NUMBUFLEN];
19046 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019047
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019048 si1 = (sortItem_T *)s1;
19049 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019050 tv1 = &si1->item->li_tv;
19051 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019052
Bram Moolenaar0b962472016-02-22 22:51:33 +010019053 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019054 {
19055 long v1 = get_tv_number(tv1);
19056 long v2 = get_tv_number(tv2);
19057
19058 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19059 }
19060
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019061#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019062 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019063 {
19064 float_T v1 = get_tv_float(tv1);
19065 float_T v2 = get_tv_float(tv2);
19066
19067 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19068 }
19069#endif
19070
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019071 /* tv2string() puts quotes around a string and allocates memory. Don't do
19072 * that for string variables. Use a single quote when comparing with a
19073 * non-string to do what the docs promise. */
19074 if (tv1->v_type == VAR_STRING)
19075 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019076 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019077 p1 = (char_u *)"'";
19078 else
19079 p1 = tv1->vval.v_string;
19080 }
19081 else
19082 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19083 if (tv2->v_type == VAR_STRING)
19084 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019085 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019086 p2 = (char_u *)"'";
19087 else
19088 p2 = tv2->vval.v_string;
19089 }
19090 else
19091 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019092 if (p1 == NULL)
19093 p1 = (char_u *)"";
19094 if (p2 == NULL)
19095 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019097 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019098 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019099 res = STRICMP(p1, p2);
19100 else
19101 res = STRCMP(p1, p2);
19102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019104 {
19105 double n1, n2;
19106 n1 = strtod((char *)p1, (char **)&p1);
19107 n2 = strtod((char *)p2, (char **)&p2);
19108 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19109 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019110
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019111 /* When the result would be zero, compare the item indexes. Makes the
19112 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019113 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019114 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019115
Bram Moolenaar0d660222005-01-07 21:51:51 +000019116 vim_free(tofree1);
19117 vim_free(tofree2);
19118 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019119}
19120
19121 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019122#ifdef __BORLANDC__
19123_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019125item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019127 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019128 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019129 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019130 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019131 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019132 char_u *func_name;
19133 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019135 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019137 return 0;
19138
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019139 si1 = (sortItem_T *)s1;
19140 si2 = (sortItem_T *)s2;
19141
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019142 if (partial == NULL)
19143 func_name = sortinfo->item_compare_func;
19144 else
19145 func_name = partial->pt_name;
19146
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019147 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019148 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019149 copy_tv(&si1->item->li_tv, &argv[0]);
19150 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151
19152 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019153 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019154 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019155 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156 clear_tv(&argv[0]);
19157 clear_tv(&argv[1]);
19158
19159 if (res == FAIL)
19160 res = ITEM_COMPARE_FAIL;
19161 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019162 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19163 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019164 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019166
19167 /* When the result would be zero, compare the pointers themselves. Makes
19168 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019170 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019171
Bram Moolenaar0d660222005-01-07 21:51:51 +000019172 return res;
19173}
19174
19175/*
19176 * "sort({list})" function
19177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019179do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019180{
Bram Moolenaar33570922005-01-25 22:26:29 +000019181 list_T *l;
19182 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019183 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019184 sortinfo_T *old_sortinfo;
19185 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019186 long len;
19187 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188
Bram Moolenaar0b962472016-02-22 22:51:33 +010019189 /* Pointer to current info struct used in compare function. Save and
19190 * restore the current one for nested calls. */
19191 old_sortinfo = sortinfo;
19192 sortinfo = &info;
19193
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019195 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 else
19197 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019198 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019199 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019200 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19201 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019202 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019203 rettv->vval.v_list = l;
19204 rettv->v_type = VAR_LIST;
19205 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206
Bram Moolenaar0d660222005-01-07 21:51:51 +000019207 len = list_len(l);
19208 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019209 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019210
Bram Moolenaar0b962472016-02-22 22:51:33 +010019211 info.item_compare_ic = FALSE;
19212 info.item_compare_numeric = FALSE;
19213 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019214#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019215 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019216#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019217 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019218 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019220 if (argvars[1].v_type != VAR_UNKNOWN)
19221 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019222 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019223 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019224 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019225 else if (argvars[1].v_type == VAR_PARTIAL)
19226 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019227 else
19228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019229 int error = FALSE;
19230
19231 i = get_tv_number_chk(&argvars[1], &error);
19232 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019233 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019235 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010019236 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019237 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010019238 else if (i != 0)
19239 {
19240 EMSG(_(e_invarg));
19241 goto theend;
19242 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019243 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019244 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010019245 if (*info.item_compare_func == NUL)
19246 {
19247 /* empty string means default sort */
19248 info.item_compare_func = NULL;
19249 }
19250 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019251 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019252 info.item_compare_func = NULL;
19253 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019254 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019255 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019256 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019257 info.item_compare_func = NULL;
19258 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019259 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019260#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019261 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019262 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019263 info.item_compare_func = NULL;
19264 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019265 }
19266#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019267 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019268 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019269 info.item_compare_func = NULL;
19270 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019271 }
19272 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019273 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019274
19275 if (argvars[2].v_type != VAR_UNKNOWN)
19276 {
19277 /* optional third argument: {dict} */
19278 if (argvars[2].v_type != VAR_DICT)
19279 {
19280 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019281 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019282 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019283 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286
Bram Moolenaar0d660222005-01-07 21:51:51 +000019287 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019288 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019289 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019290 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291
Bram Moolenaar327aa022014-03-25 18:24:23 +010019292 i = 0;
19293 if (sort)
19294 {
19295 /* sort(): ptrs will be the list to sort */
19296 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019297 {
19298 ptrs[i].item = li;
19299 ptrs[i].idx = i;
19300 ++i;
19301 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019302
Bram Moolenaar0b962472016-02-22 22:51:33 +010019303 info.item_compare_func_err = FALSE;
19304 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019305 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019306 if ((info.item_compare_func != NULL
19307 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019308 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019309 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019310 EMSG(_("E702: Sort compare function failed"));
19311 else
19312 {
19313 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019314 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019315 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019316 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010019317 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019318
Bram Moolenaar0b962472016-02-22 22:51:33 +010019319 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019320 {
19321 /* Clear the List and append the items in sorted order. */
19322 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19323 l->lv_len = 0;
19324 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019325 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019326 }
19327 }
19328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019330 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019331 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019332
19333 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019334 info.item_compare_func_err = FALSE;
19335 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010019336 item_compare_func_ptr = info.item_compare_func != NULL
19337 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019338 ? item_compare2 : item_compare;
19339
19340 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19341 li = li->li_next)
19342 {
19343 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19344 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019345 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019346 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019347 {
19348 EMSG(_("E882: Uniq compare function failed"));
19349 break;
19350 }
19351 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019352
Bram Moolenaar0b962472016-02-22 22:51:33 +010019353 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019354 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019355 while (--i >= 0)
19356 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019357 li = ptrs[i].item->li_next;
19358 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019359 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019360 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019361 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019362 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019363 list_fix_watch(l, li);
19364 listitem_free(li);
19365 l->lv_len--;
19366 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019367 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019368 }
19369
19370 vim_free(ptrs);
19371 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019372theend:
19373 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374}
19375
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019376/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019377 * "sort({list})" function
19378 */
19379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019380f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019381{
19382 do_sort_uniq(argvars, rettv, TRUE);
19383}
19384
19385/*
19386 * "uniq({list})" function
19387 */
19388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019389f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019390{
19391 do_sort_uniq(argvars, rettv, FALSE);
19392}
19393
19394/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019395 * "soundfold({word})" function
19396 */
19397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019398f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019399{
19400 char_u *s;
19401
19402 rettv->v_type = VAR_STRING;
19403 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019404#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019405 rettv->vval.v_string = eval_soundfold(s);
19406#else
19407 rettv->vval.v_string = vim_strsave(s);
19408#endif
19409}
19410
19411/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019412 * "spellbadword()" function
19413 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019415f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019416{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019417 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019418 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019419 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019420
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019421 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019422 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019423
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019424#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019425 if (argvars[0].v_type == VAR_UNKNOWN)
19426 {
19427 /* Find the start and length of the badly spelled word. */
19428 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19429 if (len != 0)
19430 word = ml_get_cursor();
19431 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019432 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019433 {
19434 char_u *str = get_tv_string_chk(&argvars[0]);
19435 int capcol = -1;
19436
19437 if (str != NULL)
19438 {
19439 /* Check the argument for spelling. */
19440 while (*str != NUL)
19441 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019442 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019443 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019444 {
19445 word = str;
19446 break;
19447 }
19448 str += len;
19449 }
19450 }
19451 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019452#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019453
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019454 list_append_string(rettv->vval.v_list, word, len);
19455 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019456 attr == HLF_SPB ? "bad" :
19457 attr == HLF_SPR ? "rare" :
19458 attr == HLF_SPL ? "local" :
19459 attr == HLF_SPC ? "caps" :
19460 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019461}
19462
19463/*
19464 * "spellsuggest()" function
19465 */
19466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019467f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019468{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019469#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019470 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019471 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019472 int maxcount;
19473 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019474 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019475 listitem_T *li;
19476 int need_capital = FALSE;
19477#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019478
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019479 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019480 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019481
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019482#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019483 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019484 {
19485 str = get_tv_string(&argvars[0]);
19486 if (argvars[1].v_type != VAR_UNKNOWN)
19487 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019488 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019489 if (maxcount <= 0)
19490 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019491 if (argvars[2].v_type != VAR_UNKNOWN)
19492 {
19493 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19494 if (typeerr)
19495 return;
19496 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019497 }
19498 else
19499 maxcount = 25;
19500
Bram Moolenaar4770d092006-01-12 23:22:24 +000019501 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019502
19503 for (i = 0; i < ga.ga_len; ++i)
19504 {
19505 str = ((char_u **)ga.ga_data)[i];
19506
19507 li = listitem_alloc();
19508 if (li == NULL)
19509 vim_free(str);
19510 else
19511 {
19512 li->li_tv.v_type = VAR_STRING;
19513 li->li_tv.v_lock = 0;
19514 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019515 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019516 }
19517 }
19518 ga_clear(&ga);
19519 }
19520#endif
19521}
19522
Bram Moolenaar0d660222005-01-07 21:51:51 +000019523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019524f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019525{
19526 char_u *str;
19527 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019528 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019529 regmatch_T regmatch;
19530 char_u patbuf[NUMBUFLEN];
19531 char_u *save_cpo;
19532 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019533 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019534 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019535 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019536
19537 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19538 save_cpo = p_cpo;
19539 p_cpo = (char_u *)"";
19540
19541 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019542 if (argvars[1].v_type != VAR_UNKNOWN)
19543 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019544 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19545 if (pat == NULL)
19546 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019547 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019548 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019549 }
19550 if (pat == NULL || *pat == NUL)
19551 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019552
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019553 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019555 if (typeerr)
19556 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557
Bram Moolenaar0d660222005-01-07 21:51:51 +000019558 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19559 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019561 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019562 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019563 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019564 if (*str == NUL)
19565 match = FALSE; /* empty item at the end */
19566 else
19567 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019568 if (match)
19569 end = regmatch.startp[0];
19570 else
19571 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019572 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19573 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019574 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019575 if (list_append_string(rettv->vval.v_list, str,
19576 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019577 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019578 }
19579 if (!match)
19580 break;
19581 /* Advance to just after the match. */
19582 if (regmatch.endp[0] > str)
19583 col = 0;
19584 else
19585 {
19586 /* Don't get stuck at the same match. */
19587#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019588 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019589#else
19590 col = 1;
19591#endif
19592 }
19593 str = regmatch.endp[0];
19594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595
Bram Moolenaar473de612013-06-08 18:19:48 +020019596 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019598
Bram Moolenaar0d660222005-01-07 21:51:51 +000019599 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600}
19601
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019602#ifdef FEAT_FLOAT
19603/*
19604 * "sqrt()" function
19605 */
19606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019607f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019608{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019609 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019610
19611 rettv->v_type = VAR_FLOAT;
19612 if (get_float_arg(argvars, &f) == OK)
19613 rettv->vval.v_float = sqrt(f);
19614 else
19615 rettv->vval.v_float = 0.0;
19616}
19617
19618/*
19619 * "str2float()" function
19620 */
19621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019622f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019623{
19624 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19625
19626 if (*p == '+')
19627 p = skipwhite(p + 1);
19628 (void)string2float(p, &rettv->vval.v_float);
19629 rettv->v_type = VAR_FLOAT;
19630}
19631#endif
19632
Bram Moolenaar2c932302006-03-18 21:42:09 +000019633/*
19634 * "str2nr()" function
19635 */
19636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019637f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019638{
19639 int base = 10;
19640 char_u *p;
19641 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019642 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019643
19644 if (argvars[1].v_type != VAR_UNKNOWN)
19645 {
19646 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019647 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019648 {
19649 EMSG(_(e_invarg));
19650 return;
19651 }
19652 }
19653
19654 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019655 if (*p == '+')
19656 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019657 switch (base)
19658 {
19659 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19660 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19661 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19662 default: what = 0;
19663 }
19664 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019665 rettv->vval.v_number = n;
19666}
19667
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668#ifdef HAVE_STRFTIME
19669/*
19670 * "strftime({format}[, {time}])" function
19671 */
19672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019673f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674{
19675 char_u result_buf[256];
19676 struct tm *curtime;
19677 time_t seconds;
19678 char_u *p;
19679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019680 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019682 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019683 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019684 seconds = time(NULL);
19685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019686 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019687 curtime = localtime(&seconds);
19688 /* MSVC returns NULL for an invalid value of seconds. */
19689 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019690 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 else
19692 {
19693# ifdef FEAT_MBYTE
19694 vimconv_T conv;
19695 char_u *enc;
19696
19697 conv.vc_type = CONV_NONE;
19698 enc = enc_locale();
19699 convert_setup(&conv, p_enc, enc);
19700 if (conv.vc_type != CONV_NONE)
19701 p = string_convert(&conv, p, NULL);
19702# endif
19703 if (p != NULL)
19704 (void)strftime((char *)result_buf, sizeof(result_buf),
19705 (char *)p, curtime);
19706 else
19707 result_buf[0] = NUL;
19708
19709# ifdef FEAT_MBYTE
19710 if (conv.vc_type != CONV_NONE)
19711 vim_free(p);
19712 convert_setup(&conv, enc, p_enc);
19713 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019714 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715 else
19716# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019717 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718
19719# ifdef FEAT_MBYTE
19720 /* Release conversion descriptors */
19721 convert_setup(&conv, NULL, NULL);
19722 vim_free(enc);
19723# endif
19724 }
19725}
19726#endif
19727
19728/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019729 * "strgetchar()" function
19730 */
19731 static void
19732f_strgetchar(typval_T *argvars, typval_T *rettv)
19733{
19734 char_u *str;
19735 int len;
19736 int error = FALSE;
19737 int charidx;
19738
19739 rettv->vval.v_number = -1;
19740 str = get_tv_string_chk(&argvars[0]);
19741 if (str == NULL)
19742 return;
19743 len = (int)STRLEN(str);
19744 charidx = get_tv_number_chk(&argvars[1], &error);
19745 if (error)
19746 return;
19747#ifdef FEAT_MBYTE
19748 {
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019749 int byteidx = 0;
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019750
19751 while (charidx >= 0 && byteidx < len)
19752 {
19753 if (charidx == 0)
19754 {
19755 rettv->vval.v_number = mb_ptr2char(str + byteidx);
19756 break;
19757 }
19758 --charidx;
Bram Moolenaar5d18e0e2016-04-14 22:54:24 +020019759 byteidx += mb_cptr2len(str + byteidx);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019760 }
19761 }
19762#else
19763 if (charidx < len)
19764 rettv->vval.v_number = str[charidx];
19765#endif
19766}
19767
19768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019769 * "stridx()" function
19770 */
19771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019772f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773{
19774 char_u buf[NUMBUFLEN];
19775 char_u *needle;
19776 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019777 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019779 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019781 needle = get_tv_string_chk(&argvars[1]);
19782 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019783 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019784 if (needle == NULL || haystack == NULL)
19785 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786
Bram Moolenaar33570922005-01-25 22:26:29 +000019787 if (argvars[2].v_type != VAR_UNKNOWN)
19788 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019789 int error = FALSE;
19790
19791 start_idx = get_tv_number_chk(&argvars[2], &error);
19792 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019793 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019794 if (start_idx >= 0)
19795 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019796 }
19797
19798 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19799 if (pos != NULL)
19800 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019801}
19802
19803/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019804 * "string()" function
19805 */
19806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019807f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019808{
19809 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019810 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019813 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19814 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019815 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019816 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818}
19819
19820/*
19821 * "strlen()" function
19822 */
19823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019824f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019826 rettv->vval.v_number = (varnumber_T)(STRLEN(
19827 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019828}
19829
19830/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019831 * "strchars()" function
19832 */
19833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019834f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019835{
19836 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019837 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019838#ifdef FEAT_MBYTE
19839 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019840 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019841#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019842
19843 if (argvars[1].v_type != VAR_UNKNOWN)
19844 skipcc = get_tv_number_chk(&argvars[1], NULL);
19845 if (skipcc < 0 || skipcc > 1)
19846 EMSG(_(e_invarg));
19847 else
19848 {
19849#ifdef FEAT_MBYTE
19850 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19851 while (*s != NUL)
19852 {
19853 func_mb_ptr2char_adv(&s);
19854 ++len;
19855 }
19856 rettv->vval.v_number = len;
19857#else
19858 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19859#endif
19860 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019861}
19862
19863/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019864 * "strdisplaywidth()" function
19865 */
19866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019867f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019868{
19869 char_u *s = get_tv_string(&argvars[0]);
19870 int col = 0;
19871
19872 if (argvars[1].v_type != VAR_UNKNOWN)
19873 col = get_tv_number(&argvars[1]);
19874
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019875 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019876}
19877
19878/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019879 * "strwidth()" function
19880 */
19881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019882f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019883{
19884 char_u *s = get_tv_string(&argvars[0]);
19885
19886 rettv->vval.v_number = (varnumber_T)(
19887#ifdef FEAT_MBYTE
19888 mb_string2cells(s, -1)
19889#else
19890 STRLEN(s)
19891#endif
19892 );
19893}
19894
19895/*
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019896 * "strcharpart()" function
19897 */
19898 static void
19899f_strcharpart(typval_T *argvars, typval_T *rettv)
19900{
19901#ifdef FEAT_MBYTE
19902 char_u *p;
19903 int nchar;
19904 int nbyte = 0;
19905 int charlen;
19906 int len = 0;
19907 int slen;
19908 int error = FALSE;
19909
19910 p = get_tv_string(&argvars[0]);
19911 slen = (int)STRLEN(p);
19912
19913 nchar = get_tv_number_chk(&argvars[1], &error);
19914 if (!error)
19915 {
19916 if (nchar > 0)
19917 while (nchar > 0 && nbyte < slen)
19918 {
Bram Moolenaarfca66002016-04-23 15:30:09 +020019919 nbyte += mb_cptr2len(p + nbyte);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019920 --nchar;
19921 }
19922 else
19923 nbyte = nchar;
19924 if (argvars[2].v_type != VAR_UNKNOWN)
19925 {
19926 charlen = get_tv_number(&argvars[2]);
19927 while (charlen > 0 && nbyte + len < slen)
19928 {
Bram Moolenaar73dfe912016-04-23 13:54:48 +020019929 int off = nbyte + len;
19930
19931 if (off < 0)
19932 len += 1;
19933 else
Bram Moolenaarfca66002016-04-23 15:30:09 +020019934 len += mb_cptr2len(p + off);
Bram Moolenaar58de0e22016-04-14 15:13:46 +020019935 --charlen;
19936 }
19937 }
19938 else
19939 len = slen - nbyte; /* default: all bytes that are available. */
19940 }
19941
19942 /*
19943 * Only return the overlap between the specified part and the actual
19944 * string.
19945 */
19946 if (nbyte < 0)
19947 {
19948 len += nbyte;
19949 nbyte = 0;
19950 }
19951 else if (nbyte > slen)
19952 nbyte = slen;
19953 if (len < 0)
19954 len = 0;
19955 else if (nbyte + len > slen)
19956 len = slen - nbyte;
19957
19958 rettv->v_type = VAR_STRING;
19959 rettv->vval.v_string = vim_strnsave(p + nbyte, len);
19960#else
19961 f_strpart(argvars, rettv);
19962#endif
19963}
19964
19965/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966 * "strpart()" function
19967 */
19968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019969f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970{
19971 char_u *p;
19972 int n;
19973 int len;
19974 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019975 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019977 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 slen = (int)STRLEN(p);
19979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019980 n = get_tv_number_chk(&argvars[1], &error);
19981 if (error)
19982 len = 0;
19983 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019985 else
19986 len = slen - n; /* default len: all bytes that are available. */
19987
19988 /*
19989 * Only return the overlap between the specified part and the actual
19990 * string.
19991 */
19992 if (n < 0)
19993 {
19994 len += n;
19995 n = 0;
19996 }
19997 else if (n > slen)
19998 n = slen;
19999 if (len < 0)
20000 len = 0;
20001 else if (n + len > slen)
20002 len = slen - n;
20003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 rettv->v_type = VAR_STRING;
20005 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006}
20007
20008/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020009 * "strridx()" function
20010 */
20011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020012f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020013{
20014 char_u buf[NUMBUFLEN];
20015 char_u *needle;
20016 char_u *haystack;
20017 char_u *rest;
20018 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020019 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000020020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020021 needle = get_tv_string_chk(&argvars[1]);
20022 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020023
20024 rettv->vval.v_number = -1;
20025 if (needle == NULL || haystack == NULL)
20026 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020027
20028 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020029 if (argvars[2].v_type != VAR_UNKNOWN)
20030 {
20031 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020032 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000020033 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020034 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020035 }
20036 else
20037 end_idx = haystack_len;
20038
Bram Moolenaar0d660222005-01-07 21:51:51 +000020039 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000020040 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020041 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000020042 lastmatch = haystack + end_idx;
20043 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020044 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000020045 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000020046 for (rest = haystack; *rest != '\0'; ++rest)
20047 {
20048 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000020049 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020050 break;
20051 lastmatch = rest;
20052 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000020053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020054
20055 if (lastmatch == NULL)
20056 rettv->vval.v_number = -1;
20057 else
20058 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
20059}
20060
20061/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020062 * "strtrans()" function
20063 */
20064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020065f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020066{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020067 rettv->v_type = VAR_STRING;
20068 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069}
20070
20071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000020072 * "submatch()" function
20073 */
20074 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020075f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020076{
Bram Moolenaar41571762014-04-02 19:00:58 +020020077 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020020078 int no;
20079 int retList = 0;
20080
20081 no = (int)get_tv_number_chk(&argvars[0], &error);
20082 if (error)
20083 return;
20084 error = FALSE;
20085 if (argvars[1].v_type != VAR_UNKNOWN)
20086 retList = get_tv_number_chk(&argvars[1], &error);
20087 if (error)
20088 return;
20089
20090 if (retList == 0)
20091 {
20092 rettv->v_type = VAR_STRING;
20093 rettv->vval.v_string = reg_submatch(no);
20094 }
20095 else
20096 {
20097 rettv->v_type = VAR_LIST;
20098 rettv->vval.v_list = reg_submatch_list(no);
20099 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000020100}
20101
20102/*
20103 * "substitute()" function
20104 */
20105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020106f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000020107{
20108 char_u patbuf[NUMBUFLEN];
20109 char_u subbuf[NUMBUFLEN];
20110 char_u flagsbuf[NUMBUFLEN];
20111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020112 char_u *str = get_tv_string_chk(&argvars[0]);
20113 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
20114 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
20115 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
20116
Bram Moolenaar0d660222005-01-07 21:51:51 +000020117 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020118 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
20119 rettv->vval.v_string = NULL;
20120 else
20121 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000020122}
20123
20124/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020125 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020128f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129{
20130 int id = 0;
20131#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020132 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020133 long col;
20134 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020135 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020136
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020137 lnum = get_tv_lnum(argvars); /* -1 on type error */
20138 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20139 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020141 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000020142 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020143 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144#endif
20145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147}
20148
20149/*
20150 * "synIDattr(id, what [, mode])" function
20151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020153f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154{
20155 char_u *p = NULL;
20156#ifdef FEAT_SYN_HL
20157 int id;
20158 char_u *what;
20159 char_u *mode;
20160 char_u modebuf[NUMBUFLEN];
20161 int modec;
20162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020163 id = get_tv_number(&argvars[0]);
20164 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020165 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020166 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020167 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020169 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 modec = 0; /* replace invalid with current */
20171 }
20172 else
20173 {
Bram Moolenaar61be73b2016-04-29 22:59:22 +020020174#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
Bram Moolenaarda5b3dc2016-04-23 15:19:02 +020020175 if (USE_24BIT)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020176 modec = 'g';
20177 else
20178#endif
20179 if (t_colors > 1)
20180 modec = 'c';
20181 else
20182 modec = 't';
20183 }
20184
20185
20186 switch (TOLOWER_ASC(what[0]))
20187 {
20188 case 'b':
20189 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20190 p = highlight_color(id, what, modec);
20191 else /* bold */
20192 p = highlight_has_attr(id, HL_BOLD, modec);
20193 break;
20194
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020195 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 p = highlight_color(id, what, modec);
20197 break;
20198
20199 case 'i':
20200 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20201 p = highlight_has_attr(id, HL_INVERSE, modec);
20202 else /* italic */
20203 p = highlight_has_attr(id, HL_ITALIC, modec);
20204 break;
20205
20206 case 'n': /* name */
20207 p = get_highlight_name(NULL, id - 1);
20208 break;
20209
20210 case 'r': /* reverse */
20211 p = highlight_has_attr(id, HL_INVERSE, modec);
20212 break;
20213
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020214 case 's':
20215 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20216 p = highlight_color(id, what, modec);
20217 else /* standout */
20218 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020219 break;
20220
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020221 case 'u':
20222 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20223 /* underline */
20224 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20225 else
20226 /* undercurl */
20227 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 break;
20229 }
20230
20231 if (p != NULL)
20232 p = vim_strsave(p);
20233#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020234 rettv->v_type = VAR_STRING;
20235 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236}
20237
20238/*
20239 * "synIDtrans(id)" function
20240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020242f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243{
20244 int id;
20245
20246#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020247 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248
20249 if (id > 0)
20250 id = syn_get_final_id(id);
20251 else
20252#endif
20253 id = 0;
20254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020255 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256}
20257
20258/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020259 * "synconcealed(lnum, col)" function
20260 */
20261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020262f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020263{
20264#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20265 long lnum;
20266 long col;
20267 int syntax_flags = 0;
20268 int cchar;
20269 int matchid = 0;
20270 char_u str[NUMBUFLEN];
20271#endif
20272
20273 rettv->v_type = VAR_LIST;
20274 rettv->vval.v_list = NULL;
20275
20276#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20277 lnum = get_tv_lnum(argvars); /* -1 on type error */
20278 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20279
20280 vim_memset(str, NUL, sizeof(str));
20281
20282 if (rettv_list_alloc(rettv) != FAIL)
20283 {
20284 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20285 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20286 && curwin->w_p_cole > 0)
20287 {
20288 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20289 syntax_flags = get_syntax_info(&matchid);
20290
20291 /* get the conceal character */
20292 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20293 {
20294 cchar = syn_get_sub_char();
20295 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20296 cchar = lcs_conceal;
20297 if (cchar != NUL)
20298 {
20299# ifdef FEAT_MBYTE
20300 if (has_mbyte)
20301 (*mb_char2bytes)(cchar, str);
20302 else
20303# endif
20304 str[0] = cchar;
20305 }
20306 }
20307 }
20308
20309 list_append_number(rettv->vval.v_list,
20310 (syntax_flags & HL_CONCEAL) != 0);
20311 /* -1 to auto-determine strlen */
20312 list_append_string(rettv->vval.v_list, str, -1);
20313 list_append_number(rettv->vval.v_list, matchid);
20314 }
20315#endif
20316}
20317
20318/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020319 * "synstack(lnum, col)" function
20320 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020322f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020323{
20324#ifdef FEAT_SYN_HL
20325 long lnum;
20326 long col;
20327 int i;
20328 int id;
20329#endif
20330
20331 rettv->v_type = VAR_LIST;
20332 rettv->vval.v_list = NULL;
20333
20334#ifdef FEAT_SYN_HL
20335 lnum = get_tv_lnum(argvars); /* -1 on type error */
20336 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20337
20338 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020339 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020340 && rettv_list_alloc(rettv) != FAIL)
20341 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020342 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020343 for (i = 0; ; ++i)
20344 {
20345 id = syn_get_stack_item(i);
20346 if (id < 0)
20347 break;
20348 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20349 break;
20350 }
20351 }
20352#endif
20353}
20354
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356get_cmd_output_as_rettv(
20357 typval_T *argvars,
20358 typval_T *rettv,
20359 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020361 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020362 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020363 char_u *infile = NULL;
20364 char_u buf[NUMBUFLEN];
20365 int err = FALSE;
20366 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020367 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020368 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020369
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020370 rettv->v_type = VAR_STRING;
20371 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020372 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020373 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020374
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020375 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020376 {
20377 /*
20378 * Write the string to a temp file, to be used for input of the shell
20379 * command.
20380 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020381 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020382 {
20383 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020384 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020385 }
20386
20387 fd = mch_fopen((char *)infile, WRITEBIN);
20388 if (fd == NULL)
20389 {
20390 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020391 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020392 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020393 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020394 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020395 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20396 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020397 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020398 else
20399 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020400 size_t len;
20401
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020402 p = get_tv_string_buf_chk(&argvars[1], buf);
20403 if (p == NULL)
20404 {
20405 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020406 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020407 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020408 len = STRLEN(p);
20409 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020410 err = TRUE;
20411 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020412 if (fclose(fd) != 0)
20413 err = TRUE;
20414 if (err)
20415 {
20416 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020417 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020418 }
20419 }
20420
Bram Moolenaar52a72462014-08-29 15:53:52 +020020421 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20422 * echoes typeahead, that messes up the display. */
20423 if (!msg_silent)
20424 flags += SHELL_COOKED;
20425
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020426 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020427 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020428 int len;
20429 listitem_T *li;
20430 char_u *s = NULL;
20431 char_u *start;
20432 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020433 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434
Bram Moolenaar52a72462014-08-29 15:53:52 +020020435 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020436 if (res == NULL)
20437 goto errret;
20438
20439 list = list_alloc();
20440 if (list == NULL)
20441 goto errret;
20442
20443 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020445 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020446 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020447 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020448 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020449
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020450 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020451 if (s == NULL)
20452 goto errret;
20453
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020454 for (p = s; start < end; ++p, ++start)
20455 *p = *start == NUL ? NL : *start;
20456 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020457
20458 li = listitem_alloc();
20459 if (li == NULL)
20460 {
20461 vim_free(s);
20462 goto errret;
20463 }
20464 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020465 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020466 li->li_tv.vval.v_string = s;
20467 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020469
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020470 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020471 rettv->v_type = VAR_LIST;
20472 rettv->vval.v_list = list;
20473 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020474 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020475 else
20476 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020477 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020478#ifdef USE_CR
20479 /* translate <CR> into <NL> */
20480 if (res != NULL)
20481 {
20482 char_u *s;
20483
20484 for (s = res; *s; ++s)
20485 {
20486 if (*s == CAR)
20487 *s = NL;
20488 }
20489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020490#else
20491# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020492 /* translate <CR><NL> into <NL> */
20493 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020494 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020495 char_u *s, *d;
20496
20497 d = res;
20498 for (s = res; *s; ++s)
20499 {
20500 if (s[0] == CAR && s[1] == NL)
20501 ++s;
20502 *d++ = *s;
20503 }
20504 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506# endif
20507#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020508 rettv->vval.v_string = res;
20509 res = NULL;
20510 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020511
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020512errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020513 if (infile != NULL)
20514 {
20515 mch_remove(infile);
20516 vim_free(infile);
20517 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020518 if (res != NULL)
20519 vim_free(res);
20520 if (list != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +020020521 list_free(list);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020522}
20523
20524/*
20525 * "system()" function
20526 */
20527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020528f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020529{
20530 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20531}
20532
20533/*
20534 * "systemlist()" function
20535 */
20536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020538{
20539 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540}
20541
20542/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020543 * "tabpagebuflist()" function
20544 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020546f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020547{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020548#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020549 tabpage_T *tp;
20550 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020551
20552 if (argvars[0].v_type == VAR_UNKNOWN)
20553 wp = firstwin;
20554 else
20555 {
20556 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20557 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020558 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020559 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020560 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020561 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020562 for (; wp != NULL; wp = wp->w_next)
20563 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020564 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020565 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020566 }
20567#endif
20568}
20569
20570
20571/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020572 * "tabpagenr()" function
20573 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020575f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020576{
20577 int nr = 1;
20578#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020579 char_u *arg;
20580
20581 if (argvars[0].v_type != VAR_UNKNOWN)
20582 {
20583 arg = get_tv_string_chk(&argvars[0]);
20584 nr = 0;
20585 if (arg != NULL)
20586 {
20587 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020588 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020589 else
20590 EMSG2(_(e_invexpr2), arg);
20591 }
20592 }
20593 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020594 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020595#endif
20596 rettv->vval.v_number = nr;
20597}
20598
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020599
20600#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020601static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020602
20603/*
20604 * Common code for tabpagewinnr() and winnr().
20605 */
20606 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020607get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020608{
20609 win_T *twin;
20610 int nr = 1;
20611 win_T *wp;
20612 char_u *arg;
20613
20614 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20615 if (argvar->v_type != VAR_UNKNOWN)
20616 {
20617 arg = get_tv_string_chk(argvar);
20618 if (arg == NULL)
20619 nr = 0; /* type error; errmsg already given */
20620 else if (STRCMP(arg, "$") == 0)
20621 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20622 else if (STRCMP(arg, "#") == 0)
20623 {
20624 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20625 if (twin == NULL)
20626 nr = 0;
20627 }
20628 else
20629 {
20630 EMSG2(_(e_invexpr2), arg);
20631 nr = 0;
20632 }
20633 }
20634
20635 if (nr > 0)
20636 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20637 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020638 {
20639 if (wp == NULL)
20640 {
20641 /* didn't find it in this tabpage */
20642 nr = 0;
20643 break;
20644 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020645 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020646 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020647 return nr;
20648}
20649#endif
20650
20651/*
20652 * "tabpagewinnr()" function
20653 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020655f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020656{
20657 int nr = 1;
20658#ifdef FEAT_WINDOWS
20659 tabpage_T *tp;
20660
20661 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20662 if (tp == NULL)
20663 nr = 0;
20664 else
20665 nr = get_winnr(tp, &argvars[1]);
20666#endif
20667 rettv->vval.v_number = nr;
20668}
20669
20670
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020671/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020672 * "tagfiles()" function
20673 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020675f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020676{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020677 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020678 tagname_T tn;
20679 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020681 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020682 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020683 fname = alloc(MAXPATHL);
20684 if (fname == NULL)
20685 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020686
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020687 for (first = TRUE; ; first = FALSE)
20688 if (get_tagfname(&tn, first, fname) == FAIL
20689 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020690 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020691 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020692 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020693}
20694
20695/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020696 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020697 */
20698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020699f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020700{
20701 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020702
20703 tag_pattern = get_tv_string(&argvars[0]);
20704
20705 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020706 if (*tag_pattern == NUL)
20707 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020708
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020709 if (rettv_list_alloc(rettv) == OK)
20710 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020711}
20712
20713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020714 * "tempname()" function
20715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020717f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718{
20719 static int x = 'A';
20720
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020721 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020722 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020723
20724 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20725 * names. Skip 'I' and 'O', they are used for shell redirection. */
20726 do
20727 {
20728 if (x == 'Z')
20729 x = '0';
20730 else if (x == '9')
20731 x = 'A';
20732 else
20733 {
20734#ifdef EBCDIC
20735 if (x == 'I')
20736 x = 'J';
20737 else if (x == 'R')
20738 x = 'S';
20739 else
20740#endif
20741 ++x;
20742 }
20743 } while (x == 'I' || x == 'O');
20744}
20745
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020746#ifdef FEAT_FLOAT
20747/*
20748 * "tan()" function
20749 */
20750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020751f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020752{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020753 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020754
20755 rettv->v_type = VAR_FLOAT;
20756 if (get_float_arg(argvars, &f) == OK)
20757 rettv->vval.v_float = tan(f);
20758 else
20759 rettv->vval.v_float = 0.0;
20760}
20761
20762/*
20763 * "tanh()" function
20764 */
20765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020766f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020767{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020768 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020769
20770 rettv->v_type = VAR_FLOAT;
20771 if (get_float_arg(argvars, &f) == OK)
20772 rettv->vval.v_float = tanh(f);
20773 else
20774 rettv->vval.v_float = 0.0;
20775}
20776#endif
20777
Bram Moolenaar574860b2016-05-24 17:33:34 +020020778/*
Bram Moolenaar8e8df252016-05-25 21:23:21 +020020779 * "test_alloc_fail(id, countdown, repeat)" function
20780 */
20781 static void
20782f_test_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
20783{
20784 if (argvars[0].v_type != VAR_NUMBER
20785 || argvars[0].vval.v_number <= 0
20786 || argvars[1].v_type != VAR_NUMBER
20787 || argvars[1].vval.v_number < 0
20788 || argvars[2].v_type != VAR_NUMBER)
20789 EMSG(_(e_invarg));
20790 else
20791 {
20792 alloc_fail_id = argvars[0].vval.v_number;
20793 if (alloc_fail_id >= aid_last)
20794 EMSG(_(e_invarg));
20795 alloc_fail_countdown = argvars[1].vval.v_number;
20796 alloc_fail_repeat = argvars[2].vval.v_number;
20797 did_outofmem_msg = FALSE;
20798 }
20799}
20800
20801/*
20802 * "test_disable_char_avail({expr})" function
20803 */
20804 static void
20805f_test_disable_char_avail(typval_T *argvars, typval_T *rettv UNUSED)
20806{
20807 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
20808}
20809
20810/*
Bram Moolenaar574860b2016-05-24 17:33:34 +020020811 * "test_garbagecollect_now()" function
20812 */
20813 static void
20814f_test_garbagecollect_now(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20815{
20816 /* This is dangerous, any Lists and Dicts used internally may be freed
20817 * while still in use. */
20818 garbage_collect(TRUE);
20819}
20820
20821#ifdef FEAT_JOB_CHANNEL
20822 static void
20823f_test_null_channel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20824{
20825 rettv->v_type = VAR_CHANNEL;
20826 rettv->vval.v_channel = NULL;
20827}
20828#endif
20829
20830 static void
20831f_test_null_dict(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20832{
20833 rettv->v_type = VAR_DICT;
20834 rettv->vval.v_dict = NULL;
20835}
20836
20837#ifdef FEAT_JOB_CHANNEL
20838 static void
20839f_test_null_job(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20840{
20841 rettv->v_type = VAR_JOB;
20842 rettv->vval.v_job = NULL;
20843}
20844#endif
20845
20846 static void
20847f_test_null_list(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20848{
20849 rettv->v_type = VAR_LIST;
20850 rettv->vval.v_list = NULL;
20851}
20852
20853 static void
20854f_test_null_partial(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20855{
20856 rettv->v_type = VAR_PARTIAL;
20857 rettv->vval.v_partial = NULL;
20858}
20859
20860 static void
20861f_test_null_string(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
20862{
20863 rettv->v_type = VAR_STRING;
20864 rettv->vval.v_string = NULL;
20865}
20866
Bram Moolenaar975b5272016-03-15 23:10:59 +010020867#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20868/*
20869 * Get a callback from "arg". It can be a Funcref or a function name.
20870 * When "arg" is zero return an empty string.
20871 * Return NULL for an invalid argument.
20872 */
20873 char_u *
20874get_callback(typval_T *arg, partial_T **pp)
20875{
20876 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20877 {
20878 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020879 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020880 return (*pp)->pt_name;
20881 }
20882 *pp = NULL;
20883 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20884 return arg->vval.v_string;
20885 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20886 return (char_u *)"";
20887 EMSG(_("E921: Invalid callback argument"));
20888 return NULL;
20889}
20890#endif
20891
20892#ifdef FEAT_TIMERS
20893/*
20894 * "timer_start(time, callback [, options])" function
20895 */
20896 static void
20897f_timer_start(typval_T *argvars, typval_T *rettv)
20898{
20899 long msec = get_tv_number(&argvars[0]);
20900 timer_T *timer;
20901 int repeat = 0;
20902 char_u *callback;
20903 dict_T *dict;
20904
Bram Moolenaar38499922016-04-22 20:46:52 +020020905 if (check_secure())
20906 return;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020907 if (argvars[2].v_type != VAR_UNKNOWN)
20908 {
20909 if (argvars[2].v_type != VAR_DICT
20910 || (dict = argvars[2].vval.v_dict) == NULL)
20911 {
20912 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20913 return;
20914 }
20915 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20916 repeat = get_dict_number(dict, (char_u *)"repeat");
20917 }
20918
20919 timer = create_timer(msec, repeat);
20920 callback = get_callback(&argvars[1], &timer->tr_partial);
20921 if (callback == NULL)
20922 {
20923 stop_timer(timer);
20924 rettv->vval.v_number = -1;
20925 }
20926 else
20927 {
20928 timer->tr_callback = vim_strsave(callback);
20929 rettv->vval.v_number = timer->tr_id;
20930 }
20931}
20932
20933/*
20934 * "timer_stop(timer)" function
20935 */
20936 static void
20937f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20938{
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020939 timer_T *timer;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020940
Bram Moolenaare40d75f2016-05-15 18:00:19 +020020941 if (argvars[0].v_type != VAR_NUMBER)
20942 {
20943 EMSG(_(e_number_exp));
20944 return;
20945 }
20946 timer = find_timer(get_tv_number(&argvars[0]));
Bram Moolenaar975b5272016-03-15 23:10:59 +010020947 if (timer != NULL)
20948 stop_timer(timer);
20949}
20950#endif
20951
Bram Moolenaard52d9742005-08-21 22:20:28 +000020952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 * "tolower(string)" function
20954 */
20955 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020956f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957{
20958 char_u *p;
20959
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020960 p = vim_strsave(get_tv_string(&argvars[0]));
20961 rettv->v_type = VAR_STRING;
20962 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963
20964 if (p != NULL)
20965 while (*p != NUL)
20966 {
20967#ifdef FEAT_MBYTE
20968 int l;
20969
20970 if (enc_utf8)
20971 {
20972 int c, lc;
20973
20974 c = utf_ptr2char(p);
20975 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020976 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020977 /* TODO: reallocate string when byte count changes. */
20978 if (utf_char2len(lc) == l)
20979 utf_char2bytes(lc, p);
20980 p += l;
20981 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020982 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 p += l; /* skip multi-byte character */
20984 else
20985#endif
20986 {
20987 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20988 ++p;
20989 }
20990 }
20991}
20992
20993/*
20994 * "toupper(string)" function
20995 */
20996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020997f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020999 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021000 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001}
21002
21003/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000021004 * "tr(string, fromstr, tostr)" function
21005 */
21006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021007f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021008{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021009 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021010 char_u *fromstr;
21011 char_u *tostr;
21012 char_u *p;
21013#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000021014 int inlen;
21015 int fromlen;
21016 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021017 int idx;
21018 char_u *cpstr;
21019 int cplen;
21020 int first = TRUE;
21021#endif
21022 char_u buf[NUMBUFLEN];
21023 char_u buf2[NUMBUFLEN];
21024 garray_T ga;
21025
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021026 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021027 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
21028 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021029
21030 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021031 rettv->v_type = VAR_STRING;
21032 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021033 if (fromstr == NULL || tostr == NULL)
21034 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021035 ga_init2(&ga, (int)sizeof(char), 80);
21036
21037#ifdef FEAT_MBYTE
21038 if (!has_mbyte)
21039#endif
21040 /* not multi-byte: fromstr and tostr must be the same length */
21041 if (STRLEN(fromstr) != STRLEN(tostr))
21042 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021043#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000021044error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000021045#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000021046 EMSG2(_(e_invarg2), fromstr);
21047 ga_clear(&ga);
21048 return;
21049 }
21050
21051 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021052 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021053 {
21054#ifdef FEAT_MBYTE
21055 if (has_mbyte)
21056 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021057 inlen = (*mb_ptr2len)(in_str);
21058 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021059 cplen = inlen;
21060 idx = 0;
21061 for (p = fromstr; *p != NUL; p += fromlen)
21062 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021063 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021064 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021065 {
21066 for (p = tostr; *p != NUL; p += tolen)
21067 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021068 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021069 if (idx-- == 0)
21070 {
21071 cplen = tolen;
21072 cpstr = p;
21073 break;
21074 }
21075 }
21076 if (*p == NUL) /* tostr is shorter than fromstr */
21077 goto error;
21078 break;
21079 }
21080 ++idx;
21081 }
21082
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021083 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000021084 {
21085 /* Check that fromstr and tostr have the same number of
21086 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021087 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000021088 first = FALSE;
21089 for (p = tostr; *p != NUL; p += tolen)
21090 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000021091 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021092 --idx;
21093 }
21094 if (idx != 0)
21095 goto error;
21096 }
21097
Bram Moolenaarcde88542015-08-11 19:14:00 +020021098 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000021099 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021100 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021101
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021102 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021103 }
21104 else
21105#endif
21106 {
21107 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021108 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000021109 if (p != NULL)
21110 ga_append(&ga, tostr[p - fromstr]);
21111 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010021112 ga_append(&ga, *in_str);
21113 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021114 }
21115 }
21116
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021117 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020021118 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000021119 ga_append(&ga, NUL);
21120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021121 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000021122}
21123
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021124#ifdef FEAT_FLOAT
21125/*
21126 * "trunc({float})" function
21127 */
21128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021129f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021130{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010021131 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021132
21133 rettv->v_type = VAR_FLOAT;
21134 if (get_float_arg(argvars, &f) == OK)
21135 /* trunc() is not in C90, use floor() or ceil() instead. */
21136 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
21137 else
21138 rettv->vval.v_float = 0.0;
21139}
21140#endif
21141
Bram Moolenaar8299df92004-07-10 09:47:34 +000021142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 * "type(expr)" function
21144 */
21145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021146f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021147{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010021148 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021149
21150 switch (argvars[0].v_type)
21151 {
21152 case VAR_NUMBER: n = 0; break;
21153 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010021154 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021155 case VAR_FUNC: n = 2; break;
21156 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000021157 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021158 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010021159 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010021160 if (argvars[0].vval.v_number == VVAL_FALSE
21161 || argvars[0].vval.v_number == VVAL_TRUE)
21162 n = 6;
21163 else
21164 n = 7;
21165 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021166 case VAR_JOB: n = 8; break;
21167 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010021168 case VAR_UNKNOWN:
21169 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
21170 n = -1;
21171 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000021172 }
21173 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174}
21175
21176/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021177 * "undofile(name)" function
21178 */
21179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021180f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021181{
21182 rettv->v_type = VAR_STRING;
21183#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021184 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021185 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021186
Bram Moolenaarb41d9682012-04-30 17:35:48 +020021187 if (*fname == NUL)
21188 {
21189 /* If there is no file name there will be no undo file. */
21190 rettv->vval.v_string = NULL;
21191 }
21192 else
21193 {
21194 char_u *ffname = FullName_save(fname, FALSE);
21195
21196 if (ffname != NULL)
21197 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
21198 vim_free(ffname);
21199 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020021200 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020021201#else
21202 rettv->vval.v_string = NULL;
21203#endif
21204}
21205
21206/*
Bram Moolenaara800b422010-06-27 01:15:55 +020021207 * "undotree()" function
21208 */
21209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021210f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020021211{
21212 if (rettv_dict_alloc(rettv) == OK)
21213 {
21214 dict_T *dict = rettv->vval.v_dict;
21215 list_T *list;
21216
Bram Moolenaar730cde92010-06-27 05:18:54 +020021217 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021218 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021219 dict_add_nr_str(dict, "save_last",
21220 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021221 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
21222 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020021223 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020021224
21225 list = list_alloc();
21226 if (list != NULL)
21227 {
21228 u_eval_tree(curbuf->b_u_oldhead, list);
21229 dict_add_list(dict, "entries", list);
21230 }
21231 }
21232}
21233
21234/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000021235 * "values(dict)" function
21236 */
21237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021238f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000021239{
21240 dict_list(argvars, rettv, 1);
21241}
21242
21243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 * "virtcol(string)" function
21245 */
21246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021247f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248{
21249 colnr_T vcol = 0;
21250 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021251 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021253 fp = var2fpos(&argvars[0], FALSE, &fnum);
21254 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
21255 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 {
21257 getvvcol(curwin, fp, NULL, NULL, &vcol);
21258 ++vcol;
21259 }
21260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262}
21263
21264/*
21265 * "visualmode()" function
21266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010021268f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269{
Bram Moolenaar071d4272004-06-13 20:20:40 +000021270 char_u str[2];
21271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021272 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 str[0] = curbuf->b_visual_mode_eval;
21274 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021275 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276
21277 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000021278 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280}
21281
21282/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021283 * "wildmenumode()" function
21284 */
21285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021286f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010021287{
21288#ifdef FEAT_WILDMENU
21289 if (wild_menu_showing)
21290 rettv->vval.v_number = 1;
21291#endif
21292}
21293
21294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 * "winbufnr(nr)" function
21296 */
21297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021298f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299{
21300 win_T *wp;
21301
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021302 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021303 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021306 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021307}
21308
21309/*
21310 * "wincol()" function
21311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021313f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314{
21315 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021316 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021317}
21318
21319/*
21320 * "winheight(nr)" function
21321 */
21322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021323f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021324{
21325 win_T *wp;
21326
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021327 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021329 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021331 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332}
21333
21334/*
21335 * "winline()" function
21336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021338f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339{
21340 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021341 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342}
21343
21344/*
21345 * "winnr()" function
21346 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021348f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349{
21350 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021351
Bram Moolenaar071d4272004-06-13 20:20:40 +000021352#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021353 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021354#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021355 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021356}
21357
21358/*
21359 * "winrestcmd()" function
21360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021362f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021363{
21364#ifdef FEAT_WINDOWS
21365 win_T *wp;
21366 int winnr = 1;
21367 garray_T ga;
21368 char_u buf[50];
21369
21370 ga_init2(&ga, (int)sizeof(char), 70);
21371 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21372 {
21373 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21374 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021375 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21376 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021377 ++winnr;
21378 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021379 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021381 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021383 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021385 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021386}
21387
21388/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021389 * "winrestview()" function
21390 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021393{
21394 dict_T *dict;
21395
21396 if (argvars[0].v_type != VAR_DICT
21397 || (dict = argvars[0].vval.v_dict) == NULL)
21398 EMSG(_(e_invarg));
21399 else
21400 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021401 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21402 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21403 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21404 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021405#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021406 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21407 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021408#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021409 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21410 {
21411 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21412 curwin->w_set_curswant = FALSE;
21413 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021414
Bram Moolenaar82c25852014-05-28 16:47:16 +020021415 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21416 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021417#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021418 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21419 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021420#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021421 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21422 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21423 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21424 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021425
21426 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021427 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021428# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020021429 win_new_width(curwin, W_WIDTH(curwin));
21430# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021431 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021432
Bram Moolenaarb851a962014-10-31 15:45:52 +010021433 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021434 curwin->w_topline = 1;
21435 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21436 curwin->w_topline = curbuf->b_ml.ml_line_count;
21437#ifdef FEAT_DIFF
21438 check_topfill(curwin, TRUE);
21439#endif
21440 }
21441}
21442
21443/*
21444 * "winsaveview()" function
21445 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021447f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021448{
21449 dict_T *dict;
21450
Bram Moolenaara800b422010-06-27 01:15:55 +020021451 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021452 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021453 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021454
21455 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21456 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21457#ifdef FEAT_VIRTUALEDIT
21458 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21459#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021460 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021461 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21462
21463 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21464#ifdef FEAT_DIFF
21465 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21466#endif
21467 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21468 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21469}
21470
21471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 * "winwidth(nr)" function
21473 */
21474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021475f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476{
21477 win_T *wp;
21478
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021479 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021481 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010021483#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021484 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021486 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487#endif
21488}
21489
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021491 * "wordcount()" function
21492 */
21493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021494f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021495{
21496 if (rettv_dict_alloc(rettv) == FAIL)
21497 return;
21498 cursor_pos_info(rettv->vval.v_dict);
21499}
21500
21501/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021502 * Write list of strings to file
21503 */
21504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021505write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021506{
21507 listitem_T *li;
21508 int c;
21509 int ret = OK;
21510 char_u *s;
21511
21512 for (li = list->lv_first; li != NULL; li = li->li_next)
21513 {
21514 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21515 {
21516 if (*s == '\n')
21517 c = putc(NUL, fd);
21518 else
21519 c = putc(*s, fd);
21520 if (c == EOF)
21521 {
21522 ret = FAIL;
21523 break;
21524 }
21525 }
21526 if (!binary || li->li_next != NULL)
21527 if (putc('\n', fd) == EOF)
21528 {
21529 ret = FAIL;
21530 break;
21531 }
21532 if (ret == FAIL)
21533 {
21534 EMSG(_(e_write));
21535 break;
21536 }
21537 }
21538 return ret;
21539}
21540
21541/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021542 * "writefile()" function
21543 */
21544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021545f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021546{
21547 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021548 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021549 char_u *fname;
21550 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021551 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021552
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021553 if (check_restricted() || check_secure())
21554 return;
21555
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021556 if (argvars[0].v_type != VAR_LIST)
21557 {
21558 EMSG2(_(e_listarg), "writefile()");
21559 return;
21560 }
21561 if (argvars[0].vval.v_list == NULL)
21562 return;
21563
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021564 if (argvars[2].v_type != VAR_UNKNOWN)
21565 {
21566 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21567 binary = TRUE;
21568 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21569 append = TRUE;
21570 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021571
21572 /* Always open the file in binary mode, library functions have a mind of
21573 * their own about CR-LF conversion. */
21574 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021575 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21576 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021577 {
21578 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21579 ret = -1;
21580 }
21581 else
21582 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021583 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21584 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021585 fclose(fd);
21586 }
21587
21588 rettv->vval.v_number = ret;
21589}
21590
21591/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021592 * "xor(expr, expr)" function
21593 */
21594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021595f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021596{
21597 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21598 ^ get_tv_number_chk(&argvars[1], NULL);
21599}
21600
21601
21602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021604 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 */
21606 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021607var2fpos(
21608 typval_T *varp,
21609 int dollar_lnum, /* TRUE when $ is last line */
21610 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021612 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021614 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615
Bram Moolenaara5525202006-03-02 22:52:09 +000021616 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021617 if (varp->v_type == VAR_LIST)
21618 {
21619 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021620 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021621 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021622 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021623
21624 l = varp->vval.v_list;
21625 if (l == NULL)
21626 return NULL;
21627
21628 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021629 pos.lnum = list_find_nr(l, 0L, &error);
21630 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021631 return NULL; /* invalid line number */
21632
21633 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021634 pos.col = list_find_nr(l, 1L, &error);
21635 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021636 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021637 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021638
21639 /* We accept "$" for the column number: last column. */
21640 li = list_find(l, 1L);
21641 if (li != NULL && li->li_tv.v_type == VAR_STRING
21642 && li->li_tv.vval.v_string != NULL
21643 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21644 pos.col = len + 1;
21645
Bram Moolenaara5525202006-03-02 22:52:09 +000021646 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021647 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021648 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021649 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021650
Bram Moolenaara5525202006-03-02 22:52:09 +000021651#ifdef FEAT_VIRTUALEDIT
21652 /* Get the virtual offset. Defaults to zero. */
21653 pos.coladd = list_find_nr(l, 2L, &error);
21654 if (error)
21655 pos.coladd = 0;
21656#endif
21657
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021658 return &pos;
21659 }
21660
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021661 name = get_tv_string_chk(varp);
21662 if (name == NULL)
21663 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021664 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021665 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021666 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21667 {
21668 if (VIsual_active)
21669 return &VIsual;
21670 return &curwin->w_cursor;
21671 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021672 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021674 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021675 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21676 return NULL;
21677 return pp;
21678 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021679
21680#ifdef FEAT_VIRTUALEDIT
21681 pos.coladd = 0;
21682#endif
21683
Bram Moolenaar477933c2007-07-17 14:32:23 +000021684 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021685 {
21686 pos.col = 0;
21687 if (name[1] == '0') /* "w0": first visible line */
21688 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021689 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021690 pos.lnum = curwin->w_topline;
21691 return &pos;
21692 }
21693 else if (name[1] == '$') /* "w$": last visible line */
21694 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021695 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021696 pos.lnum = curwin->w_botline - 1;
21697 return &pos;
21698 }
21699 }
21700 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021702 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021703 {
21704 pos.lnum = curbuf->b_ml.ml_line_count;
21705 pos.col = 0;
21706 }
21707 else
21708 {
21709 pos.lnum = curwin->w_cursor.lnum;
21710 pos.col = (colnr_T)STRLEN(ml_get_curline());
21711 }
21712 return &pos;
21713 }
21714 return NULL;
21715}
21716
21717/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021718 * Convert list in "arg" into a position and optional file number.
21719 * When "fnump" is NULL there is no file number, only 3 items.
21720 * Note that the column is passed on as-is, the caller may want to decrement
21721 * it to use 1 for the first column.
21722 * Return FAIL when conversion is not possible, doesn't check the position for
21723 * validity.
21724 */
21725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021726list2fpos(
21727 typval_T *arg,
21728 pos_T *posp,
21729 int *fnump,
21730 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021731{
21732 list_T *l = arg->vval.v_list;
21733 long i = 0;
21734 long n;
21735
Bram Moolenaar493c1782014-05-28 14:34:46 +020021736 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21737 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021738 if (arg->v_type != VAR_LIST
21739 || l == NULL
21740 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021741 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021742 return FAIL;
21743
21744 if (fnump != NULL)
21745 {
21746 n = list_find_nr(l, i++, NULL); /* fnum */
21747 if (n < 0)
21748 return FAIL;
21749 if (n == 0)
21750 n = curbuf->b_fnum; /* current buffer */
21751 *fnump = n;
21752 }
21753
21754 n = list_find_nr(l, i++, NULL); /* lnum */
21755 if (n < 0)
21756 return FAIL;
21757 posp->lnum = n;
21758
21759 n = list_find_nr(l, i++, NULL); /* col */
21760 if (n < 0)
21761 return FAIL;
21762 posp->col = n;
21763
21764#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021765 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021766 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021767 posp->coladd = 0;
21768 else
21769 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021770#endif
21771
Bram Moolenaar493c1782014-05-28 14:34:46 +020021772 if (curswantp != NULL)
21773 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21774
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021775 return OK;
21776}
21777
21778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 * Get the length of an environment variable name.
21780 * Advance "arg" to the first character after the name.
21781 * Return 0 for error.
21782 */
21783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021784get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785{
21786 char_u *p;
21787 int len;
21788
21789 for (p = *arg; vim_isIDc(*p); ++p)
21790 ;
21791 if (p == *arg) /* no name found */
21792 return 0;
21793
21794 len = (int)(p - *arg);
21795 *arg = p;
21796 return len;
21797}
21798
21799/*
21800 * Get the length of the name of a function or internal variable.
21801 * "arg" is advanced to the first non-white character after the name.
21802 * Return 0 if something is wrong.
21803 */
21804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021805get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806{
21807 char_u *p;
21808 int len;
21809
21810 /* Find the end of the name. */
21811 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021812 {
21813 if (*p == ':')
21814 {
21815 /* "s:" is start of "s:var", but "n:" is not and can be used in
21816 * slice "[n:]". Also "xx:" is not a namespace. */
21817 len = (int)(p - *arg);
21818 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21819 || len > 1)
21820 break;
21821 }
21822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021823 if (p == *arg) /* no name found */
21824 return 0;
21825
21826 len = (int)(p - *arg);
21827 *arg = skipwhite(p);
21828
21829 return len;
21830}
21831
21832/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021833 * Get the length of the name of a variable or function.
21834 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021836 * Return -1 if curly braces expansion failed.
21837 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021838 * If the name contains 'magic' {}'s, expand them and return the
21839 * expanded name in an allocated string via 'alias' - caller must free.
21840 */
21841 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021842get_name_len(
21843 char_u **arg,
21844 char_u **alias,
21845 int evaluate,
21846 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021847{
21848 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021849 char_u *p;
21850 char_u *expr_start;
21851 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021852
21853 *alias = NULL; /* default to no alias */
21854
21855 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21856 && (*arg)[2] == (int)KE_SNR)
21857 {
21858 /* hard coded <SNR>, already translated */
21859 *arg += 3;
21860 return get_id_len(arg) + 3;
21861 }
21862 len = eval_fname_script(*arg);
21863 if (len > 0)
21864 {
21865 /* literal "<SID>", "s:" or "<SNR>" */
21866 *arg += len;
21867 }
21868
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021870 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021871 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021872 p = find_name_end(*arg, &expr_start, &expr_end,
21873 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021874 if (expr_start != NULL)
21875 {
21876 char_u *temp_string;
21877
21878 if (!evaluate)
21879 {
21880 len += (int)(p - *arg);
21881 *arg = skipwhite(p);
21882 return len;
21883 }
21884
21885 /*
21886 * Include any <SID> etc in the expanded string:
21887 * Thus the -len here.
21888 */
21889 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21890 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021891 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 *alias = temp_string;
21893 *arg = skipwhite(p);
21894 return (int)STRLEN(temp_string);
21895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896
21897 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021898 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021899 EMSG2(_(e_invexpr2), *arg);
21900
21901 return len;
21902}
21903
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021904/*
21905 * Find the end of a variable or function name, taking care of magic braces.
21906 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21907 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021908 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021909 * Return a pointer to just after the name. Equal to "arg" if there is no
21910 * valid name.
21911 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021912 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021913find_name_end(
21914 char_u *arg,
21915 char_u **expr_start,
21916 char_u **expr_end,
21917 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021918{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021919 int mb_nest = 0;
21920 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021922 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021924 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021926 *expr_start = NULL;
21927 *expr_end = NULL;
21928 }
21929
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021930 /* Quick check for valid starting character. */
21931 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21932 return arg;
21933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021934 for (p = arg; *p != NUL
21935 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021936 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021937 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021938 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021939 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021940 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021941 if (*p == '\'')
21942 {
21943 /* skip over 'string' to avoid counting [ and ] inside it. */
21944 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21945 ;
21946 if (*p == NUL)
21947 break;
21948 }
21949 else if (*p == '"')
21950 {
21951 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21952 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21953 if (*p == '\\' && p[1] != NUL)
21954 ++p;
21955 if (*p == NUL)
21956 break;
21957 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021958 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21959 {
21960 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021961 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021962 len = (int)(p - arg);
21963 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021964 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021965 break;
21966 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021967
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021968 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021970 if (*p == '[')
21971 ++br_nest;
21972 else if (*p == ']')
21973 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021974 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021975
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021976 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021978 if (*p == '{')
21979 {
21980 mb_nest++;
21981 if (expr_start != NULL && *expr_start == NULL)
21982 *expr_start = p;
21983 }
21984 else if (*p == '}')
21985 {
21986 mb_nest--;
21987 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21988 *expr_end = p;
21989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
21992
21993 return p;
21994}
21995
21996/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021997 * Expands out the 'magic' {}'s in a variable/function name.
21998 * Note that this can call itself recursively, to deal with
21999 * constructs like foo{bar}{baz}{bam}
22000 * The four pointer arguments point to "foo{expre}ss{ion}bar"
22001 * "in_start" ^
22002 * "expr_start" ^
22003 * "expr_end" ^
22004 * "in_end" ^
22005 *
22006 * Returns a new allocated string, which the caller must free.
22007 * Returns NULL for failure.
22008 */
22009 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022010make_expanded_name(
22011 char_u *in_start,
22012 char_u *expr_start,
22013 char_u *expr_end,
22014 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022015{
22016 char_u c1;
22017 char_u *retval = NULL;
22018 char_u *temp_result;
22019 char_u *nextcmd = NULL;
22020
22021 if (expr_end == NULL || in_end == NULL)
22022 return NULL;
22023 *expr_start = NUL;
22024 *expr_end = NUL;
22025 c1 = *in_end;
22026 *in_end = NUL;
22027
Bram Moolenaar362e1a32006-03-06 23:29:24 +000022028 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022029 if (temp_result != NULL && nextcmd == NULL)
22030 {
22031 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
22032 + (in_end - expr_end) + 1));
22033 if (retval != NULL)
22034 {
22035 STRCPY(retval, in_start);
22036 STRCAT(retval, temp_result);
22037 STRCAT(retval, expr_end + 1);
22038 }
22039 }
22040 vim_free(temp_result);
22041
22042 *in_end = c1; /* put char back for error messages */
22043 *expr_start = '{';
22044 *expr_end = '}';
22045
22046 if (retval != NULL)
22047 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022048 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022049 if (expr_start != NULL)
22050 {
22051 /* Further expansion! */
22052 temp_result = make_expanded_name(retval, expr_start,
22053 expr_end, temp_result);
22054 vim_free(retval);
22055 retval = temp_result;
22056 }
22057 }
22058
22059 return retval;
22060}
22061
22062/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022064 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 */
22066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022067eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022068{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022069 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
22070}
22071
22072/*
22073 * Return TRUE if character "c" can be used as the first character in a
22074 * variable or function name (excluding '{' and '}').
22075 */
22076 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022077eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022078{
22079 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000022080}
22081
22082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022083 * Set number v: variable to "val".
22084 */
22085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022086set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022087{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022088 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089}
22090
22091/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022092 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022093 */
22094 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022095get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022096{
Bram Moolenaare9a41262005-01-15 22:18:47 +000022097 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022098}
22099
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022100/*
22101 * Get string v: variable value. Uses a static buffer, can only be used once.
22102 */
22103 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022105{
22106 return get_tv_string(&vimvars[idx].vv_tv);
22107}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000022108
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022110 * Get List v: variable value. Caller must take care of reference count when
22111 * needed.
22112 */
22113 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022114get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000022115{
22116 return vimvars[idx].vv_list;
22117}
22118
22119/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022120 * Set v:char to character "c".
22121 */
22122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022123set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022124{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020022125 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000022126
22127#ifdef FEAT_MBYTE
22128 if (has_mbyte)
22129 buf[(*mb_char2bytes)(c, buf)] = NUL;
22130 else
22131#endif
22132 {
22133 buf[0] = c;
22134 buf[1] = NUL;
22135 }
22136 set_vim_var_string(VV_CHAR, buf, -1);
22137}
22138
22139/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022140 * Set v:count to "count" and v:count1 to "count1".
22141 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022142 */
22143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022144set_vcount(
22145 long count,
22146 long count1,
22147 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022148{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000022149 if (set_prevcount)
22150 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022151 vimvars[VV_COUNT].vv_nr = count;
22152 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022153}
22154
22155/*
22156 * Set string v: variable to a copy of "val".
22157 */
22158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022159set_vim_var_string(
22160 int idx,
22161 char_u *val,
22162 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022163{
Bram Moolenaara542c682016-01-31 16:28:04 +010022164 clear_tv(&vimvars[idx].vv_di.di_tv);
22165 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022167 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022169 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022171 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172}
22173
22174/*
Bram Moolenaard812df62008-11-09 12:46:09 +000022175 * Set List v: variable to "val".
22176 */
22177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022178set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000022179{
Bram Moolenaara542c682016-01-31 16:28:04 +010022180 clear_tv(&vimvars[idx].vv_di.di_tv);
22181 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000022182 vimvars[idx].vv_list = val;
22183 if (val != NULL)
22184 ++val->lv_refcount;
22185}
22186
22187/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020022188 * Set Dictionary v: variable to "val".
22189 */
22190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022191set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020022192{
22193 int todo;
22194 hashitem_T *hi;
22195
Bram Moolenaara542c682016-01-31 16:28:04 +010022196 clear_tv(&vimvars[idx].vv_di.di_tv);
22197 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020022198 vimvars[idx].vv_dict = val;
22199 if (val != NULL)
22200 {
22201 ++val->dv_refcount;
22202
22203 /* Set readonly */
22204 todo = (int)val->dv_hashtab.ht_used;
22205 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
22206 {
22207 if (HASHITEM_EMPTY(hi))
22208 continue;
22209 --todo;
22210 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22211 }
22212 }
22213}
22214
22215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216 * Set v:register if needed.
22217 */
22218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022219set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220{
22221 char_u regname;
22222
22223 if (c == 0 || c == ' ')
22224 regname = '"';
22225 else
22226 regname = c;
22227 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000022228 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 set_vim_var_string(VV_REG, &regname, 1);
22230}
22231
22232/*
22233 * Get or set v:exception. If "oldval" == NULL, return the current value.
22234 * Otherwise, restore the value to "oldval" and return NULL.
22235 * Must always be called in pairs to save and restore v:exception! Does not
22236 * take care of memory allocations.
22237 */
22238 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022240{
22241 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022242 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243
Bram Moolenaare9a41262005-01-15 22:18:47 +000022244 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 return NULL;
22246}
22247
22248/*
22249 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
22250 * Otherwise, restore the value to "oldval" and return NULL.
22251 * Must always be called in pairs to save and restore v:throwpoint! Does not
22252 * take care of memory allocations.
22253 */
22254 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022255v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022256{
22257 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022258 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259
Bram Moolenaare9a41262005-01-15 22:18:47 +000022260 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022261 return NULL;
22262}
22263
22264#if defined(FEAT_AUTOCMD) || defined(PROTO)
22265/*
22266 * Set v:cmdarg.
22267 * If "eap" != NULL, use "eap" to generate the value and return the old value.
22268 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
22269 * Must always be called in pairs!
22270 */
22271 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022272set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022273{
22274 char_u *oldval;
22275 char_u *newval;
22276 unsigned len;
22277
Bram Moolenaare9a41262005-01-15 22:18:47 +000022278 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022279 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022280 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022281 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000022282 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022283 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022284 }
22285
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022286 if (eap->force_bin == FORCE_BIN)
22287 len = 6;
22288 else if (eap->force_bin == FORCE_NOBIN)
22289 len = 8;
22290 else
22291 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022292
22293 if (eap->read_edit)
22294 len += 7;
22295
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022296 if (eap->force_ff != 0)
22297 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
22298# ifdef FEAT_MBYTE
22299 if (eap->force_enc != 0)
22300 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022301 if (eap->bad_char != 0)
22302 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022303# endif
22304
22305 newval = alloc(len + 1);
22306 if (newval == NULL)
22307 return NULL;
22308
22309 if (eap->force_bin == FORCE_BIN)
22310 sprintf((char *)newval, " ++bin");
22311 else if (eap->force_bin == FORCE_NOBIN)
22312 sprintf((char *)newval, " ++nobin");
22313 else
22314 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022315
22316 if (eap->read_edit)
22317 STRCAT(newval, " ++edit");
22318
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022319 if (eap->force_ff != 0)
22320 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22321 eap->cmd + eap->force_ff);
22322# ifdef FEAT_MBYTE
22323 if (eap->force_enc != 0)
22324 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22325 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022326 if (eap->bad_char == BAD_KEEP)
22327 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22328 else if (eap->bad_char == BAD_DROP)
22329 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22330 else if (eap->bad_char != 0)
22331 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022332# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022333 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022334 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335}
22336#endif
22337
22338/*
22339 * Get the value of internal variable "name".
22340 * Return OK or FAIL.
22341 */
22342 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022343get_var_tv(
22344 char_u *name,
22345 int len, /* length of "name" */
22346 typval_T *rettv, /* NULL when only checking existence */
22347 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22348 int verbose, /* may give error message */
22349 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022350{
22351 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 typval_T *tv = NULL;
22353 typval_T atv;
22354 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022355 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022356
22357 /* truncate the name, so that we can use strcmp() */
22358 cc = name[len];
22359 name[len] = NUL;
22360
22361 /*
22362 * Check for "b:changedtick".
22363 */
22364 if (STRCMP(name, "b:changedtick") == 0)
22365 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022366 atv.v_type = VAR_NUMBER;
22367 atv.vval.v_number = curbuf->b_changedtick;
22368 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 }
22370
22371 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 * Check for user-defined variables.
22373 */
22374 else
22375 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022376 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022377 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022378 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022380 if (dip != NULL)
22381 *dip = v;
22382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 }
22384
Bram Moolenaare9a41262005-01-15 22:18:47 +000022385 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022387 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022388 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389 ret = FAIL;
22390 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022391 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022392 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393
22394 name[len] = cc;
22395
22396 return ret;
22397}
22398
22399/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022400 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22401 * Also handle function call with Funcref variable: func(expr)
22402 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22403 */
22404 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022405handle_subscript(
22406 char_u **arg,
22407 typval_T *rettv,
22408 int evaluate, /* do more than finding the end */
22409 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022410{
22411 int ret = OK;
22412 dict_T *selfdict = NULL;
22413 char_u *s;
22414 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022415 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022416
22417 while (ret == OK
22418 && (**arg == '['
22419 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022420 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
22421 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022422 && !vim_iswhite(*(*arg - 1)))
22423 {
22424 if (**arg == '(')
22425 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010022426 partial_T *pt = NULL;
22427
Bram Moolenaard9fba312005-06-26 22:34:35 +000022428 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022429 if (evaluate)
22430 {
22431 functv = *rettv;
22432 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022433
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022434 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022435 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022436 {
22437 pt = functv.vval.v_partial;
22438 s = pt->pt_name;
22439 }
22440 else
22441 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022442 }
22443 else
22444 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022445 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022446 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022447 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000022448
22449 /* Clear the funcref afterwards, so that deleting it while
22450 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022451 if (evaluate)
22452 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022453
22454 /* Stop the expression evaluation when immediately aborting on
22455 * error, or when an interrupt occurred or an exception was thrown
22456 * but not caught. */
22457 if (aborting())
22458 {
22459 if (ret == OK)
22460 clear_tv(rettv);
22461 ret = FAIL;
22462 }
22463 dict_unref(selfdict);
22464 selfdict = NULL;
22465 }
22466 else /* **arg == '[' || **arg == '.' */
22467 {
22468 dict_unref(selfdict);
22469 if (rettv->v_type == VAR_DICT)
22470 {
22471 selfdict = rettv->vval.v_dict;
22472 if (selfdict != NULL)
22473 ++selfdict->dv_refcount;
22474 }
22475 else
22476 selfdict = NULL;
22477 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22478 {
22479 clear_tv(rettv);
22480 ret = FAIL;
22481 }
22482 }
22483 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022484
Bram Moolenaar1d429612016-05-24 15:44:17 +020022485 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
22486 * Don't do this when "Func" is already a partial that was bound
22487 * explicitly (pt_auto is FALSE). */
22488 if (selfdict != NULL
22489 && (rettv->v_type == VAR_FUNC
22490 || (rettv->v_type == VAR_PARTIAL
22491 && (rettv->vval.v_partial->pt_auto
22492 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022493 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022494 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
22495 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022496 char_u *tofree = NULL;
22497 ufunc_T *fp;
22498 char_u fname_buf[FLEN_FIXED + 1];
22499 int error;
22500
22501 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022502 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010022503 fp = find_func(fname);
22504 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022505
Bram Moolenaar65639032016-03-16 21:40:30 +010022506 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022507 {
Bram Moolenaar65639032016-03-16 21:40:30 +010022508 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
22509
22510 if (pt != NULL)
22511 {
22512 pt->pt_refcount = 1;
22513 pt->pt_dict = selfdict;
Bram Moolenaar1d429612016-05-24 15:44:17 +020022514 pt->pt_auto = TRUE;
Bram Moolenaar65639032016-03-16 21:40:30 +010022515 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022516 if (rettv->v_type == VAR_FUNC)
22517 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022518 /* Just a function: Take over the function name and use
22519 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022520 pt->pt_name = rettv->vval.v_string;
22521 }
22522 else
22523 {
22524 partial_T *ret_pt = rettv->vval.v_partial;
22525 int i;
22526
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022527 /* Partial: copy the function name, use selfdict and copy
22528 * args. Can't take over name or args, the partial might
22529 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022530 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010022531 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010022532 if (ret_pt->pt_argc > 0)
22533 {
22534 pt->pt_argv = (typval_T *)alloc(
22535 sizeof(typval_T) * ret_pt->pt_argc);
22536 if (pt->pt_argv == NULL)
22537 /* out of memory: drop the arguments */
22538 pt->pt_argc = 0;
22539 else
22540 {
22541 pt->pt_argc = ret_pt->pt_argc;
22542 for (i = 0; i < pt->pt_argc; i++)
22543 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
22544 }
22545 }
22546 partial_unref(ret_pt);
22547 }
Bram Moolenaar65639032016-03-16 21:40:30 +010022548 rettv->v_type = VAR_PARTIAL;
22549 rettv->vval.v_partial = pt;
22550 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010022551 }
22552 }
22553
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022554 dict_unref(selfdict);
22555 return ret;
22556}
22557
22558/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022559 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022560 * value).
22561 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022562 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022563alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022564{
Bram Moolenaar33570922005-01-25 22:26:29 +000022565 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022566}
22567
22568/*
22569 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 * The string "s" must have been allocated, it is consumed.
22571 * Return NULL for out of memory, the variable otherwise.
22572 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022573 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022574alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575{
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022578 rettv = alloc_tv();
22579 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022581 rettv->v_type = VAR_STRING;
22582 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022583 }
22584 else
22585 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022586 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022587}
22588
22589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022590 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022592 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022593free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594{
22595 if (varp != NULL)
22596 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022597 switch (varp->v_type)
22598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022599 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022600 func_unref(varp->vval.v_string);
22601 /*FALLTHROUGH*/
22602 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022603 vim_free(varp->vval.v_string);
22604 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022605 case VAR_PARTIAL:
22606 partial_unref(varp->vval.v_partial);
22607 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022608 case VAR_LIST:
22609 list_unref(varp->vval.v_list);
22610 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022611 case VAR_DICT:
22612 dict_unref(varp->vval.v_dict);
22613 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022614 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022615#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022616 job_unref(varp->vval.v_job);
22617 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022618#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022619 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022620#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022621 channel_unref(varp->vval.v_channel);
22622 break;
22623#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022624 case VAR_NUMBER:
22625 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022626 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022627 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022628 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 vim_free(varp);
22631 }
22632}
22633
22634/*
22635 * Free the memory for a variable value and set the value to NULL or 0.
22636 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022638clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639{
22640 if (varp != NULL)
22641 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022642 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022644 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022645 func_unref(varp->vval.v_string);
22646 /*FALLTHROUGH*/
22647 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022648 vim_free(varp->vval.v_string);
22649 varp->vval.v_string = NULL;
22650 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022651 case VAR_PARTIAL:
22652 partial_unref(varp->vval.v_partial);
22653 varp->vval.v_partial = NULL;
22654 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022655 case VAR_LIST:
22656 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022657 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022658 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022659 case VAR_DICT:
22660 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022661 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022662 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022663 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022664 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022665 varp->vval.v_number = 0;
22666 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022667 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022668#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022669 varp->vval.v_float = 0.0;
22670 break;
22671#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022672 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022673#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022674 job_unref(varp->vval.v_job);
22675 varp->vval.v_job = NULL;
22676#endif
22677 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022678 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022680 channel_unref(varp->vval.v_channel);
22681 varp->vval.v_channel = NULL;
22682#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022683 case VAR_UNKNOWN:
22684 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022686 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 }
22688}
22689
22690/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022691 * Set the value of a variable to NULL without freeing items.
22692 */
22693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022694init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022695{
22696 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022698}
22699
22700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022701 * Get the number value of a variable.
22702 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022703 * For incompatible types, return 0.
22704 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22705 * caller of incompatible types: it sets *denote to TRUE if "denote"
22706 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022708 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022709get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022711 int error = FALSE;
22712
22713 return get_tv_number_chk(varp, &error); /* return 0L on error */
22714}
22715
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022716 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022717get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022718{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022719 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022720
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022721 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022722 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022723 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022724 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022725 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022726#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022727 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022728 break;
22729#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022730 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022731 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022732 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022733 break;
22734 case VAR_STRING:
22735 if (varp->vval.v_string != NULL)
22736 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022737 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022738 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022739 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022740 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022741 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022742 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022743 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022744 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022745 case VAR_SPECIAL:
22746 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22747 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022748 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022749#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022750 EMSG(_("E910: Using a Job as a Number"));
22751 break;
22752#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022753 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022754#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022755 EMSG(_("E913: Using a Channel as a Number"));
22756 break;
22757#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022758 case VAR_UNKNOWN:
22759 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022760 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022762 if (denote == NULL) /* useful for values that must be unsigned */
22763 n = -1;
22764 else
22765 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022767}
22768
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022769#ifdef FEAT_FLOAT
22770 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022771get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022772{
22773 switch (varp->v_type)
22774 {
22775 case VAR_NUMBER:
22776 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022777 case VAR_FLOAT:
22778 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022779 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022780 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022781 EMSG(_("E891: Using a Funcref as a Float"));
22782 break;
22783 case VAR_STRING:
22784 EMSG(_("E892: Using a String as a Float"));
22785 break;
22786 case VAR_LIST:
22787 EMSG(_("E893: Using a List as a Float"));
22788 break;
22789 case VAR_DICT:
22790 EMSG(_("E894: Using a Dictionary as a Float"));
22791 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022792 case VAR_SPECIAL:
22793 EMSG(_("E907: Using a special value as a Float"));
22794 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022795 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022796# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022797 EMSG(_("E911: Using a Job as a Float"));
22798 break;
22799# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022800 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022801# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022802 EMSG(_("E914: Using a Channel as a Float"));
22803 break;
22804# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022805 case VAR_UNKNOWN:
22806 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022807 break;
22808 }
22809 return 0;
22810}
22811#endif
22812
Bram Moolenaar071d4272004-06-13 20:20:40 +000022813/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022814 * Get the lnum from the first argument.
22815 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022816 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022817 */
22818 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022819get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820{
Bram Moolenaar33570922005-01-25 22:26:29 +000022821 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022822 linenr_T lnum;
22823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022824 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 if (lnum == 0) /* no valid number, try using line() */
22826 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022827 rettv.v_type = VAR_NUMBER;
22828 f_line(argvars, &rettv);
22829 lnum = rettv.vval.v_number;
22830 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 }
22832 return lnum;
22833}
22834
22835/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022836 * Get the lnum from the first argument.
22837 * Also accepts "$", then "buf" is used.
22838 * Returns 0 on error.
22839 */
22840 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022841get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022842{
22843 if (argvars[0].v_type == VAR_STRING
22844 && argvars[0].vval.v_string != NULL
22845 && argvars[0].vval.v_string[0] == '$'
22846 && buf != NULL)
22847 return buf->b_ml.ml_line_count;
22848 return get_tv_number_chk(&argvars[0], NULL);
22849}
22850
22851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 * Get the string value of a variable.
22853 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022854 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22855 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 * If the String variable has never been set, return an empty string.
22857 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022858 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22859 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022862get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022863{
22864 static char_u mybuf[NUMBUFLEN];
22865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022866 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867}
22868
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022869 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022870get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022872 char_u *res = get_tv_string_buf_chk(varp, buf);
22873
22874 return res != NULL ? res : (char_u *)"";
22875}
22876
Bram Moolenaar7d647822014-04-05 21:28:56 +020022877/*
22878 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22879 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022880 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022881get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022882{
22883 static char_u mybuf[NUMBUFLEN];
22884
22885 return get_tv_string_buf_chk(varp, mybuf);
22886}
22887
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022888 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022889get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022890{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022891 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022892 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022893 case VAR_NUMBER:
22894 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22895 return buf;
22896 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022897 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022898 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022899 break;
22900 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022901 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022902 break;
22903 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022904 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022905 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022906 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022907#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022908 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022909 break;
22910#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022911 case VAR_STRING:
22912 if (varp->vval.v_string != NULL)
22913 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022914 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022915 case VAR_SPECIAL:
22916 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22917 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022918 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022919#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022920 {
22921 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022922 char *status;
22923
22924 if (job == NULL)
22925 return (char_u *)"no process";
22926 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022927 : job->jv_status == JOB_ENDED ? "dead"
22928 : "run";
22929# ifdef UNIX
22930 vim_snprintf((char *)buf, NUMBUFLEN,
22931 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022932# elif defined(WIN32)
22933 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022934 "process %ld %s",
22935 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022936 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022937# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022938 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022939 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22940# endif
22941 return buf;
22942 }
22943#endif
22944 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022945 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022946#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022947 {
22948 channel_T *channel = varp->vval.v_channel;
22949 char *status = channel_status(channel);
22950
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022951 if (channel == NULL)
22952 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22953 else
22954 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022955 "channel %d %s", channel->ch_id, status);
22956 return buf;
22957 }
22958#endif
22959 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022960 case VAR_UNKNOWN:
22961 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022962 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022964 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965}
22966
22967/*
22968 * Find variable "name" in the list of variables.
22969 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022970 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022971 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022972 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022973 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022975find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022977 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022978 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022979
Bram Moolenaara7043832005-01-21 11:56:39 +000022980 ht = find_var_ht(name, &varname);
22981 if (htp != NULL)
22982 *htp = ht;
22983 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022985 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022986}
22987
22988/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022989 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022990 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022991 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022993find_var_in_ht(
22994 hashtab_T *ht,
22995 int htname,
22996 char_u *varname,
22997 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022998{
Bram Moolenaar33570922005-01-25 22:26:29 +000022999 hashitem_T *hi;
23000
23001 if (*varname == NUL)
23002 {
23003 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020023004 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000023005 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023006 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023007 case 'g': return &globvars_var;
23008 case 'v': return &vimvars_var;
23009 case 'b': return &curbuf->b_bufvar;
23010 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023011#ifdef FEAT_WINDOWS
23012 case 't': return &curtab->tp_winvar;
23013#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023014 case 'l': return current_funccal == NULL
23015 ? NULL : &current_funccal->l_vars_var;
23016 case 'a': return current_funccal == NULL
23017 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000023018 }
23019 return NULL;
23020 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023021
23022 hi = hash_find(ht, varname);
23023 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023024 {
23025 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000023026 * worked find the variable again. Don't auto-load a script if it was
23027 * loaded already, otherwise it would be loaded every time when
23028 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023029 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023030 {
23031 /* Note: script_autoload() may make "hi" invalid. It must either
23032 * be obtained again or not used. */
23033 if (!script_autoload(varname, FALSE) || aborting())
23034 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023035 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010023036 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023037 if (HASHITEM_EMPTY(hi))
23038 return NULL;
23039 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023041}
23042
23043/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023044 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020023045 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000023046 * Set "varname" to the start of name without ':'.
23047 */
Bram Moolenaar33570922005-01-25 22:26:29 +000023048 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023049find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023051 hashitem_T *hi;
23052
Bram Moolenaar73627d02015-08-11 15:46:09 +020023053 if (name[0] == NUL)
23054 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 if (name[1] != ':')
23056 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023057 /* The name must not start with a colon or #. */
23058 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 return NULL;
23060 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000023061
23062 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000023063 hi = hash_find(&compat_hashtab, name);
23064 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000023065 return &compat_hashtab;
23066
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023068 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023069 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023070 }
23071 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023072 if (*name == 'g') /* global variable */
23073 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023074 /* There must be no ':' or '#' in the rest of the name, unless g: is used
23075 */
23076 if (vim_strchr(name + 2, ':') != NULL
23077 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023078 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023080 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023082 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023083#ifdef FEAT_WINDOWS
23084 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020023085 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023086#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000023087 if (*name == 'v') /* v: variable */
23088 return &vimvarht;
23089 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023090 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000023091 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023092 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 if (*name == 's' /* script variable */
23094 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
23095 return &SCRIPT_VARS(current_SID);
23096 return NULL;
23097}
23098
23099/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023100 * Get function call environment based on bactrace debug level
23101 */
23102 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023103get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023104{
23105 int i;
23106 funccall_T *funccal;
23107 funccall_T *temp_funccal;
23108
23109 funccal = current_funccal;
23110 if (debug_backtrace_level > 0)
23111 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010023112 for (i = 0; i < debug_backtrace_level; i++)
23113 {
23114 temp_funccal = funccal->caller;
23115 if (temp_funccal)
23116 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023117 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010023118 /* backtrace level overflow. reset to max */
23119 debug_backtrace_level = i;
23120 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010023121 }
23122 return funccal;
23123}
23124
23125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020023127 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 * Returns NULL when it doesn't exist.
23129 */
23130 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023131get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132{
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023135 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 if (v == NULL)
23137 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023138 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139}
23140
23141/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023142 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 * sourcing this script and when executing functions defined in the script.
23144 */
23145 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023146new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147{
Bram Moolenaara7043832005-01-21 11:56:39 +000023148 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000023149 hashtab_T *ht;
23150 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000023151
Bram Moolenaar071d4272004-06-13 20:20:40 +000023152 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
23153 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023154 /* Re-allocating ga_data means that an ht_array pointing to
23155 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000023156 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000023157 for (i = 1; i <= ga_scripts.ga_len; ++i)
23158 {
23159 ht = &SCRIPT_VARS(i);
23160 if (ht->ht_mask == HT_INIT_SIZE - 1)
23161 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023162 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000023163 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000023164 }
23165
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 while (ga_scripts.ga_len < id)
23167 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023168 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023169 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023170 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 }
23173 }
23174}
23175
23176/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023177 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
23178 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 */
23180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023181init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182{
Bram Moolenaar33570922005-01-25 22:26:29 +000023183 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020023184 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020023185 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023186 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023187 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000023188 dict_var->di_tv.vval.v_dict = dict;
23189 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023190 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023191 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23192 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023193}
23194
23195/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020023196 * Unreference a dictionary initialized by init_var_dict().
23197 */
23198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023199unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020023200{
23201 /* Now the dict needs to be freed if no one else is using it, go back to
23202 * normal reference counting. */
23203 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
23204 dict_unref(dict);
23205}
23206
23207/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023208 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000023209 * Frees all allocated variables and the value they contain.
23210 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 */
23212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023213vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000023214{
23215 vars_clear_ext(ht, TRUE);
23216}
23217
23218/*
23219 * Like vars_clear(), but only free the value if "free_val" is TRUE.
23220 */
23221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023222vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223{
Bram Moolenaara7043832005-01-21 11:56:39 +000023224 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023225 hashitem_T *hi;
23226 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227
Bram Moolenaar33570922005-01-25 22:26:29 +000023228 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023229 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000023230 for (hi = ht->ht_array; todo > 0; ++hi)
23231 {
23232 if (!HASHITEM_EMPTY(hi))
23233 {
23234 --todo;
23235
Bram Moolenaar33570922005-01-25 22:26:29 +000023236 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000023237 * ht_array might change then. hash_clear() takes care of it
23238 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023239 v = HI2DI(hi);
23240 if (free_val)
23241 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023242 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000023244 }
23245 }
23246 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000023247 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023248}
23249
Bram Moolenaara7043832005-01-21 11:56:39 +000023250/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023251 * Delete a variable from hashtab "ht" at item "hi".
23252 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000023253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023255delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256{
Bram Moolenaar33570922005-01-25 22:26:29 +000023257 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000023258
23259 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000023260 clear_tv(&di->di_tv);
23261 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262}
23263
23264/*
23265 * List the value of one internal variable.
23266 */
23267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023268list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023269{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023270 char_u *tofree;
23271 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023272 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023273
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023274 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000023275 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023276 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023277 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278}
23279
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023281list_one_var_a(
23282 char_u *prefix,
23283 char_u *name,
23284 int type,
23285 char_u *string,
23286 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287{
Bram Moolenaar31859182007-08-14 20:41:13 +000023288 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
23289 msg_start();
23290 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 if (name != NULL) /* "a:" vars don't have a name stored */
23292 msg_puts(name);
23293 msg_putchar(' ');
23294 msg_advance(22);
23295 if (type == VAR_NUMBER)
23296 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023297 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023298 msg_putchar('*');
23299 else if (type == VAR_LIST)
23300 {
23301 msg_putchar('[');
23302 if (*string == '[')
23303 ++string;
23304 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000023305 else if (type == VAR_DICT)
23306 {
23307 msg_putchar('{');
23308 if (*string == '{')
23309 ++string;
23310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 else
23312 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023313
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023315
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023316 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023317 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000023318 if (*first)
23319 {
23320 msg_clr_eos();
23321 *first = FALSE;
23322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323}
23324
23325/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023326 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023327 * If the variable already exists, the value is updated.
23328 * Otherwise the variable is created.
23329 */
23330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023331set_var(
23332 char_u *name,
23333 typval_T *tv,
23334 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023335{
Bram Moolenaar33570922005-01-25 22:26:29 +000023336 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000023338 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023340 ht = find_var_ht(name, &varname);
23341 if (ht == NULL || *varname == NUL)
23342 {
23343 EMSG2(_(e_illvar), name);
23344 return;
23345 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020023346 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010023347
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023348 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
23349 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023350 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023351
Bram Moolenaar33570922005-01-25 22:26:29 +000023352 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023355 if (var_check_ro(v->di_flags, name, FALSE)
23356 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000023357 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023358
23359 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023360 * Handle setting internal v: variables separately where needed to
23361 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000023362 */
23363 if (ht == &vimvarht)
23364 {
23365 if (v->di_tv.v_type == VAR_STRING)
23366 {
23367 vim_free(v->di_tv.vval.v_string);
23368 if (copy || tv->v_type != VAR_STRING)
23369 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
23370 else
23371 {
23372 /* Take over the string to avoid an extra alloc/free. */
23373 v->di_tv.vval.v_string = tv->vval.v_string;
23374 tv->vval.v_string = NULL;
23375 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023376 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023377 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023378 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023379 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023380 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023381 if (STRCMP(varname, "searchforward") == 0)
23382 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010023383#ifdef FEAT_SEARCH_EXTRA
23384 else if (STRCMP(varname, "hlsearch") == 0)
23385 {
23386 no_hlsearch = !v->di_tv.vval.v_number;
23387 redraw_all_later(SOME_VALID);
23388 }
23389#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023390 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023391 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023392 else if (v->di_tv.v_type != tv->v_type)
23393 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 }
23395
23396 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 }
23398 else /* add a new variable */
23399 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023400 /* Can't add "v:" variable. */
23401 if (ht == &vimvarht)
23402 {
23403 EMSG2(_(e_illvar), name);
23404 return;
23405 }
23406
Bram Moolenaar92124a32005-06-17 22:03:40 +000023407 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023408 if (!valid_varname(varname))
23409 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023410
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023411 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23412 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023413 if (v == NULL)
23414 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023415 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023416 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023418 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023419 return;
23420 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023421 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023423
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023424 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023425 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023426 else
23427 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023428 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023429 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023430 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432}
23433
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023434/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023435 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023436 * Also give an error message.
23437 */
23438 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023439var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023440{
23441 if (flags & DI_FLAGS_RO)
23442 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023443 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023444 return TRUE;
23445 }
23446 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23447 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023448 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023449 return TRUE;
23450 }
23451 return FALSE;
23452}
23453
23454/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023455 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23456 * Also give an error message.
23457 */
23458 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023459var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023460{
23461 if (flags & DI_FLAGS_FIX)
23462 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023463 EMSG2(_("E795: Cannot delete variable %s"),
23464 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023465 return TRUE;
23466 }
23467 return FALSE;
23468}
23469
23470/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023471 * Check if a funcref is assigned to a valid variable name.
23472 * Return TRUE and give an error if not.
23473 */
23474 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023475var_check_func_name(
23476 char_u *name, /* points to start of variable name */
23477 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023478{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023479 /* Allow for w: b: s: and t:. */
23480 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023481 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23482 ? name[2] : name[0]))
23483 {
23484 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23485 name);
23486 return TRUE;
23487 }
23488 /* Don't allow hiding a function. When "v" is not NULL we might be
23489 * assigning another function to the same var, the type is checked
23490 * below. */
23491 if (new_var && function_exists(name))
23492 {
23493 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23494 name);
23495 return TRUE;
23496 }
23497 return FALSE;
23498}
23499
23500/*
23501 * Check if a variable name is valid.
23502 * Return FALSE and give an error if not.
23503 */
23504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023505valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023506{
23507 char_u *p;
23508
23509 for (p = varname; *p != NUL; ++p)
23510 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23511 && *p != AUTOLOAD_CHAR)
23512 {
23513 EMSG2(_(e_illvar), varname);
23514 return FALSE;
23515 }
23516 return TRUE;
23517}
23518
23519/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023520 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023521 * Also give an error message, using "name" or _("name") when use_gettext is
23522 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023523 */
23524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023525tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023526{
23527 if (lock & VAR_LOCKED)
23528 {
23529 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023530 name == NULL ? (char_u *)_("Unknown")
23531 : use_gettext ? (char_u *)_(name)
23532 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023533 return TRUE;
23534 }
23535 if (lock & VAR_FIXED)
23536 {
23537 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023538 name == NULL ? (char_u *)_("Unknown")
23539 : use_gettext ? (char_u *)_(name)
23540 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023541 return TRUE;
23542 }
23543 return FALSE;
23544}
23545
23546/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023548 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023549 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023550 * It is OK for "from" and "to" to point to the same item. This is used to
23551 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023552 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023553 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023554copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023556 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023557 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023558 switch (from->v_type)
23559 {
23560 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023561 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023562 to->vval.v_number = from->vval.v_number;
23563 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023564 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023565#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023566 to->vval.v_float = from->vval.v_float;
23567 break;
23568#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023569 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023570#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010023571 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023572 if (to->vval.v_job != NULL)
23573 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023574 break;
23575#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023576 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010023577#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010023578 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023579 if (to->vval.v_channel != NULL)
23580 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023581 break;
23582#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023583 case VAR_STRING:
23584 case VAR_FUNC:
23585 if (from->vval.v_string == NULL)
23586 to->vval.v_string = NULL;
23587 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023589 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023590 if (from->v_type == VAR_FUNC)
23591 func_ref(to->vval.v_string);
23592 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023593 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023594 case VAR_PARTIAL:
23595 if (from->vval.v_partial == NULL)
23596 to->vval.v_partial = NULL;
23597 else
23598 {
23599 to->vval.v_partial = from->vval.v_partial;
23600 ++to->vval.v_partial->pt_refcount;
23601 }
23602 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023603 case VAR_LIST:
23604 if (from->vval.v_list == NULL)
23605 to->vval.v_list = NULL;
23606 else
23607 {
23608 to->vval.v_list = from->vval.v_list;
23609 ++to->vval.v_list->lv_refcount;
23610 }
23611 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023612 case VAR_DICT:
23613 if (from->vval.v_dict == NULL)
23614 to->vval.v_dict = NULL;
23615 else
23616 {
23617 to->vval.v_dict = from->vval.v_dict;
23618 ++to->vval.v_dict->dv_refcount;
23619 }
23620 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023621 case VAR_UNKNOWN:
23622 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023623 break;
23624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023625}
23626
23627/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023628 * Make a copy of an item.
23629 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023630 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23631 * reference to an already copied list/dict can be used.
23632 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023633 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023634 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023635item_copy(
23636 typval_T *from,
23637 typval_T *to,
23638 int deep,
23639 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023640{
23641 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023642 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023643
Bram Moolenaar33570922005-01-25 22:26:29 +000023644 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023645 {
23646 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023647 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023648 }
23649 ++recurse;
23650
23651 switch (from->v_type)
23652 {
23653 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023654 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023655 case VAR_STRING:
23656 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023657 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023658 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023659 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023660 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023661 copy_tv(from, to);
23662 break;
23663 case VAR_LIST:
23664 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023665 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023666 if (from->vval.v_list == NULL)
23667 to->vval.v_list = NULL;
23668 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23669 {
23670 /* use the copy made earlier */
23671 to->vval.v_list = from->vval.v_list->lv_copylist;
23672 ++to->vval.v_list->lv_refcount;
23673 }
23674 else
23675 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23676 if (to->vval.v_list == NULL)
23677 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023678 break;
23679 case VAR_DICT:
23680 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023681 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023682 if (from->vval.v_dict == NULL)
23683 to->vval.v_dict = NULL;
23684 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23685 {
23686 /* use the copy made earlier */
23687 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23688 ++to->vval.v_dict->dv_refcount;
23689 }
23690 else
23691 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23692 if (to->vval.v_dict == NULL)
23693 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023694 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023695 case VAR_UNKNOWN:
23696 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023697 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023698 }
23699 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023700 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023701}
23702
23703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 * ":echo expr1 ..." print each argument separated with a space, add a
23705 * newline at the end.
23706 * ":echon expr1 ..." print each argument plain.
23707 */
23708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023709ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023710{
23711 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023712 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023713 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023714 char_u *p;
23715 int needclr = TRUE;
23716 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023717 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718
23719 if (eap->skip)
23720 ++emsg_skip;
23721 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23722 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023723 /* If eval1() causes an error message the text from the command may
23724 * still need to be cleared. E.g., "echo 22,44". */
23725 need_clr_eos = needclr;
23726
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023728 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 {
23730 /*
23731 * Report the invalid expression unless the expression evaluation
23732 * has been cancelled due to an aborting error, an interrupt, or an
23733 * exception.
23734 */
23735 if (!aborting())
23736 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023737 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738 break;
23739 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023740 need_clr_eos = FALSE;
23741
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 if (!eap->skip)
23743 {
23744 if (atstart)
23745 {
23746 atstart = FALSE;
23747 /* Call msg_start() after eval1(), evaluating the expression
23748 * may cause a message to appear. */
23749 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023750 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023751 /* Mark the saved text as finishing the line, so that what
23752 * follows is displayed on a new line when scrolling back
23753 * at the more prompt. */
23754 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 }
23758 else if (eap->cmdidx == CMD_echo)
23759 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023760 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023761 if (p != NULL)
23762 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023764 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023766 if (*p != TAB && needclr)
23767 {
23768 /* remove any text still there from the command */
23769 msg_clr_eos();
23770 needclr = FALSE;
23771 }
23772 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 }
23774 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023775 {
23776#ifdef FEAT_MBYTE
23777 if (has_mbyte)
23778 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023779 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023780
23781 (void)msg_outtrans_len_attr(p, i, echo_attr);
23782 p += i - 1;
23783 }
23784 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023786 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023789 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023791 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 arg = skipwhite(arg);
23793 }
23794 eap->nextcmd = check_nextcmd(arg);
23795
23796 if (eap->skip)
23797 --emsg_skip;
23798 else
23799 {
23800 /* remove text that may still be there from the command */
23801 if (needclr)
23802 msg_clr_eos();
23803 if (eap->cmdidx == CMD_echo)
23804 msg_end();
23805 }
23806}
23807
23808/*
23809 * ":echohl {name}".
23810 */
23811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023812ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813{
23814 int id;
23815
23816 id = syn_name2id(eap->arg);
23817 if (id == 0)
23818 echo_attr = 0;
23819 else
23820 echo_attr = syn_id2attr(id);
23821}
23822
23823/*
23824 * ":execute expr1 ..." execute the result of an expression.
23825 * ":echomsg expr1 ..." Print a message
23826 * ":echoerr expr1 ..." Print an error
23827 * Each gets spaces around each argument and a newline at the end for
23828 * echo commands
23829 */
23830 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023831ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023832{
23833 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023834 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 int ret = OK;
23836 char_u *p;
23837 garray_T ga;
23838 int len;
23839 int save_did_emsg;
23840
23841 ga_init2(&ga, 1, 80);
23842
23843 if (eap->skip)
23844 ++emsg_skip;
23845 while (*arg != NUL && *arg != '|' && *arg != '\n')
23846 {
23847 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023848 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 {
23850 /*
23851 * Report the invalid expression unless the expression evaluation
23852 * has been cancelled due to an aborting error, an interrupt, or an
23853 * exception.
23854 */
23855 if (!aborting())
23856 EMSG2(_(e_invexpr2), p);
23857 ret = FAIL;
23858 break;
23859 }
23860
23861 if (!eap->skip)
23862 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023863 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023864 len = (int)STRLEN(p);
23865 if (ga_grow(&ga, len + 2) == FAIL)
23866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023867 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 ret = FAIL;
23869 break;
23870 }
23871 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023872 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 ga.ga_len += len;
23875 }
23876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023877 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878 arg = skipwhite(arg);
23879 }
23880
23881 if (ret != FAIL && ga.ga_data != NULL)
23882 {
23883 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023884 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023885 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023886 out_flush();
23887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 else if (eap->cmdidx == CMD_echoerr)
23889 {
23890 /* We don't want to abort following commands, restore did_emsg. */
23891 save_did_emsg = did_emsg;
23892 EMSG((char_u *)ga.ga_data);
23893 if (!force_abort)
23894 did_emsg = save_did_emsg;
23895 }
23896 else if (eap->cmdidx == CMD_execute)
23897 do_cmdline((char_u *)ga.ga_data,
23898 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23899 }
23900
23901 ga_clear(&ga);
23902
23903 if (eap->skip)
23904 --emsg_skip;
23905
23906 eap->nextcmd = check_nextcmd(arg);
23907}
23908
23909/*
23910 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23911 * "arg" points to the "&" or '+' when called, to "option" when returning.
23912 * Returns NULL when no option name found. Otherwise pointer to the char
23913 * after the option name.
23914 */
23915 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023916find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917{
23918 char_u *p = *arg;
23919
23920 ++p;
23921 if (*p == 'g' && p[1] == ':')
23922 {
23923 *opt_flags = OPT_GLOBAL;
23924 p += 2;
23925 }
23926 else if (*p == 'l' && p[1] == ':')
23927 {
23928 *opt_flags = OPT_LOCAL;
23929 p += 2;
23930 }
23931 else
23932 *opt_flags = 0;
23933
23934 if (!ASCII_ISALPHA(*p))
23935 return NULL;
23936 *arg = p;
23937
23938 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23939 p += 4; /* termcap option */
23940 else
23941 while (ASCII_ISALPHA(*p))
23942 ++p;
23943 return p;
23944}
23945
23946/*
23947 * ":function"
23948 */
23949 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023950ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023951{
23952 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023953 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 int j;
23955 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023957 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 char_u *name = NULL;
23959 char_u *p;
23960 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023961 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 garray_T newargs;
23963 garray_T newlines;
23964 int varargs = FALSE;
23965 int mustend = FALSE;
23966 int flags = 0;
23967 ufunc_T *fp;
23968 int indent;
23969 int nesting;
23970 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023971 dictitem_T *v;
23972 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023973 static int func_nr = 0; /* number for nameless function */
23974 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023975 hashtab_T *ht;
23976 int todo;
23977 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023978 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979
23980 /*
23981 * ":function" without argument: list functions.
23982 */
23983 if (ends_excmd(*eap->arg))
23984 {
23985 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023986 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023987 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023988 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023989 {
23990 if (!HASHITEM_EMPTY(hi))
23991 {
23992 --todo;
23993 fp = HI2UF(hi);
23994 if (!isdigit(*fp->uf_name))
23995 list_func_head(fp, FALSE);
23996 }
23997 }
23998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023999 eap->nextcmd = check_nextcmd(eap->arg);
24000 return;
24001 }
24002
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024004 * ":function /pat": list functions matching pattern.
24005 */
24006 if (*eap->arg == '/')
24007 {
24008 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
24009 if (!eap->skip)
24010 {
24011 regmatch_T regmatch;
24012
24013 c = *p;
24014 *p = NUL;
24015 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
24016 *p = c;
24017 if (regmatch.regprog != NULL)
24018 {
24019 regmatch.rm_ic = p_ic;
24020
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024021 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024022 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
24023 {
24024 if (!HASHITEM_EMPTY(hi))
24025 {
24026 --todo;
24027 fp = HI2UF(hi);
24028 if (!isdigit(*fp->uf_name)
24029 && vim_regexec(&regmatch, fp->uf_name, 0))
24030 list_func_head(fp, FALSE);
24031 }
24032 }
Bram Moolenaar473de612013-06-08 18:19:48 +020024033 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000024034 }
24035 }
24036 if (*p == '/')
24037 ++p;
24038 eap->nextcmd = check_nextcmd(p);
24039 return;
24040 }
24041
24042 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024043 * Get the function name. There are these situations:
24044 * func normal function name
24045 * "name" == func, "fudi.fd_dict" == NULL
24046 * dict.func new dictionary entry
24047 * "name" == NULL, "fudi.fd_dict" set,
24048 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
24049 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024050 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024051 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
24052 * dict.func existing dict entry that's not a Funcref
24053 * "name" == NULL, "fudi.fd_dict" set,
24054 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024055 * s:func script-local function name
24056 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024059 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024060 paren = (vim_strchr(p, '(') != NULL);
24061 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 {
24063 /*
24064 * Return on an invalid expression in braces, unless the expression
24065 * evaluation has been cancelled due to an aborting error, an
24066 * interrupt, or an exception.
24067 */
24068 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024069 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024070 if (!eap->skip && fudi.fd_newkey != NULL)
24071 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024072 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024073 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075 else
24076 eap->skip = TRUE;
24077 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000024078
Bram Moolenaar071d4272004-06-13 20:20:40 +000024079 /* An error in a function call during evaluation of an expression in magic
24080 * braces should not cause the function not to be defined. */
24081 saved_did_emsg = did_emsg;
24082 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083
24084 /*
24085 * ":function func" with only function name: list function.
24086 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024087 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 {
24089 if (!ends_excmd(*skipwhite(p)))
24090 {
24091 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024092 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 }
24094 eap->nextcmd = check_nextcmd(p);
24095 if (eap->nextcmd != NULL)
24096 *p = NUL;
24097 if (!eap->skip && !got_int)
24098 {
24099 fp = find_func(name);
24100 if (fp != NULL)
24101 {
24102 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024103 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024105 if (FUNCLINE(fp, j) == NULL)
24106 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 msg_putchar('\n');
24108 msg_outnum((long)(j + 1));
24109 if (j < 9)
24110 msg_putchar(' ');
24111 if (j < 99)
24112 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024113 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024114 out_flush(); /* show a line at a time */
24115 ui_breakcheck();
24116 }
24117 if (!got_int)
24118 {
24119 msg_putchar('\n');
24120 msg_puts((char_u *)" endfunction");
24121 }
24122 }
24123 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024124 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024126 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 }
24128
24129 /*
24130 * ":function name(arg1, arg2)" Define function.
24131 */
24132 p = skipwhite(p);
24133 if (*p != '(')
24134 {
24135 if (!eap->skip)
24136 {
24137 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024138 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024139 }
24140 /* attempt to continue by skipping some text */
24141 if (vim_strchr(p, '(') != NULL)
24142 p = vim_strchr(p, '(');
24143 }
24144 p = skipwhite(p + 1);
24145
24146 ga_init2(&newargs, (int)sizeof(char_u *), 3);
24147 ga_init2(&newlines, (int)sizeof(char_u *), 3);
24148
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024149 if (!eap->skip)
24150 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024151 /* Check the name of the function. Unless it's a dictionary function
24152 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024153 if (name != NULL)
24154 arg = name;
24155 else
24156 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000024157 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010024158 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
24159 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024160 {
24161 if (*arg == K_SPECIAL)
24162 j = 3;
24163 else
24164 j = 0;
24165 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
24166 : eval_isnamec(arg[j])))
24167 ++j;
24168 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000024169 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024170 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010024171 /* Disallow using the g: dict. */
24172 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
24173 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024174 }
24175
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 /*
24177 * Isolate the arguments: "arg1, arg2, ...)"
24178 */
24179 while (*p != ')')
24180 {
24181 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
24182 {
24183 varargs = TRUE;
24184 p += 3;
24185 mustend = TRUE;
24186 }
24187 else
24188 {
24189 arg = p;
24190 while (ASCII_ISALNUM(*p) || *p == '_')
24191 ++p;
24192 if (arg == p || isdigit(*arg)
24193 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
24194 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
24195 {
24196 if (!eap->skip)
24197 EMSG2(_("E125: Illegal argument: %s"), arg);
24198 break;
24199 }
24200 if (ga_grow(&newargs, 1) == FAIL)
24201 goto erret;
24202 c = *p;
24203 *p = NUL;
24204 arg = vim_strsave(arg);
24205 if (arg == NULL)
24206 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024207
24208 /* Check for duplicate argument name. */
24209 for (i = 0; i < newargs.ga_len; ++i)
24210 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
24211 {
24212 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010024213 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020024214 goto erret;
24215 }
24216
Bram Moolenaar071d4272004-06-13 20:20:40 +000024217 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
24218 *p = c;
24219 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 if (*p == ',')
24221 ++p;
24222 else
24223 mustend = TRUE;
24224 }
24225 p = skipwhite(p);
24226 if (mustend && *p != ')')
24227 {
24228 if (!eap->skip)
24229 EMSG2(_(e_invarg2), eap->arg);
24230 break;
24231 }
24232 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020024233 if (*p != ')')
24234 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235 ++p; /* skip the ')' */
24236
Bram Moolenaare9a41262005-01-15 22:18:47 +000024237 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024238 for (;;)
24239 {
24240 p = skipwhite(p);
24241 if (STRNCMP(p, "range", 5) == 0)
24242 {
24243 flags |= FC_RANGE;
24244 p += 5;
24245 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024246 else if (STRNCMP(p, "dict", 4) == 0)
24247 {
24248 flags |= FC_DICT;
24249 p += 4;
24250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024251 else if (STRNCMP(p, "abort", 5) == 0)
24252 {
24253 flags |= FC_ABORT;
24254 p += 5;
24255 }
24256 else
24257 break;
24258 }
24259
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024260 /* When there is a line break use what follows for the function body.
24261 * Makes 'exe "func Test()\n...\nendfunc"' work. */
24262 if (*p == '\n')
24263 line_arg = p + 1;
24264 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024265 EMSG(_(e_trailing));
24266
24267 /*
24268 * Read the body of the function, until ":endfunction" is found.
24269 */
24270 if (KeyTyped)
24271 {
24272 /* Check if the function already exists, don't let the user type the
24273 * whole function before telling him it doesn't work! For a script we
24274 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024275 if (!eap->skip && !eap->forceit)
24276 {
24277 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
24278 EMSG(_(e_funcdict));
24279 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024280 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024282
Bram Moolenaard857f0e2005-06-21 22:37:39 +000024283 if (!eap->skip && did_emsg)
24284 goto erret;
24285
Bram Moolenaar071d4272004-06-13 20:20:40 +000024286 msg_putchar('\n'); /* don't overwrite the function name */
24287 cmdline_row = msg_row;
24288 }
24289
24290 indent = 2;
24291 nesting = 0;
24292 for (;;)
24293 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024294 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024295 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020024296 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024297 saved_wait_return = FALSE;
24298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024299 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024300 sourcing_lnum_off = sourcing_lnum;
24301
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024302 if (line_arg != NULL)
24303 {
24304 /* Use eap->arg, split up in parts by line breaks. */
24305 theline = line_arg;
24306 p = vim_strchr(theline, '\n');
24307 if (p == NULL)
24308 line_arg += STRLEN(line_arg);
24309 else
24310 {
24311 *p = NUL;
24312 line_arg = p + 1;
24313 }
24314 }
24315 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316 theline = getcmdline(':', 0L, indent);
24317 else
24318 theline = eap->getline(':', eap->cookie, indent);
24319 if (KeyTyped)
24320 lines_left = Rows - 1;
24321 if (theline == NULL)
24322 {
24323 EMSG(_("E126: Missing :endfunction"));
24324 goto erret;
24325 }
24326
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024327 /* Detect line continuation: sourcing_lnum increased more than one. */
24328 if (sourcing_lnum > sourcing_lnum_off + 1)
24329 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
24330 else
24331 sourcing_lnum_off = 0;
24332
Bram Moolenaar071d4272004-06-13 20:20:40 +000024333 if (skip_until != NULL)
24334 {
24335 /* between ":append" and "." and between ":python <<EOF" and "EOF"
24336 * don't check for ":endfunc". */
24337 if (STRCMP(theline, skip_until) == 0)
24338 {
24339 vim_free(skip_until);
24340 skip_until = NULL;
24341 }
24342 }
24343 else
24344 {
24345 /* skip ':' and blanks*/
24346 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
24347 ;
24348
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024349 /* Check for "endfunction". */
24350 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024352 if (line_arg == NULL)
24353 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 break;
24355 }
24356
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024357 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358 * at "end". */
24359 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
24360 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024361 else if (STRNCMP(p, "if", 2) == 0
24362 || STRNCMP(p, "wh", 2) == 0
24363 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000024364 || STRNCMP(p, "try", 3) == 0)
24365 indent += 2;
24366
24367 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024368 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024369 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000024370 if (*p == '!')
24371 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010024373 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010024374 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375 {
Bram Moolenaaref923902014-12-13 21:00:55 +010024376 ++nesting;
24377 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024378 }
24379 }
24380
24381 /* Check for ":append" or ":insert". */
24382 p = skip_range(p, NULL);
24383 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
24384 || (p[0] == 'i'
24385 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
24386 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
24387 skip_until = vim_strsave((char_u *)".");
24388
24389 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
24390 arg = skipwhite(skiptowhite(p));
24391 if (arg[0] == '<' && arg[1] =='<'
24392 && ((p[0] == 'p' && p[1] == 'y'
24393 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
24394 || (p[0] == 'p' && p[1] == 'e'
24395 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24396 || (p[0] == 't' && p[1] == 'c'
24397 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024398 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24399 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24401 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024402 || (p[0] == 'm' && p[1] == 'z'
24403 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024404 ))
24405 {
24406 /* ":python <<" continues until a dot, like ":append" */
24407 p = skipwhite(arg + 2);
24408 if (*p == NUL)
24409 skip_until = vim_strsave((char_u *)".");
24410 else
24411 skip_until = vim_strsave(p);
24412 }
24413 }
24414
24415 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024416 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024417 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024418 if (line_arg == NULL)
24419 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024420 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024421 }
24422
24423 /* Copy the line to newly allocated memory. get_one_sourceline()
24424 * allocates 250 bytes per line, this saves 80% on average. The cost
24425 * is an extra alloc/free. */
24426 p = vim_strsave(theline);
24427 if (p != NULL)
24428 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024429 if (line_arg == NULL)
24430 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024431 theline = p;
24432 }
24433
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024434 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24435
24436 /* Add NULL lines for continuation lines, so that the line count is
24437 * equal to the index in the growarray. */
24438 while (sourcing_lnum_off-- > 0)
24439 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024440
24441 /* Check for end of eap->arg. */
24442 if (line_arg != NULL && *line_arg == NUL)
24443 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024444 }
24445
24446 /* Don't define the function when skipping commands or when an error was
24447 * detected. */
24448 if (eap->skip || did_emsg)
24449 goto erret;
24450
24451 /*
24452 * If there are no errors, add the function
24453 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024454 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024455 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024456 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024457 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024458 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024459 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024460 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024461 goto erret;
24462 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024463
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024464 fp = find_func(name);
24465 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024467 if (!eap->forceit)
24468 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024469 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024470 goto erret;
24471 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024472 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024473 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024474 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024475 name);
24476 goto erret;
24477 }
24478 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024479 ga_clear_strings(&(fp->uf_args));
24480 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024481 vim_free(name);
24482 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484 }
24485 else
24486 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024487 char numbuf[20];
24488
24489 fp = NULL;
24490 if (fudi.fd_newkey == NULL && !eap->forceit)
24491 {
24492 EMSG(_(e_funcdict));
24493 goto erret;
24494 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024495 if (fudi.fd_di == NULL)
24496 {
24497 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024498 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024499 goto erret;
24500 }
24501 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024502 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024503 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024504
24505 /* Give the function a sequential number. Can only be used with a
24506 * Funcref! */
24507 vim_free(name);
24508 sprintf(numbuf, "%d", ++func_nr);
24509 name = vim_strsave((char_u *)numbuf);
24510 if (name == NULL)
24511 goto erret;
24512 }
24513
24514 if (fp == NULL)
24515 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024516 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024517 {
24518 int slen, plen;
24519 char_u *scriptname;
24520
24521 /* Check that the autoload name matches the script name. */
24522 j = FAIL;
24523 if (sourcing_name != NULL)
24524 {
24525 scriptname = autoload_name(name);
24526 if (scriptname != NULL)
24527 {
24528 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024529 plen = (int)STRLEN(p);
24530 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024531 if (slen > plen && fnamecmp(p,
24532 sourcing_name + slen - plen) == 0)
24533 j = OK;
24534 vim_free(scriptname);
24535 }
24536 }
24537 if (j == FAIL)
24538 {
24539 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24540 goto erret;
24541 }
24542 }
24543
24544 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024545 if (fp == NULL)
24546 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024547
24548 if (fudi.fd_dict != NULL)
24549 {
24550 if (fudi.fd_di == NULL)
24551 {
24552 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024553 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024554 if (fudi.fd_di == NULL)
24555 {
24556 vim_free(fp);
24557 goto erret;
24558 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024559 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24560 {
24561 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024562 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024563 goto erret;
24564 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024565 }
24566 else
24567 /* overwrite existing dict entry */
24568 clear_tv(&fudi.fd_di->di_tv);
24569 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024570 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024571 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024572 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024573
24574 /* behave like "dict" was used */
24575 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024576 }
24577
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024579 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024580 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24581 {
24582 vim_free(fp);
24583 goto erret;
24584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024585 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024586 fp->uf_args = newargs;
24587 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024588#ifdef FEAT_PROFILE
24589 fp->uf_tml_count = NULL;
24590 fp->uf_tml_total = NULL;
24591 fp->uf_tml_self = NULL;
24592 fp->uf_profiling = FALSE;
24593 if (prof_def_func())
24594 func_do_profile(fp);
24595#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024596 fp->uf_varargs = varargs;
24597 fp->uf_flags = flags;
24598 fp->uf_calls = 0;
24599 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024600 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601
24602erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024603 ga_clear_strings(&newargs);
24604 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024605ret_free:
24606 vim_free(skip_until);
24607 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024608 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024609 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024610 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024611}
24612
24613/*
24614 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024615 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024616 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024617 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024618 * TFN_INT: internal function name OK
24619 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024620 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024621 * Advances "pp" to just after the function name (if no error).
24622 */
24623 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024624trans_function_name(
24625 char_u **pp,
24626 int skip, /* only find the end, don't evaluate */
24627 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024628 funcdict_T *fdp, /* return: info about dictionary used */
24629 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024631 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 char_u *start;
24633 char_u *end;
24634 int lead;
24635 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024636 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024637 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024638
24639 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024640 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024641 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024642
24643 /* Check for hard coded <SNR>: already translated function ID (from a user
24644 * command). */
24645 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24646 && (*pp)[2] == (int)KE_SNR)
24647 {
24648 *pp += 3;
24649 len = get_id_len(pp) + 3;
24650 return vim_strnsave(start, len);
24651 }
24652
24653 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24654 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024655 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024656 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024657 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024658
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024659 /* Note that TFN_ flags use the same values as GLV_ flags. */
24660 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024661 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024662 if (end == start)
24663 {
24664 if (!skip)
24665 EMSG(_("E129: Function name required"));
24666 goto theend;
24667 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024668 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024669 {
24670 /*
24671 * Report an invalid expression in braces, unless the expression
24672 * evaluation has been cancelled due to an aborting error, an
24673 * interrupt, or an exception.
24674 */
24675 if (!aborting())
24676 {
24677 if (end != NULL)
24678 EMSG2(_(e_invarg2), start);
24679 }
24680 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024681 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024682 goto theend;
24683 }
24684
24685 if (lv.ll_tv != NULL)
24686 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024687 if (fdp != NULL)
24688 {
24689 fdp->fd_dict = lv.ll_dict;
24690 fdp->fd_newkey = lv.ll_newkey;
24691 lv.ll_newkey = NULL;
24692 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024694 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24695 {
24696 name = vim_strsave(lv.ll_tv->vval.v_string);
24697 *pp = end;
24698 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024699 else if (lv.ll_tv->v_type == VAR_PARTIAL
24700 && lv.ll_tv->vval.v_partial != NULL)
24701 {
24702 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24703 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024704 if (partial != NULL)
24705 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024706 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024707 else
24708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024709 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24710 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024711 EMSG(_(e_funcref));
24712 else
24713 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024714 name = NULL;
24715 }
24716 goto theend;
24717 }
24718
24719 if (lv.ll_name == NULL)
24720 {
24721 /* Error found, but continue after the function name. */
24722 *pp = end;
24723 goto theend;
24724 }
24725
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024726 /* Check if the name is a Funcref. If so, use the value. */
24727 if (lv.ll_exp_name != NULL)
24728 {
24729 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024730 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024731 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024732 if (name == lv.ll_exp_name)
24733 name = NULL;
24734 }
24735 else
24736 {
24737 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024738 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024739 if (name == *pp)
24740 name = NULL;
24741 }
24742 if (name != NULL)
24743 {
24744 name = vim_strsave(name);
24745 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024746 if (STRNCMP(name, "<SNR>", 5) == 0)
24747 {
24748 /* Change "<SNR>" to the byte sequence. */
24749 name[0] = K_SPECIAL;
24750 name[1] = KS_EXTRA;
24751 name[2] = (int)KE_SNR;
24752 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24753 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024754 goto theend;
24755 }
24756
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024757 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024758 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024759 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024760 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24761 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24762 {
24763 /* When there was "s:" already or the name expanded to get a
24764 * leading "s:" then remove it. */
24765 lv.ll_name += 2;
24766 len -= 2;
24767 lead = 2;
24768 }
24769 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024770 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024771 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024772 /* skip over "s:" and "g:" */
24773 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024774 lv.ll_name += 2;
24775 len = (int)(end - lv.ll_name);
24776 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024777
24778 /*
24779 * Copy the function name to allocated memory.
24780 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24781 * Accept <SNR>123_name() outside a script.
24782 */
24783 if (skip)
24784 lead = 0; /* do nothing */
24785 else if (lead > 0)
24786 {
24787 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024788 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24789 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024790 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024791 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024792 if (current_SID <= 0)
24793 {
24794 EMSG(_(e_usingsid));
24795 goto theend;
24796 }
24797 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24798 lead += (int)STRLEN(sid_buf);
24799 }
24800 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024801 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024802 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024803 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024804 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024805 goto theend;
24806 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024807 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024808 {
24809 char_u *cp = vim_strchr(lv.ll_name, ':');
24810
24811 if (cp != NULL && cp < end)
24812 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024813 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024814 goto theend;
24815 }
24816 }
24817
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024818 name = alloc((unsigned)(len + lead + 1));
24819 if (name != NULL)
24820 {
24821 if (lead > 0)
24822 {
24823 name[0] = K_SPECIAL;
24824 name[1] = KS_EXTRA;
24825 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024826 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024827 STRCPY(name + 3, sid_buf);
24828 }
24829 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024830 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024831 }
24832 *pp = end;
24833
24834theend:
24835 clear_lval(&lv);
24836 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024837}
24838
24839/*
24840 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24841 * Return 2 if "p" starts with "s:".
24842 * Return 0 otherwise.
24843 */
24844 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024845eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024847 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24848 * the standard library function. */
24849 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24850 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 return 5;
24852 if (p[0] == 's' && p[1] == ':')
24853 return 2;
24854 return 0;
24855}
24856
24857/*
24858 * Return TRUE if "p" starts with "<SID>" or "s:".
24859 * Only works if eval_fname_script() returned non-zero for "p"!
24860 */
24861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024862eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863{
24864 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24865}
24866
24867/*
24868 * List the head of the function: "name(arg1, arg2)".
24869 */
24870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024871list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872{
24873 int j;
24874
24875 msg_start();
24876 if (indent)
24877 MSG_PUTS(" ");
24878 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024879 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024880 {
24881 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024882 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024883 }
24884 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024885 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024887 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 {
24889 if (j)
24890 MSG_PUTS(", ");
24891 msg_puts(FUNCARG(fp, j));
24892 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024893 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 {
24895 if (j)
24896 MSG_PUTS(", ");
24897 MSG_PUTS("...");
24898 }
24899 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024900 if (fp->uf_flags & FC_ABORT)
24901 MSG_PUTS(" abort");
24902 if (fp->uf_flags & FC_RANGE)
24903 MSG_PUTS(" range");
24904 if (fp->uf_flags & FC_DICT)
24905 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024906 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024907 if (p_verbose > 0)
24908 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909}
24910
24911/*
24912 * Find a function by name, return pointer to it in ufuncs.
24913 * Return NULL for unknown function.
24914 */
24915 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024916find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024917{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024918 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024920 hi = hash_find(&func_hashtab, name);
24921 if (!HASHITEM_EMPTY(hi))
24922 return HI2UF(hi);
24923 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024924}
24925
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024926#if defined(EXITFREE) || defined(PROTO)
24927 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024928free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024929{
24930 hashitem_T *hi;
24931
24932 /* Need to start all over every time, because func_free() may change the
24933 * hash table. */
24934 while (func_hashtab.ht_used > 0)
24935 for (hi = func_hashtab.ht_array; ; ++hi)
24936 if (!HASHITEM_EMPTY(hi))
24937 {
24938 func_free(HI2UF(hi));
24939 break;
24940 }
24941}
24942#endif
24943
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024944 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024945translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024946{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024947 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024948 return find_internal_func(name) >= 0;
24949 return find_func(name) != NULL;
24950}
24951
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024952/*
24953 * Return TRUE if a function "name" exists.
24954 */
24955 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024956function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024957{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024958 char_u *nm = name;
24959 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024960 int n = FALSE;
24961
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024962 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024963 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024964 nm = skipwhite(nm);
24965
24966 /* Only accept "funcname", "funcname ", "funcname (..." and
24967 * "funcname(...", not "funcname!...". */
24968 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024969 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024970 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024971 return n;
24972}
24973
Bram Moolenaara1544c02013-05-30 12:35:52 +020024974 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024975get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024976{
24977 char_u *nm = name;
24978 char_u *p;
24979
Bram Moolenaar65639032016-03-16 21:40:30 +010024980 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024981
24982 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024983 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024984 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024985
Bram Moolenaara1544c02013-05-30 12:35:52 +020024986 vim_free(p);
24987 return NULL;
24988}
24989
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024990/*
24991 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024992 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24993 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024994 */
24995 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024996builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024997{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024998 char_u *p;
24999
25000 if (!ASCII_ISLOWER(name[0]))
25001 return FALSE;
25002 p = vim_strchr(name, AUTOLOAD_CHAR);
25003 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025004}
25005
Bram Moolenaar05159a02005-02-26 23:04:13 +000025006#if defined(FEAT_PROFILE) || defined(PROTO)
25007/*
25008 * Start profiling function "fp".
25009 */
25010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025011func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025012{
Bram Moolenaar904c6222010-07-24 16:57:39 +020025013 int len = fp->uf_lines.ga_len;
25014
25015 if (len == 0)
25016 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025017 fp->uf_tm_count = 0;
25018 profile_zero(&fp->uf_tm_self);
25019 profile_zero(&fp->uf_tm_total);
25020 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025021 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025022 if (fp->uf_tml_total == NULL)
25023 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025024 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025025 if (fp->uf_tml_self == NULL)
25026 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020025027 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025028 fp->uf_tml_idx = -1;
25029 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
25030 || fp->uf_tml_self == NULL)
25031 return; /* out of memory */
25032
25033 fp->uf_profiling = TRUE;
25034}
25035
25036/*
25037 * Dump the profiling results for all functions in file "fd".
25038 */
25039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025040func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025041{
25042 hashitem_T *hi;
25043 int todo;
25044 ufunc_T *fp;
25045 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000025046 ufunc_T **sorttab;
25047 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025048
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025049 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025050 if (todo == 0)
25051 return; /* nothing to dump */
25052
Bram Moolenaare2e4b982015-06-09 20:30:51 +020025053 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000025054
Bram Moolenaar05159a02005-02-26 23:04:13 +000025055 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
25056 {
25057 if (!HASHITEM_EMPTY(hi))
25058 {
25059 --todo;
25060 fp = HI2UF(hi);
25061 if (fp->uf_profiling)
25062 {
Bram Moolenaar73830342005-02-28 22:48:19 +000025063 if (sorttab != NULL)
25064 sorttab[st_len++] = fp;
25065
Bram Moolenaar05159a02005-02-26 23:04:13 +000025066 if (fp->uf_name[0] == K_SPECIAL)
25067 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
25068 else
25069 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
25070 if (fp->uf_tm_count == 1)
25071 fprintf(fd, "Called 1 time\n");
25072 else
25073 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
25074 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
25075 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
25076 fprintf(fd, "\n");
25077 fprintf(fd, "count total (s) self (s)\n");
25078
25079 for (i = 0; i < fp->uf_lines.ga_len; ++i)
25080 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025081 if (FUNCLINE(fp, i) == NULL)
25082 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000025083 prof_func_line(fd, fp->uf_tml_count[i],
25084 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085 fprintf(fd, "%s\n", FUNCLINE(fp, i));
25086 }
25087 fprintf(fd, "\n");
25088 }
25089 }
25090 }
Bram Moolenaar73830342005-02-28 22:48:19 +000025091
25092 if (sorttab != NULL && st_len > 0)
25093 {
25094 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25095 prof_total_cmp);
25096 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
25097 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
25098 prof_self_cmp);
25099 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
25100 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000025101
25102 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025103}
Bram Moolenaar73830342005-02-28 22:48:19 +000025104
25105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025106prof_sort_list(
25107 FILE *fd,
25108 ufunc_T **sorttab,
25109 int st_len,
25110 char *title,
25111 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025112{
25113 int i;
25114 ufunc_T *fp;
25115
25116 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
25117 fprintf(fd, "count total (s) self (s) function\n");
25118 for (i = 0; i < 20 && i < st_len; ++i)
25119 {
25120 fp = sorttab[i];
25121 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
25122 prefer_self);
25123 if (fp->uf_name[0] == K_SPECIAL)
25124 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
25125 else
25126 fprintf(fd, " %s()\n", fp->uf_name);
25127 }
25128 fprintf(fd, "\n");
25129}
25130
25131/*
25132 * Print the count and times for one function or function line.
25133 */
25134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025135prof_func_line(
25136 FILE *fd,
25137 int count,
25138 proftime_T *total,
25139 proftime_T *self,
25140 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000025141{
25142 if (count > 0)
25143 {
25144 fprintf(fd, "%5d ", count);
25145 if (prefer_self && profile_equal(total, self))
25146 fprintf(fd, " ");
25147 else
25148 fprintf(fd, "%s ", profile_msg(total));
25149 if (!prefer_self && profile_equal(total, self))
25150 fprintf(fd, " ");
25151 else
25152 fprintf(fd, "%s ", profile_msg(self));
25153 }
25154 else
25155 fprintf(fd, " ");
25156}
25157
25158/*
25159 * Compare function for total time sorting.
25160 */
25161 static int
25162#ifdef __BORLANDC__
25163_RTLENTRYF
25164#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025165prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025166{
25167 ufunc_T *p1, *p2;
25168
25169 p1 = *(ufunc_T **)s1;
25170 p2 = *(ufunc_T **)s2;
25171 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
25172}
25173
25174/*
25175 * Compare function for self time sorting.
25176 */
25177 static int
25178#ifdef __BORLANDC__
25179_RTLENTRYF
25180#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010025181prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000025182{
25183 ufunc_T *p1, *p2;
25184
25185 p1 = *(ufunc_T **)s1;
25186 p2 = *(ufunc_T **)s2;
25187 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
25188}
25189
Bram Moolenaar05159a02005-02-26 23:04:13 +000025190#endif
25191
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025192/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025193 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025194 * Return TRUE if a package was loaded.
25195 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020025196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025197script_autoload(
25198 char_u *name,
25199 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025200{
25201 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025202 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025203 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025204 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025205
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025206 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025207 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025208 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025209 return FALSE;
25210
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025211 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025212
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025213 /* Find the name in the list of previously loaded package names. Skip
25214 * "autoload/", it's always the same. */
25215 for (i = 0; i < ga_loaded.ga_len; ++i)
25216 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
25217 break;
25218 if (!reload && i < ga_loaded.ga_len)
25219 ret = FALSE; /* was loaded already */
25220 else
25221 {
25222 /* Remember the name if it wasn't loaded already. */
25223 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
25224 {
25225 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
25226 tofree = NULL;
25227 }
25228
25229 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010025230 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000025231 ret = TRUE;
25232 }
25233
25234 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025235 return ret;
25236}
25237
25238/*
25239 * Return the autoload script name for a function or variable name.
25240 * Returns NULL when out of memory.
25241 */
25242 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025243autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025244{
25245 char_u *p;
25246 char_u *scriptname;
25247
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025248 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025249 scriptname = alloc((unsigned)(STRLEN(name) + 14));
25250 if (scriptname == NULL)
25251 return FALSE;
25252 STRCPY(scriptname, "autoload/");
25253 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025254 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025255 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000025256 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025257 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025258 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000025259}
25260
Bram Moolenaar071d4272004-06-13 20:20:40 +000025261#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
25262
25263/*
25264 * Function given to ExpandGeneric() to obtain the list of user defined
25265 * function names.
25266 */
25267 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025268get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025270 static long_u done;
25271 static hashitem_T *hi;
25272 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025273
25274 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025276 done = 0;
25277 hi = func_hashtab.ht_array;
25278 }
25279 if (done < func_hashtab.ht_used)
25280 {
25281 if (done++ > 0)
25282 ++hi;
25283 while (HASHITEM_EMPTY(hi))
25284 ++hi;
25285 fp = HI2UF(hi);
25286
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025287 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010025288 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010025289
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025290 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
25291 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292
25293 cat_func_name(IObuff, fp);
25294 if (xp->xp_context != EXPAND_USER_FUNC)
25295 {
25296 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025297 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025298 STRCAT(IObuff, ")");
25299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025300 return IObuff;
25301 }
25302 return NULL;
25303}
25304
25305#endif /* FEAT_CMDL_COMPL */
25306
25307/*
25308 * Copy the function name of "fp" to buffer "buf".
25309 * "buf" must be able to hold the function name plus three bytes.
25310 * Takes care of script-local function names.
25311 */
25312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025313cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025314{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025315 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 {
25317 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025318 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 }
25320 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025321 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322}
25323
25324/*
25325 * ":delfunction {name}"
25326 */
25327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025328ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025329{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025330 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331 char_u *p;
25332 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000025333 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025334
25335 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010025336 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025337 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025338 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025339 {
25340 if (fudi.fd_dict != NULL && !eap->skip)
25341 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025344 if (!ends_excmd(*skipwhite(p)))
25345 {
25346 vim_free(name);
25347 EMSG(_(e_trailing));
25348 return;
25349 }
25350 eap->nextcmd = check_nextcmd(p);
25351 if (eap->nextcmd != NULL)
25352 *p = NUL;
25353
25354 if (!eap->skip)
25355 fp = find_func(name);
25356 vim_free(name);
25357
25358 if (!eap->skip)
25359 {
25360 if (fp == NULL)
25361 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025362 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025363 return;
25364 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025365 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025366 {
25367 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
25368 return;
25369 }
25370
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025371 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025373 /* Delete the dict item that refers to the function, it will
25374 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000025375 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025377 else
25378 func_free(fp);
25379 }
25380}
25381
25382/*
25383 * Free a function and remove it from the list of functions.
25384 */
25385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025386func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025387{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025388 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025389
25390 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025391 ga_clear_strings(&(fp->uf_args));
25392 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000025393#ifdef FEAT_PROFILE
25394 vim_free(fp->uf_tml_count);
25395 vim_free(fp->uf_tml_total);
25396 vim_free(fp->uf_tml_self);
25397#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025398
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025399 /* remove the function from the function hashtable */
25400 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
25401 if (HASHITEM_EMPTY(hi))
25402 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025403 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025404 hash_remove(&func_hashtab, hi);
25405
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025406 vim_free(fp);
25407}
25408
25409/*
25410 * Unreference a Function: decrement the reference count and free it when it
25411 * becomes zero. Only for numbered functions.
25412 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025414func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025415{
25416 ufunc_T *fp;
25417
25418 if (name != NULL && isdigit(*name))
25419 {
25420 fp = find_func(name);
25421 if (fp == NULL)
Bram Moolenaara9673212016-06-01 22:21:06 +020025422 {
Bram Moolenaarb89a25f2016-06-01 23:08:39 +020025423#ifdef EXITFREE
25424 if (!entered_free_all_mem)
25425#endif
Bram Moolenaara9673212016-06-01 22:21:06 +020025426 EMSG2(_(e_intern2), "func_unref()");
25427 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025428 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025429 {
25430 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025431 * when "uf_calls" becomes zero. */
25432 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025433 func_free(fp);
25434 }
25435 }
25436}
25437
25438/*
25439 * Count a reference to a Function.
25440 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025441 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025442func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025443{
25444 ufunc_T *fp;
25445
25446 if (name != NULL && isdigit(*name))
25447 {
25448 fp = find_func(name);
25449 if (fp == NULL)
25450 EMSG2(_(e_intern2), "func_ref()");
25451 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025452 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453 }
25454}
25455
25456/*
25457 * Call a user function.
25458 */
25459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025460call_user_func(
25461 ufunc_T *fp, /* pointer to function */
25462 int argcount, /* nr of args */
25463 typval_T *argvars, /* arguments */
25464 typval_T *rettv, /* return value */
25465 linenr_T firstline, /* first line of range */
25466 linenr_T lastline, /* last line of range */
25467 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468{
Bram Moolenaar33570922005-01-25 22:26:29 +000025469 char_u *save_sourcing_name;
25470 linenr_T save_sourcing_lnum;
25471 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025472 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025473 int save_did_emsg;
25474 static int depth = 0;
25475 dictitem_T *v;
25476 int fixvar_idx = 0; /* index in fixvar[] */
25477 int i;
25478 int ai;
25479 char_u numbuf[NUMBUFLEN];
25480 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025481 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025482#ifdef FEAT_PROFILE
25483 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025484 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486
25487 /* If depth of calling is getting too high, don't execute the function */
25488 if (depth >= p_mfd)
25489 {
25490 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025491 rettv->v_type = VAR_NUMBER;
25492 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493 return;
25494 }
25495 ++depth;
25496
25497 line_breakcheck(); /* check for CTRL-C hit */
25498
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025499 fc = (funccall_T *)alloc(sizeof(funccall_T));
25500 fc->caller = current_funccal;
25501 current_funccal = fc;
25502 fc->func = fp;
25503 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025504 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025505 fc->linenr = 0;
25506 fc->returned = FALSE;
25507 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025509 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25510 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025511
Bram Moolenaar33570922005-01-25 22:26:29 +000025512 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025513 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025514 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25515 * each argument variable and saves a lot of time.
25516 */
25517 /*
25518 * Init l: variables.
25519 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025520 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025521 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025522 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025523 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25524 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025525 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025526 name = v->di_key;
25527 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025528 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025529 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025530 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025531 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025532 v->di_tv.vval.v_dict = selfdict;
25533 ++selfdict->dv_refcount;
25534 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025535
Bram Moolenaar33570922005-01-25 22:26:29 +000025536 /*
25537 * Init a: variables.
25538 * Set a:0 to "argcount".
25539 * Set a:000 to a list with room for the "..." arguments.
25540 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025541 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025542 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025543 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025544 /* Use "name" to avoid a warning from some compiler that checks the
25545 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025546 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025547 name = v->di_key;
25548 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025549 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025550 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025551 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025552 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025553 v->di_tv.vval.v_list = &fc->l_varlist;
25554 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25555 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25556 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025557
25558 /*
25559 * Set a:firstline to "firstline" and a:lastline to "lastline".
25560 * Set a:name to named arguments.
25561 * Set a:N to the "..." arguments.
25562 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025563 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025564 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025565 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025566 (varnumber_T)lastline);
25567 for (i = 0; i < argcount; ++i)
25568 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025569 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025570 if (ai < 0)
25571 /* named argument a:name */
25572 name = FUNCARG(fp, i);
25573 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025574 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025575 /* "..." argument a:1, a:2, etc. */
25576 sprintf((char *)numbuf, "%d", ai + 1);
25577 name = numbuf;
25578 }
25579 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25580 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025581 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025582 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25583 }
25584 else
25585 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025586 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25587 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025588 if (v == NULL)
25589 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025590 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025591 }
25592 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025593 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025594
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025595 /* Note: the values are copied directly to avoid alloc/free.
25596 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025597 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025598 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025599
25600 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25601 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025602 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25603 fc->l_listitems[ai].li_tv = argvars[i];
25604 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025605 }
25606 }
25607
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608 /* Don't redraw while executing the function. */
25609 ++RedrawingDisabled;
25610 save_sourcing_name = sourcing_name;
25611 save_sourcing_lnum = sourcing_lnum;
25612 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025613 /* need space for function name + ("function " + 3) or "[number]" */
25614 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25615 + STRLEN(fp->uf_name) + 20;
25616 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 if (sourcing_name != NULL)
25618 {
25619 if (save_sourcing_name != NULL
25620 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025621 sprintf((char *)sourcing_name, "%s[%d]..",
25622 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623 else
25624 STRCPY(sourcing_name, "function ");
25625 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25626
25627 if (p_verbose >= 12)
25628 {
25629 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025630 verbose_enter_scroll();
25631
Bram Moolenaar555b2802005-05-19 21:08:39 +000025632 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 if (p_verbose >= 14)
25634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025635 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025636 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025637 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025638 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025639
25640 msg_puts((char_u *)"(");
25641 for (i = 0; i < argcount; ++i)
25642 {
25643 if (i > 0)
25644 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025645 if (argvars[i].v_type == VAR_NUMBER)
25646 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647 else
25648 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025649 /* Do not want errors such as E724 here. */
25650 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025651 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025652 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025653 if (s != NULL)
25654 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025655 if (vim_strsize(s) > MSG_BUF_CLEN)
25656 {
25657 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25658 s = buf;
25659 }
25660 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025661 vim_free(tofree);
25662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663 }
25664 }
25665 msg_puts((char_u *)")");
25666 }
25667 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025668
25669 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 --no_wait_return;
25671 }
25672 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025673#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025674 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025675 {
25676 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25677 func_do_profile(fp);
25678 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025679 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025680 {
25681 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025682 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025683 profile_zero(&fp->uf_tm_children);
25684 }
25685 script_prof_save(&wait_start);
25686 }
25687#endif
25688
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025690 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025691 save_did_emsg = did_emsg;
25692 did_emsg = FALSE;
25693
25694 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025695 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025696 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25697
25698 --RedrawingDisabled;
25699
25700 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025701 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025702 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025703 clear_tv(rettv);
25704 rettv->v_type = VAR_NUMBER;
25705 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025706 }
25707
Bram Moolenaar05159a02005-02-26 23:04:13 +000025708#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025709 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025710 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025711 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025712 profile_end(&call_start);
25713 profile_sub_wait(&wait_start, &call_start);
25714 profile_add(&fp->uf_tm_total, &call_start);
25715 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025716 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025717 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025718 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25719 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025720 }
25721 }
25722#endif
25723
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724 /* when being verbose, mention the return value */
25725 if (p_verbose >= 12)
25726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025728 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729
Bram Moolenaar071d4272004-06-13 20:20:40 +000025730 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025731 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025732 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025733 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025734 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025735 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025736 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025737 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025738 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025739 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025740 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025741
Bram Moolenaar555b2802005-05-19 21:08:39 +000025742 /* The value may be very long. Skip the middle part, so that we
25743 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025744 * truncate it at the end. Don't want errors such as E724 here. */
25745 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025746 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025747 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025748 if (s != NULL)
25749 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025750 if (vim_strsize(s) > MSG_BUF_CLEN)
25751 {
25752 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25753 s = buf;
25754 }
25755 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025756 vim_free(tofree);
25757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025758 }
25759 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025760
25761 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025762 --no_wait_return;
25763 }
25764
25765 vim_free(sourcing_name);
25766 sourcing_name = save_sourcing_name;
25767 sourcing_lnum = save_sourcing_lnum;
25768 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025769#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025770 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025771 script_prof_restore(&wait_start);
25772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773
25774 if (p_verbose >= 12 && sourcing_name != NULL)
25775 {
25776 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025777 verbose_enter_scroll();
25778
Bram Moolenaar555b2802005-05-19 21:08:39 +000025779 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025780 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025781
25782 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 --no_wait_return;
25784 }
25785
25786 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025787 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025788 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025789
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025790 /* If the a:000 list and the l: and a: dicts are not referenced we can
25791 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025792 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25793 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25794 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25795 {
25796 free_funccal(fc, FALSE);
25797 }
25798 else
25799 {
25800 hashitem_T *hi;
25801 listitem_T *li;
25802 int todo;
25803
25804 /* "fc" is still in use. This can happen when returning "a:000" or
25805 * assigning "l:" to a global variable.
25806 * Link "fc" in the list for garbage collection later. */
25807 fc->caller = previous_funccal;
25808 previous_funccal = fc;
25809
25810 /* Make a copy of the a: variables, since we didn't do that above. */
25811 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25812 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25813 {
25814 if (!HASHITEM_EMPTY(hi))
25815 {
25816 --todo;
25817 v = HI2DI(hi);
25818 copy_tv(&v->di_tv, &v->di_tv);
25819 }
25820 }
25821
25822 /* Make a copy of the a:000 items, since we didn't do that above. */
25823 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25824 copy_tv(&li->li_tv, &li->li_tv);
25825 }
25826}
25827
25828/*
25829 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025830 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025831 */
25832 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025833can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025834{
25835 return (fc->l_varlist.lv_copyID != copyID
25836 && fc->l_vars.dv_copyID != copyID
25837 && fc->l_avars.dv_copyID != copyID);
25838}
25839
25840/*
25841 * Free "fc" and what it contains.
25842 */
25843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025844free_funccal(
25845 funccall_T *fc,
25846 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025847{
25848 listitem_T *li;
25849
25850 /* The a: variables typevals may not have been allocated, only free the
25851 * allocated variables. */
25852 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25853
25854 /* free all l: variables */
25855 vars_clear(&fc->l_vars.dv_hashtab);
25856
25857 /* Free the a:000 variables if they were allocated. */
25858 if (free_val)
25859 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25860 clear_tv(&li->li_tv);
25861
25862 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025863}
25864
25865/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025866 * Add a number variable "name" to dict "dp" with value "nr".
25867 */
25868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025869add_nr_var(
25870 dict_T *dp,
25871 dictitem_T *v,
25872 char *name,
25873 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025874{
25875 STRCPY(v->di_key, name);
25876 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25877 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25878 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025879 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025880 v->di_tv.vval.v_number = nr;
25881}
25882
25883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 * ":return [expr]"
25885 */
25886 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025887ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888{
25889 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025890 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 int returning = FALSE;
25892
25893 if (current_funccal == NULL)
25894 {
25895 EMSG(_("E133: :return not inside a function"));
25896 return;
25897 }
25898
25899 if (eap->skip)
25900 ++emsg_skip;
25901
25902 eap->nextcmd = NULL;
25903 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025904 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025905 {
25906 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025907 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025909 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 }
25911 /* It's safer to return also on error. */
25912 else if (!eap->skip)
25913 {
25914 /*
25915 * Return unless the expression evaluation has been cancelled due to an
25916 * aborting error, an interrupt, or an exception.
25917 */
25918 if (!aborting())
25919 returning = do_return(eap, FALSE, TRUE, NULL);
25920 }
25921
25922 /* When skipping or the return gets pending, advance to the next command
25923 * in this line (!returning). Otherwise, ignore the rest of the line.
25924 * Following lines will be ignored by get_func_line(). */
25925 if (returning)
25926 eap->nextcmd = NULL;
25927 else if (eap->nextcmd == NULL) /* no argument */
25928 eap->nextcmd = check_nextcmd(arg);
25929
25930 if (eap->skip)
25931 --emsg_skip;
25932}
25933
25934/*
25935 * Return from a function. Possibly makes the return pending. Also called
25936 * for a pending return at the ":endtry" or after returning from an extra
25937 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025938 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025939 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025940 * FALSE when the return gets pending.
25941 */
25942 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025943do_return(
25944 exarg_T *eap,
25945 int reanimate,
25946 int is_cmd,
25947 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948{
25949 int idx;
25950 struct condstack *cstack = eap->cstack;
25951
25952 if (reanimate)
25953 /* Undo the return. */
25954 current_funccal->returned = FALSE;
25955
25956 /*
25957 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25958 * not in its finally clause (which then is to be executed next) is found.
25959 * In this case, make the ":return" pending for execution at the ":endtry".
25960 * Otherwise, return normally.
25961 */
25962 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25963 if (idx >= 0)
25964 {
25965 cstack->cs_pending[idx] = CSTP_RETURN;
25966
25967 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025968 /* A pending return again gets pending. "rettv" points to an
25969 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025970 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025971 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025972 else
25973 {
25974 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025975 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025976 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025977 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025978
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025979 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980 {
25981 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025982 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025983 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984 else
25985 EMSG(_(e_outofmem));
25986 }
25987 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025988 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989
25990 if (reanimate)
25991 {
25992 /* The pending return value could be overwritten by a ":return"
25993 * without argument in a finally clause; reset the default
25994 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025995 current_funccal->rettv->v_type = VAR_NUMBER;
25996 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997 }
25998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025999 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 }
26001 else
26002 {
26003 current_funccal->returned = TRUE;
26004
26005 /* If the return is carried out now, store the return value. For
26006 * a return immediately after reanimation, the value is already
26007 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026008 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026010 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000026011 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026013 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026014 }
26015 }
26016
26017 return idx < 0;
26018}
26019
26020/*
26021 * Free the variable with a pending return value.
26022 */
26023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026024discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025{
Bram Moolenaar33570922005-01-25 22:26:29 +000026026 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026027}
26028
26029/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026030 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000026031 * is an allocated string. Used by report_pending() for verbose messages.
26032 */
26033 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026034get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026036 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026037 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026038 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026039
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026040 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026041 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026042 if (s == NULL)
26043 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026044
26045 STRCPY(IObuff, ":return ");
26046 STRNCPY(IObuff + 8, s, IOSIZE - 8);
26047 if (STRLEN(s) + 8 >= IOSIZE)
26048 STRCPY(IObuff + IOSIZE - 4, "...");
26049 vim_free(tofree);
26050 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051}
26052
26053/*
26054 * Get next function line.
26055 * Called by do_cmdline() to get the next line.
26056 * Returns allocated string, or NULL for end of function.
26057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026058 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026059get_func_line(
26060 int c UNUSED,
26061 void *cookie,
26062 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026063{
Bram Moolenaar33570922005-01-25 22:26:29 +000026064 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026065 ufunc_T *fp = fcp->func;
26066 char_u *retval;
26067 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068
26069 /* If breakpoints have been added/deleted need to check for it. */
26070 if (fcp->dbg_tick != debug_tick)
26071 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026072 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026073 sourcing_lnum);
26074 fcp->dbg_tick = debug_tick;
26075 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000026076#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026077 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026078 func_line_end(cookie);
26079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026080
Bram Moolenaar05159a02005-02-26 23:04:13 +000026081 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026082 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
26083 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 retval = NULL;
26085 else
26086 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026087 /* Skip NULL lines (continuation lines). */
26088 while (fcp->linenr < gap->ga_len
26089 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
26090 ++fcp->linenr;
26091 if (fcp->linenr >= gap->ga_len)
26092 retval = NULL;
26093 else
26094 {
26095 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
26096 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026097#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000026098 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026099 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026100#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102 }
26103
26104 /* Did we encounter a breakpoint? */
26105 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
26106 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000026107 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000026109 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110 sourcing_lnum);
26111 fcp->dbg_tick = debug_tick;
26112 }
26113
26114 return retval;
26115}
26116
Bram Moolenaar05159a02005-02-26 23:04:13 +000026117#if defined(FEAT_PROFILE) || defined(PROTO)
26118/*
26119 * Called when starting to read a function line.
26120 * "sourcing_lnum" must be correct!
26121 * When skipping lines it may not actually be executed, but we won't find out
26122 * until later and we need to store the time now.
26123 */
26124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026125func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026126{
26127 funccall_T *fcp = (funccall_T *)cookie;
26128 ufunc_T *fp = fcp->func;
26129
26130 if (fp->uf_profiling && sourcing_lnum >= 1
26131 && sourcing_lnum <= fp->uf_lines.ga_len)
26132 {
26133 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000026134 /* Skip continuation lines. */
26135 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
26136 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000026137 fp->uf_tml_execed = FALSE;
26138 profile_start(&fp->uf_tml_start);
26139 profile_zero(&fp->uf_tml_children);
26140 profile_get_wait(&fp->uf_tml_wait);
26141 }
26142}
26143
26144/*
26145 * Called when actually executing a function line.
26146 */
26147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026148func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026149{
26150 funccall_T *fcp = (funccall_T *)cookie;
26151 ufunc_T *fp = fcp->func;
26152
26153 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26154 fp->uf_tml_execed = TRUE;
26155}
26156
26157/*
26158 * Called when done with a function line.
26159 */
26160 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026161func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000026162{
26163 funccall_T *fcp = (funccall_T *)cookie;
26164 ufunc_T *fp = fcp->func;
26165
26166 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
26167 {
26168 if (fp->uf_tml_execed)
26169 {
26170 ++fp->uf_tml_count[fp->uf_tml_idx];
26171 profile_end(&fp->uf_tml_start);
26172 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026173 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000026174 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
26175 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000026176 }
26177 fp->uf_tml_idx = -1;
26178 }
26179}
26180#endif
26181
Bram Moolenaar071d4272004-06-13 20:20:40 +000026182/*
26183 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026184 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000026185 */
26186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026187func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026188{
Bram Moolenaar33570922005-01-25 22:26:29 +000026189 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190
26191 /* Ignore the "abort" flag if the abortion behavior has been changed due to
26192 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026193 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194 || fcp->returned);
26195}
26196
26197/*
26198 * return TRUE if cookie indicates a function which "abort"s on errors.
26199 */
26200 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026201func_has_abort(
26202 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026203{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000026204 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026205}
26206
26207#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
26208typedef enum
26209{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026210 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
26211 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
26212 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026213} var_flavour_T;
26214
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026215static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026216
26217 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010026218var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219{
26220 char_u *p = varname;
26221
26222 if (ASCII_ISUPPER(*p))
26223 {
26224 while (*(++p))
26225 if (ASCII_ISLOWER(*p))
26226 return VAR_FLAVOUR_SESSION;
26227 return VAR_FLAVOUR_VIMINFO;
26228 }
26229 else
26230 return VAR_FLAVOUR_DEFAULT;
26231}
26232#endif
26233
26234#if defined(FEAT_VIMINFO) || defined(PROTO)
26235/*
26236 * Restore global vars that start with a capital from the viminfo file
26237 */
26238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026239read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240{
26241 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026242 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000026243 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026244 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026245
26246 if (!writing && (find_viminfo_parameter('!') != NULL))
26247 {
26248 tab = vim_strchr(virp->vir_line + 1, '\t');
26249 if (tab != NULL)
26250 {
26251 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026252 switch (*tab)
26253 {
26254 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026255#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026256 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026257#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026258 case 'D': type = VAR_DICT; break;
26259 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026260 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026262
26263 tab = vim_strchr(tab, '\t');
26264 if (tab != NULL)
26265 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026266 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026267 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026268 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026270#ifdef FEAT_FLOAT
26271 else if (type == VAR_FLOAT)
26272 (void)string2float(tab + 1, &tv.vval.v_float);
26273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026274 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026275 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026276 if (type == VAR_DICT || type == VAR_LIST)
26277 {
26278 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
26279
26280 if (etv == NULL)
26281 /* Failed to parse back the dict or list, use it as a
26282 * string. */
26283 tv.v_type = VAR_STRING;
26284 else
26285 {
26286 vim_free(tv.vval.v_string);
26287 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010026288 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026289 }
26290 }
26291
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026292 /* when in a function use global variables */
26293 save_funccal = current_funccal;
26294 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026295 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010026296 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026297
26298 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026299 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026300 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
26301 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026302 }
26303 }
26304 }
26305
26306 return viminfo_readline(virp);
26307}
26308
26309/*
26310 * Write global vars that start with a capital to the viminfo file
26311 */
26312 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026313write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026314{
Bram Moolenaar33570922005-01-25 22:26:29 +000026315 hashitem_T *hi;
26316 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026317 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010026318 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026319 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026320 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000026321 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000026322
26323 if (find_viminfo_parameter('!') == NULL)
26324 return;
26325
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020026326 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000026327
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026328 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026329 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026330 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026331 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026332 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026333 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026334 this_var = HI2DI(hi);
26335 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026336 {
Bram Moolenaar33570922005-01-25 22:26:29 +000026337 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000026338 {
26339 case VAR_STRING: s = "STR"; break;
26340 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026341 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020026342 case VAR_DICT: s = "DIC"; break;
26343 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010026344 case VAR_SPECIAL: s = "XPL"; break;
26345
26346 case VAR_UNKNOWN:
26347 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010026348 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010026349 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010026350 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010026351 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000026352 }
Bram Moolenaar33570922005-01-25 22:26:29 +000026353 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000026354 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000026355 if (p != NULL)
26356 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000026357 vim_free(tofree);
26358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026359 }
26360 }
26361}
26362#endif
26363
26364#if defined(FEAT_SESSION) || defined(PROTO)
26365 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026366store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026367{
Bram Moolenaar33570922005-01-25 22:26:29 +000026368 hashitem_T *hi;
26369 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000026370 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026371 char_u *p, *t;
26372
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026373 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000026374 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026376 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026377 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026378 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000026379 this_var = HI2DI(hi);
26380 if ((this_var->di_tv.v_type == VAR_NUMBER
26381 || this_var->di_tv.v_type == VAR_STRING)
26382 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000026383 {
Bram Moolenaara7043832005-01-21 11:56:39 +000026384 /* Escape special characters with a backslash. Turn a LF and
26385 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000026386 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000026387 (char_u *)"\\\"\n\r");
26388 if (p == NULL) /* out of memory */
26389 break;
26390 for (t = p; *t != NUL; ++t)
26391 if (*t == '\n')
26392 *t = 'n';
26393 else if (*t == '\r')
26394 *t = 'r';
26395 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000026396 this_var->di_key,
26397 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26398 : ' ',
26399 p,
26400 (this_var->di_tv.v_type == VAR_STRING) ? '"'
26401 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000026402 || put_eol(fd) == FAIL)
26403 {
26404 vim_free(p);
26405 return FAIL;
26406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026407 vim_free(p);
26408 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026409#ifdef FEAT_FLOAT
26410 else if (this_var->di_tv.v_type == VAR_FLOAT
26411 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26412 {
26413 float_T f = this_var->di_tv.vval.v_float;
26414 int sign = ' ';
26415
26416 if (f < 0)
26417 {
26418 f = -f;
26419 sign = '-';
26420 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026421 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026422 this_var->di_key, sign, f) < 0)
26423 || put_eol(fd) == FAIL)
26424 return FAIL;
26425 }
26426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026427 }
26428 }
26429 return OK;
26430}
26431#endif
26432
Bram Moolenaar661b1822005-07-28 22:36:45 +000026433/*
26434 * Display script name where an item was last set.
26435 * Should only be invoked when 'verbose' is non-zero.
26436 */
26437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026438last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026439{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026440 char_u *p;
26441
Bram Moolenaar661b1822005-07-28 22:36:45 +000026442 if (scriptID != 0)
26443 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026444 p = home_replace_save(NULL, get_scriptname(scriptID));
26445 if (p != NULL)
26446 {
26447 verbose_enter();
26448 MSG_PUTS(_("\n\tLast set from "));
26449 MSG_PUTS(p);
26450 vim_free(p);
26451 verbose_leave();
26452 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026453 }
26454}
26455
Bram Moolenaard812df62008-11-09 12:46:09 +000026456/*
26457 * List v:oldfiles in a nice way.
26458 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026459 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026460ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026461{
26462 list_T *l = vimvars[VV_OLDFILES].vv_list;
26463 listitem_T *li;
26464 int nr = 0;
26465
26466 if (l == NULL)
26467 msg((char_u *)_("No old files"));
26468 else
26469 {
26470 msg_start();
26471 msg_scroll = TRUE;
26472 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26473 {
26474 msg_outnum((long)++nr);
26475 MSG_PUTS(": ");
26476 msg_outtrans(get_tv_string(&li->li_tv));
26477 msg_putchar('\n');
26478 out_flush(); /* output one line at a time */
26479 ui_breakcheck();
26480 }
26481 /* Assume "got_int" was set to truncate the listing. */
26482 got_int = FALSE;
26483
26484#ifdef FEAT_BROWSE_CMD
26485 if (cmdmod.browse)
26486 {
26487 quit_more = FALSE;
26488 nr = prompt_for_number(FALSE);
26489 msg_starthere();
26490 if (nr > 0)
26491 {
26492 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26493 (long)nr);
26494
26495 if (p != NULL)
26496 {
26497 p = expand_env_save(p);
26498 eap->arg = p;
26499 eap->cmdidx = CMD_edit;
26500 cmdmod.browse = FALSE;
26501 do_exedit(eap, NULL);
26502 vim_free(p);
26503 }
26504 }
26505 }
26506#endif
26507 }
26508}
26509
Bram Moolenaar53744302015-07-17 17:38:22 +020026510/* reset v:option_new, v:option_old and v:option_type */
26511 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026512reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026513{
26514 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26515 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26516 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26517}
26518
26519
Bram Moolenaar071d4272004-06-13 20:20:40 +000026520#endif /* FEAT_EVAL */
26521
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026523#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524
26525#ifdef WIN3264
26526/*
26527 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26528 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026529static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26530static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26531static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532
26533/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026534 * Get the short path (8.3) for the filename in "fnamep".
26535 * Only works for a valid file name.
26536 * When the path gets longer "fnamep" is changed and the allocated buffer
26537 * is put in "bufp".
26538 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26539 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026540 */
26541 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026542get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026543{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026544 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026545 char_u *newbuf;
26546
26547 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026548 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026549 if (l > len - 1)
26550 {
26551 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026552 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026553 newbuf = vim_strnsave(*fnamep, l);
26554 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026555 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026556
26557 vim_free(*bufp);
26558 *fnamep = *bufp = newbuf;
26559
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026560 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026561 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026562 }
26563
26564 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026565 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566}
26567
26568/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026569 * Get the short path (8.3) for the filename in "fname". The converted
26570 * path is returned in "bufp".
26571 *
26572 * Some of the directories specified in "fname" may not exist. This function
26573 * will shorten the existing directories at the beginning of the path and then
26574 * append the remaining non-existing path.
26575 *
26576 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026577 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026578 * bufp - Pointer to an allocated buffer for the filename.
26579 * fnamelen - Length of the filename pointed to by fname
26580 *
26581 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 */
26583 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026584shortpath_for_invalid_fname(
26585 char_u **fname,
26586 char_u **bufp,
26587 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026588{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026589 char_u *short_fname, *save_fname, *pbuf_unused;
26590 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026591 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026592 int old_len, len;
26593 int new_len, sfx_len;
26594 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595
26596 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026597 old_len = *fnamelen;
26598 save_fname = vim_strnsave(*fname, old_len);
26599 pbuf_unused = NULL;
26600 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026601
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026602 endp = save_fname + old_len - 1; /* Find the end of the copy */
26603 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026604
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026605 /*
26606 * Try shortening the supplied path till it succeeds by removing one
26607 * directory at a time from the tail of the path.
26608 */
26609 len = 0;
26610 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026611 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026612 /* go back one path-separator */
26613 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26614 --endp;
26615 if (endp <= save_fname)
26616 break; /* processed the complete path */
26617
26618 /*
26619 * Replace the path separator with a NUL and try to shorten the
26620 * resulting path.
26621 */
26622 ch = *endp;
26623 *endp = 0;
26624 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026625 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026626 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26627 {
26628 retval = FAIL;
26629 goto theend;
26630 }
26631 *endp = ch; /* preserve the string */
26632
26633 if (len > 0)
26634 break; /* successfully shortened the path */
26635
26636 /* failed to shorten the path. Skip the path separator */
26637 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026638 }
26639
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026640 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026641 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026642 /*
26643 * Succeeded in shortening the path. Now concatenate the shortened
26644 * path with the remaining path at the tail.
26645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026646
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026647 /* Compute the length of the new path. */
26648 sfx_len = (int)(save_endp - endp) + 1;
26649 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026650
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026651 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026652 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026653 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026654 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026655 /* There is not enough space in the currently allocated string,
26656 * copy it to a buffer big enough. */
26657 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026658 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026659 {
26660 retval = FAIL;
26661 goto theend;
26662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026663 }
26664 else
26665 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026666 /* Transfer short_fname to the main buffer (it's big enough),
26667 * unless get_short_pathname() did its work in-place. */
26668 *fname = *bufp = save_fname;
26669 if (short_fname != save_fname)
26670 vim_strncpy(save_fname, short_fname, len);
26671 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026672 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026673
26674 /* concat the not-shortened part of the path */
26675 vim_strncpy(*fname + len, endp, sfx_len);
26676 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026677 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026678
26679theend:
26680 vim_free(pbuf_unused);
26681 vim_free(save_fname);
26682
26683 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026684}
26685
26686/*
26687 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026688 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026689 */
26690 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026691shortpath_for_partial(
26692 char_u **fnamep,
26693 char_u **bufp,
26694 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026695{
26696 int sepcount, len, tflen;
26697 char_u *p;
26698 char_u *pbuf, *tfname;
26699 int hasTilde;
26700
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026701 /* Count up the path separators from the RHS.. so we know which part
26702 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026703 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026704 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026705 if (vim_ispathsep(*p))
26706 ++sepcount;
26707
26708 /* Need full path first (use expand_env() to remove a "~/") */
26709 hasTilde = (**fnamep == '~');
26710 if (hasTilde)
26711 pbuf = tfname = expand_env_save(*fnamep);
26712 else
26713 pbuf = tfname = FullName_save(*fnamep, FALSE);
26714
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026715 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026716
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026717 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026719
26720 if (len == 0)
26721 {
26722 /* Don't have a valid filename, so shorten the rest of the
26723 * path if we can. This CAN give us invalid 8.3 filenames, but
26724 * there's not a lot of point in guessing what it might be.
26725 */
26726 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026727 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26728 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026729 }
26730
26731 /* Count the paths backward to find the beginning of the desired string. */
26732 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026733 {
26734#ifdef FEAT_MBYTE
26735 if (has_mbyte)
26736 p -= mb_head_off(tfname, p);
26737#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026738 if (vim_ispathsep(*p))
26739 {
26740 if (sepcount == 0 || (hasTilde && sepcount == 1))
26741 break;
26742 else
26743 sepcount --;
26744 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026746 if (hasTilde)
26747 {
26748 --p;
26749 if (p >= tfname)
26750 *p = '~';
26751 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026753 }
26754 else
26755 ++p;
26756
26757 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26758 vim_free(*bufp);
26759 *fnamelen = (int)STRLEN(p);
26760 *bufp = pbuf;
26761 *fnamep = p;
26762
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026763 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026764}
26765#endif /* WIN3264 */
26766
26767/*
26768 * Adjust a filename, according to a string of modifiers.
26769 * *fnamep must be NUL terminated when called. When returning, the length is
26770 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026771 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026772 * When there is an error, *fnamep is set to NULL.
26773 */
26774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026775modify_fname(
26776 char_u *src, /* string with modifiers */
26777 int *usedlen, /* characters after src that are used */
26778 char_u **fnamep, /* file name so far */
26779 char_u **bufp, /* buffer for allocated file name or NULL */
26780 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026781{
26782 int valid = 0;
26783 char_u *tail;
26784 char_u *s, *p, *pbuf;
26785 char_u dirname[MAXPATHL];
26786 int c;
26787 int has_fullname = 0;
26788#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026789 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026790 int has_shortname = 0;
26791#endif
26792
26793repeat:
26794 /* ":p" - full path/file_name */
26795 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26796 {
26797 has_fullname = 1;
26798
26799 valid |= VALID_PATH;
26800 *usedlen += 2;
26801
26802 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26803 if ((*fnamep)[0] == '~'
26804#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26805 && ((*fnamep)[1] == '/'
26806# ifdef BACKSLASH_IN_FILENAME
26807 || (*fnamep)[1] == '\\'
26808# endif
26809 || (*fnamep)[1] == NUL)
26810
26811#endif
26812 )
26813 {
26814 *fnamep = expand_env_save(*fnamep);
26815 vim_free(*bufp); /* free any allocated file name */
26816 *bufp = *fnamep;
26817 if (*fnamep == NULL)
26818 return -1;
26819 }
26820
26821 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026822 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026823 {
26824 if (vim_ispathsep(*p)
26825 && p[1] == '.'
26826 && (p[2] == NUL
26827 || vim_ispathsep(p[2])
26828 || (p[2] == '.'
26829 && (p[3] == NUL || vim_ispathsep(p[3])))))
26830 break;
26831 }
26832
26833 /* FullName_save() is slow, don't use it when not needed. */
26834 if (*p != NUL || !vim_isAbsName(*fnamep))
26835 {
26836 *fnamep = FullName_save(*fnamep, *p != NUL);
26837 vim_free(*bufp); /* free any allocated file name */
26838 *bufp = *fnamep;
26839 if (*fnamep == NULL)
26840 return -1;
26841 }
26842
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026843#ifdef WIN3264
26844# if _WIN32_WINNT >= 0x0500
26845 if (vim_strchr(*fnamep, '~') != NULL)
26846 {
26847 /* Expand 8.3 filename to full path. Needed to make sure the same
26848 * file does not have two different names.
26849 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26850 p = alloc(_MAX_PATH + 1);
26851 if (p != NULL)
26852 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026853 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026854 {
26855 vim_free(*bufp);
26856 *bufp = *fnamep = p;
26857 }
26858 else
26859 vim_free(p);
26860 }
26861 }
26862# endif
26863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026864 /* Append a path separator to a directory. */
26865 if (mch_isdir(*fnamep))
26866 {
26867 /* Make room for one or two extra characters. */
26868 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26869 vim_free(*bufp); /* free any allocated file name */
26870 *bufp = *fnamep;
26871 if (*fnamep == NULL)
26872 return -1;
26873 add_pathsep(*fnamep);
26874 }
26875 }
26876
26877 /* ":." - path relative to the current directory */
26878 /* ":~" - path relative to the home directory */
26879 /* ":8" - shortname path - postponed till after */
26880 while (src[*usedlen] == ':'
26881 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26882 {
26883 *usedlen += 2;
26884 if (c == '8')
26885 {
26886#ifdef WIN3264
26887 has_shortname = 1; /* Postpone this. */
26888#endif
26889 continue;
26890 }
26891 pbuf = NULL;
26892 /* Need full path first (use expand_env() to remove a "~/") */
26893 if (!has_fullname)
26894 {
26895 if (c == '.' && **fnamep == '~')
26896 p = pbuf = expand_env_save(*fnamep);
26897 else
26898 p = pbuf = FullName_save(*fnamep, FALSE);
26899 }
26900 else
26901 p = *fnamep;
26902
26903 has_fullname = 0;
26904
26905 if (p != NULL)
26906 {
26907 if (c == '.')
26908 {
26909 mch_dirname(dirname, MAXPATHL);
26910 s = shorten_fname(p, dirname);
26911 if (s != NULL)
26912 {
26913 *fnamep = s;
26914 if (pbuf != NULL)
26915 {
26916 vim_free(*bufp); /* free any allocated file name */
26917 *bufp = pbuf;
26918 pbuf = NULL;
26919 }
26920 }
26921 }
26922 else
26923 {
26924 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26925 /* Only replace it when it starts with '~' */
26926 if (*dirname == '~')
26927 {
26928 s = vim_strsave(dirname);
26929 if (s != NULL)
26930 {
26931 *fnamep = s;
26932 vim_free(*bufp);
26933 *bufp = s;
26934 }
26935 }
26936 }
26937 vim_free(pbuf);
26938 }
26939 }
26940
26941 tail = gettail(*fnamep);
26942 *fnamelen = (int)STRLEN(*fnamep);
26943
26944 /* ":h" - head, remove "/file_name", can be repeated */
26945 /* Don't remove the first "/" or "c:\" */
26946 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26947 {
26948 valid |= VALID_HEAD;
26949 *usedlen += 2;
26950 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026951 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026952 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026953 *fnamelen = (int)(tail - *fnamep);
26954#ifdef VMS
26955 if (*fnamelen > 0)
26956 *fnamelen += 1; /* the path separator is part of the path */
26957#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026958 if (*fnamelen == 0)
26959 {
26960 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26961 p = vim_strsave((char_u *)".");
26962 if (p == NULL)
26963 return -1;
26964 vim_free(*bufp);
26965 *bufp = *fnamep = tail = p;
26966 *fnamelen = 1;
26967 }
26968 else
26969 {
26970 while (tail > s && !after_pathsep(s, tail))
26971 mb_ptr_back(*fnamep, tail);
26972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026973 }
26974
26975 /* ":8" - shortname */
26976 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26977 {
26978 *usedlen += 2;
26979#ifdef WIN3264
26980 has_shortname = 1;
26981#endif
26982 }
26983
26984#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026985 /*
26986 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026987 */
26988 if (has_shortname)
26989 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026990 /* Copy the string if it is shortened by :h and when it wasn't copied
26991 * yet, because we are going to change it in place. Avoids changing
26992 * the buffer name for "%:8". */
26993 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026994 {
26995 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026996 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026997 return -1;
26998 vim_free(*bufp);
26999 *bufp = *fnamep = p;
27000 }
27001
27002 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020027003 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027004 if (!has_fullname && !vim_isAbsName(*fnamep))
27005 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027006 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027007 return -1;
27008 }
27009 else
27010 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027011 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027012
Bram Moolenaardc935552011-08-17 15:23:23 +020027013 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000027014 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027015 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027016 return -1;
27017
27018 if (l == 0)
27019 {
Bram Moolenaardc935552011-08-17 15:23:23 +020027020 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027021 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000027022 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027023 return -1;
27024 }
27025 *fnamelen = l;
27026 }
27027 }
27028#endif /* WIN3264 */
27029
27030 /* ":t" - tail, just the basename */
27031 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
27032 {
27033 *usedlen += 2;
27034 *fnamelen -= (int)(tail - *fnamep);
27035 *fnamep = tail;
27036 }
27037
27038 /* ":e" - extension, can be repeated */
27039 /* ":r" - root, without extension, can be repeated */
27040 while (src[*usedlen] == ':'
27041 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
27042 {
27043 /* find a '.' in the tail:
27044 * - for second :e: before the current fname
27045 * - otherwise: The last '.'
27046 */
27047 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
27048 s = *fnamep - 2;
27049 else
27050 s = *fnamep + *fnamelen - 1;
27051 for ( ; s > tail; --s)
27052 if (s[0] == '.')
27053 break;
27054 if (src[*usedlen + 1] == 'e') /* :e */
27055 {
27056 if (s > tail)
27057 {
27058 *fnamelen += (int)(*fnamep - (s + 1));
27059 *fnamep = s + 1;
27060#ifdef VMS
27061 /* cut version from the extension */
27062 s = *fnamep + *fnamelen - 1;
27063 for ( ; s > *fnamep; --s)
27064 if (s[0] == ';')
27065 break;
27066 if (s > *fnamep)
27067 *fnamelen = s - *fnamep;
27068#endif
27069 }
27070 else if (*fnamep <= tail)
27071 *fnamelen = 0;
27072 }
27073 else /* :r */
27074 {
27075 if (s > tail) /* remove one extension */
27076 *fnamelen = (int)(s - *fnamep);
27077 }
27078 *usedlen += 2;
27079 }
27080
27081 /* ":s?pat?foo?" - substitute */
27082 /* ":gs?pat?foo?" - global substitute */
27083 if (src[*usedlen] == ':'
27084 && (src[*usedlen + 1] == 's'
27085 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
27086 {
27087 char_u *str;
27088 char_u *pat;
27089 char_u *sub;
27090 int sep;
27091 char_u *flags;
27092 int didit = FALSE;
27093
27094 flags = (char_u *)"";
27095 s = src + *usedlen + 2;
27096 if (src[*usedlen + 1] == 'g')
27097 {
27098 flags = (char_u *)"g";
27099 ++s;
27100 }
27101
27102 sep = *s++;
27103 if (sep)
27104 {
27105 /* find end of pattern */
27106 p = vim_strchr(s, sep);
27107 if (p != NULL)
27108 {
27109 pat = vim_strnsave(s, (int)(p - s));
27110 if (pat != NULL)
27111 {
27112 s = p + 1;
27113 /* find end of substitution */
27114 p = vim_strchr(s, sep);
27115 if (p != NULL)
27116 {
27117 sub = vim_strnsave(s, (int)(p - s));
27118 str = vim_strnsave(*fnamep, *fnamelen);
27119 if (sub != NULL && str != NULL)
27120 {
27121 *usedlen = (int)(p + 1 - src);
27122 s = do_string_sub(str, pat, sub, flags);
27123 if (s != NULL)
27124 {
27125 *fnamep = s;
27126 *fnamelen = (int)STRLEN(s);
27127 vim_free(*bufp);
27128 *bufp = s;
27129 didit = TRUE;
27130 }
27131 }
27132 vim_free(sub);
27133 vim_free(str);
27134 }
27135 vim_free(pat);
27136 }
27137 }
27138 /* after using ":s", repeat all the modifiers */
27139 if (didit)
27140 goto repeat;
27141 }
27142 }
27143
Bram Moolenaar26df0922014-02-23 23:39:13 +010027144 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
27145 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010027146 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010027147 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027148 if (c != NUL)
27149 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027150 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010027151 if (c != NUL)
27152 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010027153 if (p == NULL)
27154 return -1;
27155 vim_free(*bufp);
27156 *bufp = *fnamep = p;
27157 *fnamelen = (int)STRLEN(p);
27158 *usedlen += 2;
27159 }
27160
Bram Moolenaar071d4272004-06-13 20:20:40 +000027161 return valid;
27162}
27163
27164/*
27165 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
27166 * "flags" can be "g" to do a global substitute.
27167 * Returns an allocated string, NULL for error.
27168 */
27169 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010027170do_string_sub(
27171 char_u *str,
27172 char_u *pat,
27173 char_u *sub,
27174 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000027175{
27176 int sublen;
27177 regmatch_T regmatch;
27178 int i;
27179 int do_all;
27180 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027181 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027182 garray_T ga;
27183 char_u *ret;
27184 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027185 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027186
27187 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
27188 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027189 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027190
27191 ga_init2(&ga, 1, 200);
27192
27193 do_all = (flags[0] == 'g');
27194
27195 regmatch.rm_ic = p_ic;
27196 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
27197 if (regmatch.regprog != NULL)
27198 {
27199 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010027200 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027201 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
27202 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010027203 /* Skip empty match except for first match. */
27204 if (regmatch.startp[0] == regmatch.endp[0])
27205 {
27206 if (zero_width == regmatch.startp[0])
27207 {
27208 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020027209 i = MB_PTR2LEN(tail);
27210 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
27211 (size_t)i);
27212 ga.ga_len += i;
27213 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010027214 continue;
27215 }
27216 zero_width = regmatch.startp[0];
27217 }
27218
Bram Moolenaar071d4272004-06-13 20:20:40 +000027219 /*
27220 * Get some space for a temporary buffer to do the substitution
27221 * into. It will contain:
27222 * - The text up to where the match is.
27223 * - The substituted text.
27224 * - The text after the match.
27225 */
27226 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010027227 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000027228 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
27229 {
27230 ga_clear(&ga);
27231 break;
27232 }
27233
27234 /* copy the text up to where the match is */
27235 i = (int)(regmatch.startp[0] - tail);
27236 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
27237 /* add the substituted text */
27238 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
27239 + ga.ga_len + i, TRUE, TRUE, FALSE);
27240 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020027241 tail = regmatch.endp[0];
27242 if (*tail == NUL)
27243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000027244 if (!do_all)
27245 break;
27246 }
27247
27248 if (ga.ga_data != NULL)
27249 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
27250
Bram Moolenaar473de612013-06-08 18:19:48 +020027251 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027252 }
27253
27254 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
27255 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000027256 if (p_cpo == empty_option)
27257 p_cpo = save_cpo;
27258 else
27259 /* Darn, evaluating {sub} expression changed the value. */
27260 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000027261
27262 return ret;
27263}
27264
27265#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */